Java实现猜拳游戏的核心在于电脑随机数的生成,Java中的随机数生成方法是:
首先引入包 import java.util.*; 然后 int r=new Random().nextInt(3); (nextInt中的数字三代表随机数生成的个数,从零开始)
所以在猜拳的输入中需要有0、1、2三个数字代替,如果要输入汉字,则用if进行相应判断即可。
在实现的游戏中实现①猜拳;②记录胜负;③玩家决定游戏局数;④输出获胜、失败及平局;⑤统计总共的胜负结果(根据获胜次数判断)
①猜拳基础功能:该部分代码可以放到一个方法中,减少主函数代码量。
电脑出拳即 int r=new Random().nextInt(3); 注意:该部分一定要写在for循环内部,否则无法实现每次不同的随机数。
通过if判断双方出拳是否相等 if(a==0&&r==0) else if(a==0&&r==1) else if(a==0&&r==2) 即可实现猜拳,if内直接输出相关语句即可
②记录胜负: 定义猜拳方法为int ,通过返回值记录相关比赛的胜负情况 ,可以用0--失败;1--获胜;2--平局 进行记录,在主函数中对相应抛出的数字记录即可
if(a==0&&r==0){③玩家决定局数: 定义一个数,在循环中不大于该数即可 ④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k。 ⑤统计结果,直接用if比较i、j的数字结果即可。 一下为代码 主函数部分:
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}
h=comp.compare(a,r); if (h==1) j++;
1 import java.util.*; 2 3 public class Main { 4 public static void main(String args[]){ 5 Scanner scanner=new Scanner(System.in); 6 Compare comp=new Compare(); 7 int h=0,j=0,k=0; 8 System.out.println("rules:0--cloth;1--stone;2--scissors.\nU can choose how many times you want to play:"); 9 int n=scanner.nextInt(); 10 for(int i=1;i<=n;i++){ 11 System.out.print("It's the "+i+" round,your turn:"); 12 int a=scanner.nextInt(); 13 int r=new Random().nextInt(3); 14 switch (a){ 15 case 0: 16 h=comp.compare(a,r); 17 break; 18 case 1: 19 h=comp.compare(a,r); 20 break; 21 case 2: 22 h=comp.compare(a,r); 23 break; 24 default: 25 System.out.println("Wrong number!"); 26 break; 27 } 28 if (h==1) 29 j++; 30 else if(h==0) 31 k++; 32 33 } 34 System.out.println("The total times you won are "+j+",The draw times are "+(n-j-k)+"."); 35 if(j>k) 36 System.out.println("You are the final winner"); 37 else if(k>j) 38 System.out.println("The computer is the winner."); 39 if(j==k) 40 System.out.println("The final result is draw"); 41 } 42 }
compare 方法部分:
1 package SS2_5; 2 3 public class Compare { 4 public int compare(int a,int r){ 5 int counter=0; 6 if(a==0&&r==0){ 7 System.out.println("The computer comes out with cloth,it was a draw. "); 8 return 2; 9 } 10 else if(a==0&&r==1){ 11 System.out.println("The computer comes out with stone, you won. "); 12 return 1; 13 } 14 else if(a==0&&r==2){ 15 System.out.println("The computer comes out with scissor,you lost. "); 16 return 0; 17 } 18 else if(a==1&&r==0){ 19 System.out.println("The computer comes out with cloth,you lost. "); 20 return 0; 21 } 22 else if(a==1&&r==1){ 23 System.out.println("The computer comes out with stone,it was a draw. "); 24 return 2; 25 } 26 else if(a==1&&r==2){ 27 System.out.println("The computer comes out with scissors,you won. "); 28 return 1; 29 } 30 else if(a==2&&r==0){ 31 System.out.println("The computer comes out with cloth,you won. "); 32 return 1; 33 } 34 else if(a==2&&r==1){ 35 System.out.println("The computer comes out with stone,you lost. "); 36 return 0; 37 } 38 else if(a==2&&r==2){ 39 System.out.println("The computer comes out with scissors,it was a draw. "); 40 return 2; 41 } 42 else 43 return 0; 44 } 45 }
结果示例:
1 rules:0--cloth;1--stone;2--scissors. 2 U can choose how many times you want to play: 3 4 4 It's the 1 round,your turn:1 5 The computer comes out with stone,it was a draw. 6 It's the 2 round,your turn:0 7 The computer comes out with stone, you won. 8 It's the 3 round,your turn:2 9 The computer comes out with scissors,it was a draw. 10 It's the 4 round,your turn:0 11 The computer comes out with stone, you won. 12 The total times you won are 2,The draw times are 2. 13 You are the final winner
标签:Java,猜拳,System,小游戏,&&,println,computer,comes,out From: https://www.cnblogs.com/bailichangchuan/p/16708759.html