ユーザーとコンピュータのいくつかの簡単なインタラクティブゲーム(数字を当てて、単語を当てて、はさみの石の布…)

8932 ワード

1)コンピュータは1~100の乱数を生成して、ユーザーは推測して、提示が大きいかそれとも小さいかがあります。ユーザは何回かプレイしてもいいです。終了時に彼の推測の総回数を印刷して、一つの数字の平均回数を当てます。
package userGuessInteger2;

import java.util.Random;
import java.util.Scanner;

public class userGuessInteger2 {
	
	public static void main(String[] args) {
		Random r = new Random(); 
		
		Scanner console = new Scanner(System.in); 
		System.out.print("input the times you want to guess:");
		int times = console.nextInt();
		int count = 0;
		
		int[] numguess = new int[times];
		while(count!=times)
		{
			
			int number= r.nextInt(100)+1;
			System.out.print("input an integer between 1~100: ");
			int guess = console.nextInt();
			numguess[count] = 1;//      
			while(guess != number)
			{
				if(guess > number)
					System.out.println("incorrect, bigger than the given number,guess again: ");
				else
					System.out.println("incorrect,smaller than the given number,guess again: ");					
				
		      	numguess[count ]++;
			    guess = console.nextInt();	
		    }
			System.out.println("guess it!. " + numguess[count ]+ " tries.");
			count++;
		}
		int sum =0,average=0;
		for(int i=0;i
運転結果は類似しています。
input the times you want to gess:2 input an integer between 1~100:50 incorect、smaler than the given number、gess again:  75 incorect、biggar than the given number、gess again:  60 incorect、biggar than the given number、gess again:  55 incorect、smaler than the given number、gess again:  57 gess it5 tries.input an integer between 1~100:50 incorect、smaler than the given number、gess again:  75 incorect、smaler than the given number、gess again:  85 incorect、smaler than the given number、gess again:  90 incorect、smaler than the given number、gess again:  95 incorect、smaler than the given number、gess again:  99 incorect、bigger than the given number、gess again:  96 gess it7 tries.the times you guss is:12、and the average timeyou guss rightis:6
2)ユーザーは1~10の中から数字を選んで、コンピュータは推測して、ユーザーはコンピュータが大きいかそれとも小さいかをヒントにします。プログラムは最終的に的中した数字と推測の回数を印刷します。
package computerGuessInteger;

import java.util.Random;
import java.util.Scanner;

public class ComputerGuessInteger {

	public static void main(String[] args) {
		giveIntro();
		
		Scanner console = new Scanner(System.in); 
		System.out.print("input an integer between 1 and 10: ");
		int number= console.nextInt();
		
		int numguess = 1;//       
		int guesslast1 = 1;
		int guesslast2 =10;
		
		int guess = getguess(guesslast1,guesslast2);
		while(guess!= number)
		{
			System.out.println("Is it " + guess +"? (y/n) n");
			numguess++;
			if(guess > number)
			{
				System.out.println("bigger then given number.");
				guesslast2 = guess-1;
				guess = getguess(guesslast1,guesslast2);
			}
			else
			{
				System.out.println("smaller then given number.");
				guesslast1 = guess + 1;
				guess = getguess(guesslast1,guesslast2);
			}		
		}
		
		System.out.println("Is it " + guess +"? (y/n) y" );
		System.out.println();
		System.out.println("I got your number of " + number + " correct in " + numguess + " guesses");
		
	}
	
	public static void 	giveIntro()
	{

		System.out.println("this program has you ,the user,choose a number");
		System.out.println("between 1 and 10,then I,the computer,will try");
		System.out.println("my best to guess it.");
		System.out.println();
	}
	public static int getguess(int guesslast1,int guesslast2)
	{
		Random r= new Random();
		return r.nextInt(Math.abs(guesslast1-guesslast2)+1)+ Math.min(guesslast1,guesslast2);
	}
}
運転結果は類似しています。
this program has you,the user,chose a number between 1 and 10,then I,the computer,will try my best to gess it.input an integer between 1 and 10:4 Is it 10?(y/n)n biggar then given number.Is it 7?(y/n)n biggar then given number.Is it 4?(y/n)y I got your number of 4 corect in 3 gess
3)単語を当てるゲーム。ユーザーは単語をよく考えて、コンピューターで推測します。ユーザーはコンピューター単語にいくつかの文字が含まれていると教えてくれます。
package computerGuessWord2;

import java.util.Random;
import java.util.Scanner;

public class ComputerGuessWord {

    public static final String S ="abcdefghijklmnopqrstuvwxyz";
	public static void main(String[] args) {
		
		Scanner console = new Scanner(System.in); 
		System.out.print("input the length of the word: ");
		int length= console.nextInt();
		System.out.print("input the word: ");
		String word= console.next();
		
		String guess = "";//           
		String guess0 = getGuessWord(length,guess);//          。
		
		//                   。
		
		int numguess = 1;//       
		while(!guess0.equals(word))
		{
			System.out.println("incorrect,guess again: ");
			if(guess0.charAt(0)==word.charAt(0))
			{
				guess += guess0.charAt(0);
				word=word.substring(1,length);
		    	length -= 1;				
			}				
			guess0 = getGuessWord(length,guess);	
			numguess++;
		}
		System.out.println("guess it!. " + numguess+ " tries.");


	}

	
	//               。
		public static String getGuessWord(int length,String guess)
		{
		  System.out.print("you guess: ");
		  
		  String guessword = "" ;
		  Random r =new Random();
		  for(int i = 0;i 
運転結果は類似しています。
input the length of the word:2 input the word:ac you gess:ie incorect,gess again:  gusess:fm incorect,gess again:  あなたgusess:ss incorect,gess again:  あなたgusess:r incorect,gess again:  you guss:az incorect,gess again:  あなたgusess:aw incorect,gess again:  you guss:at incorect,gess again:  あなたgusess:ay incorect,gess again:  あなたgusess:aw incorect,gess again:  you Gress:aj incorect,gess again:  あなたgusess:ah incorect,gess again:  you guss:as incorect,gess again:  あなたguss:ap incorect,gess again:  あなたgusess:ar incorect,gess again:  あなたgusess:aq incorect,gess again:  you guss:ab incorect,gess again:  you Gress:am incorect,gess again:  あなたguss:al incorect,gess again:  あなたgusess:ah incorect,gess again:  あなたguss:ac gusess it!20 tries.
4)複数回のジャンケンじゃんけんゲーム。ユーザーのパソコンはそれぞれ一つのものを取ります。ルールはいつもと同じです。最後にゲームの結果を印刷します。
package gamesStoneScissorsCloth;

import java.util.Random;
import java.util.Scanner;

public class gamesStoneScissorsCloth {

    public static final String[] ITEMS = {"scissors","stone","cloth"};
	public static void main(String[] args) {
		
		Scanner console = new Scanner(System.in); 
		System.out.print("input the times you want to play: ");
		int times= console.nextInt();	
		gamesStoneScissorsCloth(console,times);
	}
	
	public static void 	gamesStoneScissorsCloth(Scanner console,int times)
	{
		Random r= new Random();
		int computer=0;
		int user= 0;
		int countuser=0,countcomputer = 0,countdraw=0;
		while(times!=0)
		{
	        //               ,     
			computer = r.nextInt(3);
			System.out.print("input the label of the items you want to get,for " +
					"scissors as 0,stone as 1,cloth as 2: ");
			user= console.nextInt();
			System.out.print("user: " + ITEMS[user] + ". computer: " + ITEMS[computer]);
			//      ,   。
			if(computer == user)
			{
				countdraw++;
				System.out.println(" draw");
			}				
			else if((computer == 0&&user==2)||(computer==1&&user==0)||(computer==2&&user==1))
			{
				countcomputer++;
				System.out.println(" computer win");
			}			
			else
			{
				countuser++;	
				System.out.println(" user win");
			}		
			times--;
		}
		
		judgeResult(countuser,countcomputer,countdraw);
	}
	
	//     。
	public static void judgeResult(int countuser,int countcomputer,int countdraw)
	{
		System.out.println();
		System.out.println("the result is:");
		System.out.println("user win " + countuser + " times");
		System.out.println("computer win " + countcomputer + " times");
		System.out.println("draw " + countdraw + " times");
		if(countuser > countcomputer)
			System.out.println("user win!");
		else if(countuser < countcomputer)
			System.out.println("computer win!");
		else
			System.out.println("draw!");
	}
}
運転結果は類似しています。
3 input the times You want to Play:3 input the label of the item you wantto get、for scissors as 0、stone as 1、cloth as 2:0 user:scissors.coputter:cloth user win inputthe label the label of the the emitststststststsyou fors s s s s s s s s s s s s s s s s s s s s s StStStStStattttttttfofofofofofont、shshshshshshshshshshshsh、Stats、Stats、Stast、Statttttttlabel of the items you want to get,for scissors as 0,stone as 1,cloth as 2:2 user:cloth.computer:stone user win the relt is:user win 3 times computer win 0 times draw 0 times user win!