Java人机猜拳
程序员文章站
2022-07-09 15:07:16
实现: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.scor......
实现:
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;
}
}
}
}
本文地址:https://blog.csdn.net/weixin_44350205/article/details/107600296
下一篇: UE4 常用快捷键