Discuss / Java / 交作业

交作业

Topic source
public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String[] type = {"石头","剪刀","布"};
		System.out.println("请选择:");
		System.out.println(" 1: "+type[0]);
		System.out.println(" 2: "+type[1]);
		System.out.println(" 3: "+type[2]);
		int choice = scanner.nextInt();
		while (choice > 3 || choice < 1) {
			System.out.println("请输入1,2,3 三种之内的数字");
			choice = scanner.nextInt();
		}
		int random = 1 + (int) Math.random() * 3;
		String result =  switch (choice - random) {
			case 0 -> {yield "平局";}
			case -1,2 -> {yield  "您赢了";}
			case -2,1 -> {yield "您输了";}
			default -> {yield "";}
		};
		
		System.out.printf("电脑出的是:%s,您出的是:%s, %s", type[random-1], type[choice-1], result);
	}

  • 1

Reply