Java(石头剪刀布游戏)

package class_experiment_questions;

import java.util.*;

public class rock_paper_scissors {
	
	static int wins = 0;
	static int ties = 0;
	static int losses = 0;
	
	public static void main(String[] args) 
	{
		
		System.out.println("This programe plays game of rock_paper_scissors against the computer."
				+ "You'll type in your guess of 1(rock),2(paper)or3(scissors) and try to beat the computer as many times as you can.");
		Scanner in = new Scanner(System.in);
		System.out.print("Best out of how many games(must be odd)?");
		System.out.println();
		int times = in.nextInt();
		game_part(times);
		double win_percent = (double)wins/times*100;
		System.out.println("Overall results:\n"+"total game results = "+times+"\n"
							+"wins               = "+wins+"\n"
							+"losses             = "+losses+"\n"
							+"ties               = "+ties+"\n"
							+"win %              = "+win_percent+"\n");		
	}
	
	public static void game_part(int times)
	{
		
		for(int i = 1;i <= times;i++) 
		{
			System.out.println("game "+i+":");
			Scanner in = new Scanner(System.in);
			System.out.print("Choose your weapon?");
			System.out.println();
			int ueser_guess = in.nextInt();
			Random weapon =new Random();
			int computer_guess = weapon.nextInt(3);
			if(ueser_guess==1) 
			{
				if (computer_guess==1) 
				{
					System.out.println("Tie!");
					ties += 1;
				}
				else if (computer_guess == 2) 
				{
					System.out.println("You lose!");
					losses += 1;
				}
				else if (computer_guess == 3)
				{
					System.out.println("You win!");
					wins += 1;
				}
			
			}
			else if(ueser_guess==2) 
			{
				System.out.println("I choose the weapon:"+computer_guess);
				if (computer_guess==2) 
				{
					System.out.println("Tie!");
					ties += 1;
				}
				else if (computer_guess == 3) 
				{
					System.out.println("You lose!");
					losses += 1;
				}
				else if (computer_guess == 1)
				{
					System.out.println("You win!");
					wins += 1;
				}
			
			}
			else if(ueser_guess==3) 
			{
				if (computer_guess==3) 
				{
					System.out.println("Tie!");
					ties += 1;
				}
				else if (computer_guess == 1) 
				{
					System.out.println("You lose!");
					losses += 1;
				}
				else if (computer_guess == 2)
				{
					System.out.println("You win!");
					wins += 1;
				}
				else 
				{
					System.out.println("Please enter a valid input!");
				}
			
			}
		}
	
	}
	
}

运行结果:

Java(石头剪刀布游戏)_第1张图片

 

你可能感兴趣的:(java,eclipse)