package com.itranswarp.learnjava;
import java.util.Scanner;
/**
* switch实现石头/剪子/布并判断胜负
*/
public class Main {
public static void main(String[] args) {
System.out.println("please choice:");
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
// 用户输入:
int choice = 0;
int random = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Input your choice:");
choice = scanner.nextInt();
switch (choice) {
// TODO:
case 1:
System.out.println("You choose Rock");
// 计算机随机数 1, 2, 3:
random = 1 + (int) Math.random() * 3;
switch(random) {
System.out.println("Machine choose Rock");
System.out.println("draw");
break;
case 2:
System.out.println("Machine choose Scissors");
System.out.println("you win!");
case 3:
System.out.println("Machine choose paper");
System.out.println("you lose");
}
System.out.println("You choose Scissors");
System.out.println("you win");
System.out.println("You choose Paper");
default:
System.out.println("error select");
Sign in to make a reply
灵光大侠
package com.itranswarp.learnjava;
import java.util.Scanner;
/**
* switch实现石头/剪子/布并判断胜负
*/
public class Main {
public static void main(String[] args) {
System.out.println("please choice:");
System.out.println(" 1: Rock");
System.out.println(" 2: Scissors");
System.out.println(" 3: Paper");
// 用户输入:
int choice = 0;
int random = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Input your choice:");
choice = scanner.nextInt();
switch (choice) {
// TODO:
case 1:
System.out.println("You choose Rock");
// 计算机随机数 1, 2, 3:
random = 1 + (int) Math.random() * 3;
switch(random) {
case 1:
System.out.println("Machine choose Rock");
System.out.println("draw");
break;
case 2:
System.out.println("Machine choose Scissors");
System.out.println("you win!");
break;
case 3:
System.out.println("Machine choose paper");
System.out.println("you lose");
break;
}
break;
case 2:
System.out.println("You choose Scissors");
random = 1 + (int) Math.random() * 3;
switch(random) {
case 1:
System.out.println("Machine choose Rock");
System.out.println("you lose");
break;
case 2:
System.out.println("Machine choose Scissors");
System.out.println("draw");
break;
case 3:
System.out.println("Machine choose paper");
System.out.println("you win");
break;
}
break;
case 3:
System.out.println("You choose Paper");
random = 1 + (int) Math.random() * 3;
switch(random) {
case 1:
System.out.println("Machine choose Rock");
System.out.println("you win");
break;
case 2:
System.out.println("Machine choose Scissors");
System.out.println("you lose");
break;
case 3:
System.out.println("Machine choose paper");
System.out.println("draw");
break;
}
break;
default:
System.out.println("error select");
break;
}
}
}