Home >Java >javaTutorial >java guessing game

java guessing game

(*-*)浩
(*-*)浩forward
2019-10-24 15:27:313320browse

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.

java guessing game

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:

java guessing game

The above is the detailed content of java guessing game. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete