java用户猜0-100内的随机数,应具备以下3个功能。
1。根据输入内容输入提示:太大、太小、或者答对了;游戏一轮结束时,用户输入“y”能开始下一轮游戏。2。输入0能随时退出游戏;3。输入非数字时,能处理抛出的异常,并继续游戏...
1。根据输入内容输入提示:太大、太小、或者答对了; 游戏一轮结束时,用户输入“y”能开始下一轮游戏。 2。输入0能随时退出游戏;3。 输入非数字时,能处理抛出的异常,并继续游戏。
展开
3个回答
2013-08-13
展开全部
import java.util.Scanner;public class topic5
{
public static void main(String args[])
{
topic5 t5 = new topic5();
t5.guessNum(0,100);
}
public void guessNum(int minN,int maxN)
{
int minNum = minN; //所猜数字的下限
int maxNum = maxN; //所猜数字的上限
int guessTimes = 10; //剩余猜测机会
int randomNum = (int)(Math.random()*(maxNum-minNum))+minNum; //生成随机数
Scanner s = new Scanner(System.in);
while(true) //死循环
{
if(guessTimes==0) //机会用完退出
{
System.out.println("很遗憾,10次机会已经用完,您没有猜出正确的数字。\n正确的数字是:"+randomNum);
System.out.println("\n若要重新游戏,请输入Y。否则输入任意键退出...");
Object restart = s.nextLine();
if(restart.equals("Y")||restart.equals("y"))
{
minNum = minN;
maxNum = maxN;
guessTimes = 10;
randomNum = (int)(Math.random()*(maxNum-minNum))+minNum;
}
else break;
}
System.out.print("猜数字游戏("+minNum+" - "+maxNum+")您还有"+guessTimes+"次机会\n请输入一个您要猜的数字:");
String input = s.nextLine(); //取得输入值
//输入检测
char[] inArr = input.toCharArray();
for(int i=0;i<inArr.length;i++)
{
if(!Character.isDigit(inArr[i]))
{
System.out.print("输入有误,请重新输入:");
input = s.nextLine(); //取得输入值
inArr = input.toCharArray();
i=0;
}
}
int in = Integer.valueOf(input);
if(in==0) break; //输入0退出
else if(in==randomNum) //猜中后退出
{
System.out.println("\n恭喜您!猜对了!答案就是:"+in);
System.out.println("\n若要重新游戏,请输入Y。否则输入任意键退出...");
Object restart = s.next();
if(restart.equals("Y")||restart.equals("y"))
{
minNum = minN;
maxNum = maxN;
guessTimes = 10;
randomNum = (int)(Math.random()*(maxNum-minNum))+minNum;
}
else break;
}
else if(in>randomNum) //输入数过大
{
System.out.println("\n您猜的数字过大。\n");
if(in<maxNum) maxNum = in;
}
else if(in<randomNum) //输入数过小
{
System.out.println("\n您猜的数字过小。\n");
if(in>minNum) minNum = in;
}
guessTimes--; //剩余猜测机会自减
}
}
}
{
public static void main(String args[])
{
topic5 t5 = new topic5();
t5.guessNum(0,100);
}
public void guessNum(int minN,int maxN)
{
int minNum = minN; //所猜数字的下限
int maxNum = maxN; //所猜数字的上限
int guessTimes = 10; //剩余猜测机会
int randomNum = (int)(Math.random()*(maxNum-minNum))+minNum; //生成随机数
Scanner s = new Scanner(System.in);
while(true) //死循环
{
if(guessTimes==0) //机会用完退出
{
System.out.println("很遗憾,10次机会已经用完,您没有猜出正确的数字。\n正确的数字是:"+randomNum);
System.out.println("\n若要重新游戏,请输入Y。否则输入任意键退出...");
Object restart = s.nextLine();
if(restart.equals("Y")||restart.equals("y"))
{
minNum = minN;
maxNum = maxN;
guessTimes = 10;
randomNum = (int)(Math.random()*(maxNum-minNum))+minNum;
}
else break;
}
System.out.print("猜数字游戏("+minNum+" - "+maxNum+")您还有"+guessTimes+"次机会\n请输入一个您要猜的数字:");
String input = s.nextLine(); //取得输入值
//输入检测
char[] inArr = input.toCharArray();
for(int i=0;i<inArr.length;i++)
{
if(!Character.isDigit(inArr[i]))
{
System.out.print("输入有误,请重新输入:");
input = s.nextLine(); //取得输入值
inArr = input.toCharArray();
i=0;
}
}
int in = Integer.valueOf(input);
if(in==0) break; //输入0退出
else if(in==randomNum) //猜中后退出
{
System.out.println("\n恭喜您!猜对了!答案就是:"+in);
System.out.println("\n若要重新游戏,请输入Y。否则输入任意键退出...");
Object restart = s.next();
if(restart.equals("Y")||restart.equals("y"))
{
minNum = minN;
maxNum = maxN;
guessTimes = 10;
randomNum = (int)(Math.random()*(maxNum-minNum))+minNum;
}
else break;
}
else if(in>randomNum) //输入数过大
{
System.out.println("\n您猜的数字过大。\n");
if(in<maxNum) maxNum = in;
}
else if(in<randomNum) //输入数过小
{
System.out.println("\n您猜的数字过小。\n");
if(in>minNum) minNum = in;
}
guessTimes--; //剩余猜测机会自减
}
}
}
2013-08-13
展开全部
public static strictfp void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("猜数字游戏现在开始");
int s = (int)(Math.random()*100+1);
int in = 0;
while(true){
String rl = br.readLine();
if(rl.equals("y")){
s = (int)(Math.random()*101);
System.out.println("猜数字游戏重新开始");
}
try{
in = Integer.parseInt(rl);
if(in == 0)
break;
if(in > s){
System.out.println("大了");
}else if(in < s){
System.out.println("小了");
} else{
System.out.println("恭喜你答对了");
s = (int)(Math.random()*101);
System.out.println("猜数字游戏重新开始");
}
}catch(NumberFormatException e){
System.out.println("请输入数字或者字母y");
}
}
br.close();
}
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("猜数字游戏现在开始");
int s = (int)(Math.random()*100+1);
int in = 0;
while(true){
String rl = br.readLine();
if(rl.equals("y")){
s = (int)(Math.random()*101);
System.out.println("猜数字游戏重新开始");
}
try{
in = Integer.parseInt(rl);
if(in == 0)
break;
if(in > s){
System.out.println("大了");
}else if(in < s){
System.out.println("小了");
} else{
System.out.println("恭喜你答对了");
s = (int)(Math.random()*101);
System.out.println("猜数字游戏重新开始");
}
}catch(NumberFormatException e){
System.out.println("请输入数字或者字母y");
}
}
br.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-13
展开全部
太simple 不想做
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |