Please read the following precautions carefully. When the game starts, you are required to enter the player's name, and then the system will send a hero (name yourself) out of the station. Players enter 1 (rock), 2 (scissors), and 3 (paper) through the console to fight against system heroes. When the player punches, the system hero punches randomly.
According to the rules of the "Rock, Paper, Scissors" game, the winner gets 1 point. At the end of the game, the game results will be displayed! If the player wins, he will be rewarded with (number of wins*2) honor points! ! ! ! If you agree to the above rules, please enter 1 in the console and enter other numbers to end! ! !
import java.util.Random; import java.util.Scanner; public class Game{ public static void main(String[] args){ System.out.println("欢迎来到石头剪刀布游戏世界!"); System.out.println("请认真阅读以下注意事项:"); System.out.println("* 游戏开始,要求输入玩家名字"); System.out.println("* 随后系统将派出一名英雄(名字自取)出站。"); System.out.println("* 玩家通过控制台输入 1(石头)、2(剪刀)、3(布) 与系统英雄对战。"); System.out.println("* 玩家出拳的同时,系统英雄随即随机出拳。"); System.out.println("* 根据《石头剪刀布游戏》规则,胜利者得1分。"); System.out.println("* 最终比赛结束会展示比赛战绩!"); System.out.println("* 如果玩家胜利,则奖励(胜利局数*2) 荣誉值!"); System.out.println("* !!!同意以上规则请在控制台输入 1,输入其他数字结束!!!"); Scanner scanner=new Scanner(System.in); int agree=scanner.nextInt();//用户是否同意规则 Random random=new Random(); String pcName="电脑";//电脑名称 int userNum;//用户输入的数字 int pcNum;//电脑输出的数字 int a=0;//记录用户赢的次数 int b=0;//记录电脑赢的次数 int c=0;//记录平局次数 int flag=1;//用户不同意规则 或者 用户退出使用 int flagError=1;//定义用户输入错误时使用 //用户同意规则 if(agree == 1){ System.out.println("请输入玩家名字: "); String userName=scanner.next(); //for循环 for(;;){ if(flag ==1){ System.out.println("系统玩家: "+pcName+" 出战"); System.out.println(userName+" 请出拳:1(石头)、2(剪刀)、3(布) (请输入相应的数字,输入0结束比赛)"); //接收用户出拳 userNum=scanner.nextInt(); //电脑出拳 pcNum=random.nextInt(2)+1; //判断用户出拳 switch(userNum){ case 1: System.out.println(userName+" 出拳:石头"); break; case 2: System.out.println(userName+" 出拳:剪刀"); break; case 3: System.out.println(userName+" 出拳:布"); break; case 0: System.out.println("退出成功,查看结果"); flag=0; break; default: System.out.println("输入错误,重新输入"); flagError=0; break; } //用户退出 if(flag ==0){ break;//终止循环 } //用户输入错误,重新输入 if(flagError ==0){ //程序给其赋值开始下一次循环 flagError=1; continue;//只是终止本次循环,继续进行下一次循环 } //判断电脑出拳 switch(pcNum){ case 1: System.out.println(pcName+" 出拳:石头"); break; case 2: System.out.println(pcName+" 出拳:剪刀"); break; case 3: System.out.println(pcName+" 出拳:布"); break; default: break; } //判断谁赢 if((pcNum-userNum) > 0){ System.out.println(userName+" 赢了"); flag=1; a++; }else if((pcNum-userNum) ==0){ System.out.println("平局"); flag=1; c++; }else{ System.out.println(pcName+" 赢了"); flag=1; b++; } }else{ flag=0; break; } } System.out.println("*******************"); System.out.println(pcName+" 胜利 "+b+" 局"); System.out.println(userName+" 胜利 "+a+" 局"); System.out.println("平局:"+c+" 局"); if(a==b){ System.out.println("最终结果:打平手!"); }else if(a > b){ System.out.println("最终结果:"+userName+" 赢了"); }else{ System.out.println("最终结果:"+pcName+" 赢了"); } System.out.println("*******************"); }else{ System.out.println("程序结束"); } } }
Result screenshot:
The above is the detailed content of java guessing game. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools