Java的石头剪刀布简易判断练习。
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
var scanner = new Scanner(System.in);
System.out.print("请出招(1.石头,2.剪刀,3.布):");
int last = scanner.nextInt();
//首先对输入进行判断,确保输入正确。
switch(last){
case 1:
case 2:
case 3:
var computer = (int)(Math.random()*3)+1;
System.out.printf("电脑出招为:%d\n", computer);
var result = last - computer;
//对人机输入进行结果判断。
switch (result){
case 0:
System.out.println("平局");
break;
case -1:
case 2:
System.out.println("You Win.");
break;
case -2:
case 1:
System.out.println("You Lost.");
break;
}
break;
default:
System.out.println("请重新出招.");
break;
}
}
}
网友评论