Java语言程序设计基础篇_第十版_梁勇_3.17游戏:石头剪刀布源码答案 package nameyu; import java.util.Scanner;public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("scissor (0), rock(1), paper(2):"); int numByUser = input.nextInt(); int numByComputer = (int)(Math.random() * 3); if (numByUser == numByComputer){ switch(numByUser) { case 0:System.out.println("The computer is scissor. You are scissor too.It is draw."); break; case 1:System.out.println("The computer is rock. You are rock too.It is draw."); break; case 2:System.out.println("The computer is paper. You are paper too.It is draw."); break; } } else if ((numByUser == 0 && numByComputer == 2) || (numByUser == 1 && numByComputer == 0) || (numByUser == 2 && numByComputer == 1)){ switch(numByUser) { case 0:System.out.println("The computer is paper. You are scissor.You win!"); break; case 1:System.out.println("The computer is scissor. You are rock.You win!"); break; case 2:System.out.println("The computer is rock. You are paper.You win!"); break; } } else if ((numByUser == 0 && numByComputer == 1) || (numByUser == 1 && numByComputer == 2) || (numByUser == 2 && numByComputer == 0) ){ switch(numByUser) { case 0:System.out.println("The computer is rock. You are scissor.You lose!"); break; case 1:System.out.println("The computer is paper. You are rock.You lose!"); break; case 2:System.out.println("The computer is scissor. You are paper.You lose!"); break; } } } }
|