Discuss / Java / 作业

作业

Topic source
import java.util.Scanner;

/**
 * switch实现石头/剪子/布并判断胜负
 */
public class Main {

	public static void main(String[] args) {
                                String[] choices = {"Rock", "Scissors", "Paper"};
		System.out.println("please choice:");
		System.out.println(choices[0]);
		System.out.println(choices[1]);
		System.out.println(choices[2]);
		// 用户输入:
		Scanner scanner = new Scanner(System.in);
		System.out.print("Your choice:");
		int choice = scanner.nextInt() - 1;
		// 计算机随机数 1, 2, 3:
		int random = (int) (Math.random() * 3);
		int battle = (int) choice - random;
		System.out.println("Your choice is " + choices[choice]);
		System.out.println("My choice is " + choices[random]);
		switch (battle) {
		// TODO:
		case -1:
		case 2:
			System.out.println("You Win");
			break;
		case 1:
		case -2:
			System.out.println("You Lose");
			break;
        default:
            System.out.println("Draw");
            break;
		}
	}

}
import java.util.Scanner;

/**
 * switch实现石头/剪子/布并判断胜负
 */
public class Main {

	public static void main(String[] args) {
        String[] choices = {"Rock", "Scissors", "Paper"};
		System.out.println("please choice:");
		System.out.println("1: " + choices[0]);
		System.out.println("2: " + choices[1]);
		System.out.println("3: " + choices[2]);
		// 用户输入:
		Scanner scanner = new Scanner(System.in);
		System.out.print("Your choice:");
		int choice = scanner.nextInt() - 1;
		// 计算机随机数 1, 2, 3:
		int random = (int) (Math.random() * 3);
		int battle = (int) choice - random;
		System.out.println("Your choice is " + choices[choice]);
		System.out.println("My choice is " + choices[random]);
		switch (battle) {
		// TODO:
		case -1:
		case 2:
			System.out.println("You Win!");
			break;
		case 1:
		case -2:
			System.out.println("You Lose!");
			break;
        default:
            System.out.println("Draw!");
            break;
		}
	}

}

  • 1

Reply