Java实现人机猜拳游戏

所属分类: 软件编程 / java 阅读数: 67
收藏 0 赞 0 分享

本文实例为大家分享了Java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下

实现:

User类

public class User 
{
 private String name;
 private int score=0;
 private int num;
 public String GetName()
 {
 return this.name;
 }
 public void SetName(String name)
 {
 this.name=name;
 }
 public int GetScore()
 {
 return this.score;
 }
 public void SetScore(int score)
 {
 this.score+=score;
 }
 
}

Computer类

public class Computer 
{
 private String name;
 private int score=0;
 private int num;
 public String GetName()
 {
 return this.name;
 }
 public void SetName(String name)
 {
 this.name=name;
 }
 
 public int RandNums()
 {
 int n;
 n=(int)(Math.random()*3)+1;// 返回1到3的随机整数。 
 return n;
 }
 public int GetScore()
 {
 return this.score;
 }
 public void SetScore(int score)
 {
 this.score+=score;
 }
}

Gamemanager类

import java.util.Scanner;
 
public class GameManager 
{
 
 
 
 public static void main(String[] args) 
 {
 Scanner input=new Scanner(System.in);//创建一个键盘扫描类对象
 User user=new User();
 Computer computer=new Computer();
 int vsNums=0;
 System.out.println("出拳游戏规则:1、剪刀,2、石头,3、布");
 System.out.println("请选择对方角色(1、刘备,2、孙权,3、曹操)");
 int n=input.nextInt(); //输入整型
 switch(n)
 {
 case 1:
 computer.SetName("刘备");
 break;
 case 2:
 computer.SetName("孙权");
 break;
 case 3:
 computer.SetName("曹操");
 break;
 }
 System.out.println("请输入你的姓名");
 String name=input.next(); //输入字符串型
 user.SetName(name);
 System.out.println(user.GetName()+" "+"VS"+" "+computer.GetName());
 
 String flag="y";
 while(flag.equals(flag))
 {
 System.out.println("要开始吗y/n");
 String yOrn=input.next(); //输入字符串型
 if(yOrn.equals("y"))
 {
 vsNums++;
 System.out.println("请出拳:1、剪刀,2、石头,3、布(输入数字)");
 int nums=input.nextInt(); //输入整型
 switch(nums)
 {
 case 1:
  System.out.println("你出拳:"+"剪刀");
  break;
 case 2:
  System.out.println("你出拳:"+"石头");
  break;
 case 3:
  System.out.println("你出拳:"+"布");
  break;
 }
 int rand=computer.RandNums();
 switch(rand)
 {
 case 1:
  System.out.println(computer.GetName()+"出拳:"+"剪刀");
  break;
 case 2:
  System.out.println(computer.GetName()+"出拳:"+"石头");
  break;
 case 3:
  System.out.println(computer.GetName()+"出拳:"+"布");
  break;
 }
 if(nums==1 && rand==3 || nums==2 && rand==1 || nums==3 && rand==2)
 {
  System.out.println("恭喜,你赢了");
  user.SetScore(1);
 }
 else if(nums==rand)
 {
  System.out.println("平手了");
 }
 else
 {
  System.out.println("很遗憾,你输了");
  computer.SetScore(1);
 }
 
 }
 else
 {
 System.out.println(computer.GetName()+" "+"VS"+" "+user.GetName());
 System.out.println("对战次数:"+vsNums);
 System.out.println("姓名\t得分");
 System.out.println(user.GetName()+"\t"+user.GetScore());
 System.out.println(computer.GetName()+"\t"+computer.GetScore());
 if(user.GetScore()>computer.GetScore())
 {
  System.out.println("恭喜,恭喜");
 }
 else
 {
  System.out.println("继续加油");
 }
 break;
 }
 }
 
 
 }
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多