Discuss / Java / rock & scissors & paper

rock & scissors & paper

Topic source

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;
  Scanner scanner = new Scanner(System.in);
  int scchoice = scanner.nextInt();
  // 计算机随机数 1, 2, 3:
  int random = (int)(1+ (float)Math.random()*3);
  switch (random) {
  case 1 -> System.out.println("Rock");
  case 2 -> System.out.println("Scissors");
  case 3 -> System.out.println("Paper");
  }
  choice =scchoice - random;
  switch (choice) {
  // TODO:
   case -2:
   case 1:System.out.println("You Lost");
   break;
   case 0:System.out.println("Same");
   break;
   case -1:
   case 2:System.out.println("You Win");
   }
 }

}


  • 1

Reply