打卡
Topic sourcepackage com.testted;
import java.util.Scanner;
/**
* switch实现石头/剪子/布并判断胜负
*/
public class Main {
public static void main(String[] args) {
System.out.println("请输入你的选择:");
System.out.println(" 1: 石头");
System.out.println(" 2: 剪刀");
System.out.println(" 3: 布");
String [] result = {"石头","剪刀","布"};
// 用户输入:
int choice =0 ;
while (choice!=1&&choice!=2&&choice!=3){
System.out.println("请输入:1,2,3中的其中一个数字");
Scanner a = new Scanner(System.in);
choice = a.nextInt();
}
System.out.println("您的选择为"+result[choice-1]);
// 计算机随机数 1, 2, 3:
int random = 1 + (int) (Math.random() * 3);
System.out.println("电脑的选择为"+result[random-1]);
int b = random - choice;
switch (b) {
// TODO:
case 2:
System.out.println("你输了!");
break;
case 1:
System.out.println("你赢了!");
break;
case 0:
System.out.println("平局");
break;
case -1:
System.out.println("你输了!");
break;
case -2:
System.out.println("你赢了!");
break;
default:
System.out.println("请输入:1,2,3 中的一个数字");
}
}
}
- 1
lch要好好学习