首页 > 编程语言 >Java实现猜拳小游戏

Java实现猜拳小游戏

时间:2022-09-19 19:34:59浏览次数:69  
标签:Java 猜拳 System 小游戏 && println computer comes out

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){
System.out.println("The computer comes out with cloth,it was a draw. ");
return 2;
}

h=comp.compare(a,r); if (h==1) j++;
  ③玩家决定局数: 定义一个数,在循环中不大于该数即可 ④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k。 ⑤统计结果,直接用if比较i、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

相关文章

  • Java语言(基础一)
    Java语言Java的特性和优势简单性(简单易学)面向对象(一种思想万物皆对象)可移植性(一次编写到处运行JVM)高性能(及时编译)分布式(网络分布式url)动态性(反射机制)多线程(交互......
  • JavaScript高级复习——js面向对象
    面向对象优点:易维护、易复用、易扩展。适合多人合作。缺点:性能比面向过程低。面向过程优点:性能比面向对象高,适合跟硬件联系很紧密的东西,例如单片机。缺点:没有面向对象......
  • 【Java基础】包装类的使用
    目录1.包装类是什么2.为什么要用包装类3.包装类和基本数据类型之间的转换基本数据类型-->包装类包装类-->基本数据类型4.和String类型之间的转换基本数据类型-->Stri......
  • Java实践二
    关于BigInteger:由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多......
  • BigDecimal除法报错Method threw ‘java.lang.ArithmeticException‘ exception.
    今天使用BigDecimal数据类型做除法的时候,遇到以下报错:Methodthrew‘java.lang.ArithmeticException’exception.Non-terminatingdecimalexpansion;noexactreprese......
  • javascript的类的继承
     先看下java的继承,java的继承是发生在类之间,他们的对象之间没有继承关系,子类的对象通过子类创建对象,同时对象中不存储函数只有数据,函数需找到类定义以后读取里面的函数,类......
  • 了解3D世界的黑魔法-纯Java构造一个简单的3D渲染引擎
    简介: 对于非渲染引擎相关工作的开发者来说,可能认为即使构建最简单的3D程序也非常困难,但事实上并非如此,本篇文章将通过简单的200多行的纯Java代码,去实践正交投影、简单三......
  • JavaScript break 和 continue 语句
    break语句用于跳出循环。continue用于跳过循环中的一个迭代。break语句break语句可用于跳出循环。break语句跳出循环后,会继续执行该循环之后的代码continue语句......
  • 数据结构与算法【Java】07---树结构基础部分
    前言数据data结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码。要学习好数据结构就......
  • JavaScript for 循环
    循环可以将代码块执行指定的次数。for循环:如果向多次运行相同代码,且每次的值都是不同,那就使用循环不同类型的循环JavaScript支持不同类型的循环:for -循环代码块一......