import java.util.Scanner; import java.util.Random; public class GuessNumber{ public static void main(String args[]){ Scanner reader = new Scanner(System.in); Random random=new Random(); System....
import java.util.Scanner;
import java.util.Random;
public class GuessNumber{
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
Random random=new Random();
System.out.println("给你一个1至100之间的整数,请猜测这个数:");
int realNumber=random.nextInt(100)+1;//random.nextInt(100)是[0,100)中的随机整数
int yourGuess=0;
System.out.print("请输入你的猜测:");
yourGuess=reader.nextInt();
while(yourGuess != realNumber)//循环条件
{
if(yourGuess>realNumber)//猜大了的条件代码
{
System.out.print("猜大了,再输入你的猜测:");
yourGuess=reader.nextInt();
}
else if (yourGuess<realNumber)//猜小了的条件代码
{
System.out.print("猜小了,再输入你的代码:");
yourGuess=reader.nextInt();
}
}
System.out.print("猜对了!");
}
}
import java.util.Scanner;
import java.util.Random;
public class GuessNumberS{
public static void main(String args[]){
Scanner reader = new Scanner(System.in);
Random random=new Random();
System.out.println("给你一个1至100之间的整数,请猜测这个数:[1,100]");
int realNumber=random.nextInt(100)+1;//random.nextInt(100)是[0,100)中的随机整数
int yourGuess=0,count=0;
int high=100,low=1;
System.out.print("请输入你的猜测:");
yourGuess=reader.nextInt();
count++;
while(yourGuess != realNumber)//循环条件
{
if(yourGuess>realNumber)//猜大了的条件代码
{
high=yourGuess;
System.out.println("猜大了,再输入你的猜测["+low+","+high+"]");
yourGuess=reader.nextInt();
count++;
}
else if (yourGuess<realNumber)//猜小了的条件代码
{
low=yourGuess;
System.out.println("猜小了,再输入你的猜测["+low+","+high+"]");
yourGuess=reader.nextInt();
count++;
}
}
switch(count){
case 1: System.out.print("你一下就猜到了,你一定是天选之人!"); break;
}
if(count>1 && count<=5){
System.out.print("你只猜了"+count+"次就猜对啦!!!你的运气太好啦!");
}else if(count>5 && count<=8){
System.out.print("你一共猜了"+count+"次!!也很棒呦~");
}else if(count>8 && count<=10000){
System.out.print("你一共猜了"+count+"次!");
}
}
}
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com