System.out.println(""" 1. Rock 2. Scissors 3. Paper """); System.out.println("请输入的你的选择:\n"); // 设置输入 Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); int random = 1+ (int)(Math.random()*3); System.out.println("你的选择是"+GetGameChoice(choice)); System.out.println("电脑的选择是"+GetGameChoice(random)); if (GetGameChoice(choice) == "无效选择"){ System.out.println("退出游戏"); }else if (choice == random){ System.out.println("平局"); }else{ switch (choice){ case 1 : // 用户石头 if (random == 2){ // 电脑剪刀 System.out.println("你赢了"); }else { System.out.println("你输了"); } break; case 2 : // 用户剪刀 if (random == 1){ //电脑选择布 System.out.println("你输了"); }else System.out.println("你赢了"); case 3 : //用户布 if(random == 1){ // 电脑选择石头 System.out.println("你赢了"); }else System.out.println("你输了"); } }
} public static String GetGameChoice(int choice){ switch (choice){ case 1 : return "Rock"; case 2 : return "Scissors"; case 3: return "Paper"; default: return "无效选择"; } }
Sign in to make a reply
Zane
System.out.println("""
1. Rock
2. Scissors
3. Paper
""");
System.out.println("请输入的你的选择:\n");
// 设置输入
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
int random = 1+ (int)(Math.random()*3);
System.out.println("你的选择是"+GetGameChoice(choice));
System.out.println("电脑的选择是"+GetGameChoice(random));
if (GetGameChoice(choice) == "无效选择"){
System.out.println("退出游戏");
}else if (choice == random){
System.out.println("平局");
}else{
switch (choice){
case 1 : // 用户石头
if (random == 2){ // 电脑剪刀
System.out.println("你赢了");
}else {
System.out.println("你输了");
}
break;
case 2 : // 用户剪刀
if (random == 1){ //电脑选择布
System.out.println("你输了");
}else
System.out.println("你赢了");
case 3 : //用户布
if(random == 1){ // 电脑选择石头
System.out.println("你赢了");
}else
System.out.println("你输了");
}
}
}
public static String GetGameChoice(int choice){
switch (choice){
case 1 :
return "Rock";
case 2 :
return "Scissors";
case 3:
return "Paper";
default:
return "无效选择";
}
}