本文实例为大家分享了java实现猜拳游戏的具体代码,供大家参考,具体内容如下
package com.farsight.session7;
import java.util.Scanner;
/**
* 根据输入的数字,判断数组strs的值 然后进行逻辑判断
*/
public class 猜拳 {
public static void main(String[] args) {
String[] strs = { "石头", "剪刀", "布" };
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的选择:(0.石头 1.剪刀 2.布)");
int input = sc.nextInt();
String input_str = strs[input];
System.out.println("您输入的是:" + input_str);
int ran = (int) (Math.random() * 3);
String sys_str = strs[ran];
System.out.println("系统输出的是:" + sys_str);
if (input_str.equals(sys_str)) {
System.err.println("你们是平局");
} else if (input_str == "石头" && sys_str == "剪刀" || input_str == "剪刀"
&& sys_str == "布" || input_str == "布" && sys_str == "石头") {
System.err.println("你赢了");
} else {
System.err.println("你输了");
}
}
}
运行结果:
请输入您的选择:(0.石头 1.剪刀 2.布)
2
您输入的是:布
你赢了
系统输出的是:石头
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。