用户与计算机的几个简单交互游戏(猜数字、猜单词、剪刀石头布...)

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<count;i++)	
			sum += numguess[i];
		average = sum /count;
		System.out.println("the times you guess is: " + sum + 
				",and the average times you guess right is: " + average);
		
		
	}

}


运行结果类似:

input the times you want to guess:2
input an integer between 1~100: 50
incorrect,smaller than the given number,guess again: 
75
incorrect, bigger than the given number,guess again: 
60
incorrect, bigger than the given number,guess again: 
55
incorrect,smaller than the given number,guess again: 
57
guess it!. 5 tries.
input an integer between 1~100: 50
incorrect,smaller than the given number,guess again: 
75
incorrect,smaller than the given number,guess again: 
85
incorrect,smaller than the given number,guess again: 
90
incorrect,smaller than the given number,guess again: 
95
incorrect,smaller than the given number,guess again: 
99
incorrect, bigger than the given number,guess again: 
96
guess it!. 7 tries.
the times you guess is: 12,and the average times you guess right is: 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,choose a number
between 1 and 10,then I,the computer,will try
my best to guess it.


input an integer between 1 and 10: 4
Is it 10? (y/n) n
bigger then given number.
Is it 7? (y/n) n
bigger then given number.
Is it 4? (y/n) y


I got your number of 4 correct in 3 guesses


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 <length;i++)
			{			
		    	int index = r.nextInt(26);
				guessword += S.charAt(index);//字符串中也能连接字符 啊。
			}
		    System.out.println(guess + guessword);
			return guessword;
		}
}

运行结果类似:

input the length of the word: 2
input the word: ac
you guess: ie
incorrect,guess again: 
you guess: fm
incorrect,guess again: 
you guess: ss
incorrect,guess again: 
you guess: er
incorrect,guess again: 
you guess: az
incorrect,guess again: 
you guess: aw
incorrect,guess again: 
you guess: at
incorrect,guess again: 
you guess: ay
incorrect,guess again: 
you guess: aw
incorrect,guess again: 
you guess: aj
incorrect,guess again: 
you guess: ah
incorrect,guess again: 
you guess: as
incorrect,guess again: 
you guess: ap
incorrect,guess again: 
you guess: ar
incorrect,guess again: 
you guess: aq
incorrect,guess again: 
you guess: ab
incorrect,guess again: 
you guess: am
incorrect,guess again: 
you guess: al
incorrect,guess again: 
you guess: ah
incorrect,guess again: 
you guess: ac
guess 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!");
	}
}

运行结果类似:

input the times you want to play: 3
input the label of the items you want to get,for scissors as 0,stone as 1,cloth as 2: 0
user: scissors. computer: cloth user win
input the label of the items you want to get,for scissors as 0,stone as 1,cloth as 2: 1
user: stone. computer: scissors user win
input the label 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 result is:
user win 3 times
computer win 0 times
draw 0 times
user win!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值