Discuss / Java / 交作业

交作业

Topic source

小梁20420

#1 Created at ... [Delete] [Delete and Lock User]

package com.itranswarp.learnjava;

import java.util.Scanner;

/**

 * switch实现石头/剪子/布并判断胜负

 */

public class Main {

 /**

  * @param args

  */

 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");

  // 用户输入:

  double choice = 0;

  Scanner scanner = new Scanner(System.in);

  choice = scanner.nextDouble();

  if((choice != 3) && (choice != 2) && (choice != 1)){

   System.out.println("输入值错误,只能输入整数1、2、3");

   return ;

  }

  // 计算机随机数 1, 2, 3:

  int random = 1 + (int) Math.random() * 3;

  System.out.println("电脑的策略(1为石头 2为剪刀 3为布)是"+random);

  switch ((int) choice - random) {

  // TODO:

  case 0   -> System.out.println("平局");

  case 1,-2 -> System.out.println("输了");

  default  -> System.out.println("赢了");

  }

 }

}


  • 1

Reply