Java控制台实现猜拳游戏小游戏
作者:Samoy 时间:2022-04-20 09:52:55
本文实例为大家分享了Java猜拳游戏的具体代码,供大家参考,具体内容如下
先来看一下效果图:
首先我们创建一个Person类,这个类有name和score两个属性,有play这个方法,源代码如下:
package com.samoy;
public class Person {
public String name;
public int score;
public Person(String name, int score) {
this.name = name;
this.score = score;
}
public String play(int i) {
return i == 1?"剪刀":(i == 2?"石头":"布");
}
}
接下来是主程序入口:
package com.samoy;
import java.util.Random;
import java.util.Scanner;
public class JavaTest {
public static void main(String[] args) {
int num = 0;
System.out.println("*****************\n****猜拳, 开始****\n*****************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.println("请选择对方角色<1:刘备 2:孙权 3:曹操>");
Scanner scanner = new Scanner(System.in);
//new一个对手
Person opponent = new Person("",0);
int nameIndex = scanner.nextInt();
while (true){
if (nameIndex == 1){
opponent.name = "刘备";
break;
}else if (nameIndex == 2){
opponent.name = "孙权";
break;
}else if (nameIndex == 3){
opponent.name = "曹操";
break;
}else {
System.out.println("输入错误!输入对方角色<1:刘备 2:孙权 3:曹操>");
nameIndex = scanner.nextInt();
}
}
System.out.println("请输入您的姓名:");
Scanner scanner1 = new Scanner(System.in);
Person self = new Person(scanner1.next(),0);
System.out.println(self.name + " VS " +opponent.name);
System.out.println("要开始游戏吗?<y/n>");
Scanner scanner2 = new Scanner(System.in);
String option = scanner2.next();
while (option.equals("y")){
System.out.println("请出拳:1、剪刀 2、石头 3、布");
Scanner scanner3 = new Scanner(System.in);
int select = scanner3.nextInt();
String result1;
if (select!=1&&select!=2&&select!=3){
System.out.println("输入错误!请输入出拳规则:1、剪刀 2、石头 3、布");
select = scanner3.nextInt();
}
result1 = self.play(select);
System.out.println(self.name+"出:"+result1);
String result2 = opponent.play(new Random().nextInt(3)+1);
System.out.println(opponent.name+"出:"+result2);
if (result1.equals(result2)){
System.out.println("和局");
}else if (result1.equals("剪刀")&&result2.equals("布")||result1.equals("石头")&&result2.equals("剪刀")||result1.equals("布")&&result2.equals("石头")){
System.out.println("你赢了! "+self.name+"赢!");
self.score += 1;
}else {
System.out.println("你输了! "+opponent.name+"赢!");
opponent.score += 1;
}
num++;
System.out.println("需要进行下一局吗?<y/n>");
option = scanner2.next();
if (option.equals("n")){
System.out.println("=================");
System.out.println("共进行"+num+"局\n"+self.name+"得分为"+self.score+","+opponent.name+"得分为"+opponent.score +"\n");
if (self.score>opponent.score){
System.out.println(self.name+"赢!"+self.name+"好棒!");
}else if (self.score == opponent.score){
System.out.println("平局!");
}else {
System.out.println(opponent.name+"赢!"+self.name+"笨蛋!");
}
System.out.println("感谢你的使用!");
}else if (!option.equals("y")){
System.out.println("输入错误,请重新输入!");
System.out.println("需要进行下一局吗?<y/n>");
option = scanner2.next();
}
}
}
}
源代码下载:Java猜拳游戏
来源:https://blog.csdn.net/samoy/article/details/51741353
标签:java,猜拳游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA+Struts2获取服务器地址的方法
2022-01-26 22:25:11
JDBC自定义连接池过程详解
2023-11-17 13:27:55
Mybatis关联查询结果集对象嵌套的具体使用
2021-07-12 22:09:18
.NET C#利用ZXing生成、识别二维码/条形码
2022-03-25 12:20:45
java集合与数组的相同点和不同点
2022-07-19 03:13:53
TextView实现跑马灯效果 就这么简单!
2023-06-25 18:42:24
![](https://img.aspxhome.com/file/2023/2/83542_0s.png)
Spring深入探索AOP切面编程
2023-05-27 09:37:16
![](https://img.aspxhome.com/file/2023/6/78056_0s.png)
Spring Cloud Gateway(读取、修改 Request Body)的操作
2023-11-09 19:25:46
![](https://img.aspxhome.com/file/2023/0/59240_0s.jpg)
Spring Boot修改启动端口的方法
2022-02-10 05:49:55
Java swing实现酒店管理系统
2021-08-08 02:24:12
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)
SpringSecurity报错authenticationManager must be spec的解决
2021-07-27 21:52:27
![](https://img.aspxhome.com/file/2023/4/60814_0s.png)
Java实战之王者荣耀的英雄是怎么产生的?
2021-08-25 12:35:49
![](https://img.aspxhome.com/file/2023/1/66531_0s.png)
Java使用DualPivotQuicksort排序
2022-05-22 20:58:12
Java中常用的设计模式之观察者模式详解
2023-07-27 12:32:52
![](https://img.aspxhome.com/file/2023/0/75450_0s.png)
java中文传值乱码问题的解决方法
2023-11-25 16:26:47
java实现简单登录界面的实战过程
2022-02-07 20:19:51
![](https://img.aspxhome.com/file/2023/2/67152_0s.jpg)
C#使用GET、POST请求获取结果
2023-04-20 13:35:43
Java实战项目 医院预约挂号系统
2023-09-18 06:56:44
![](https://img.aspxhome.com/file/2023/2/72632_0s.png)
Android自定义View之组合控件实现类似电商app顶部栏
2023-07-26 22:14:10
![](https://img.aspxhome.com/file/2023/3/84713_0s.png)