我自己改写了个猜数学游戏,它原来是这样的:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class GuessNumber{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println( "请输入一个100以内的整数: ");
for (int i=0;i <=9;i++){
BufferedReader bf=new BufferedReader
(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shu> num)
System.out.println( "输入的数大了,输小点的! ");
else if (shu <num)
System.out.println( "输入的数小了,输大点的! ");
else {
System.out.println( "恭喜你,猜对了! ");
if (i <=2)
System.out.println( "你真是个天才! ");
else if (i <=6)
System.out.println( "还将就,你过关了! ");
else if (i <=8)
System.out.println( "但是你还……真笨! ");
else
System.out.println( "你和猪没有两样了! ");
break;
}
}
}
}
我的改写如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class GueNum{
public static void main(String[] args)
throws IOException{
Random a = new Random();
int num = a.nextInt(100);
System.out.println( "请输入一个100以内的整数 ");
int count=0;
do{
count++;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
int shu = Integer.parseInt(str);
if(shu> num)
System.out.println( "大了,再小点! ");
else if (shu <num)
System.out.println( "小了,再大点! ");
else
{System.out.println( "对了,恭喜! ");
switch (count) {
case 1:
System.out.println( "东渐……快来看上帝…… ");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
System.out.println( "这么快就猜对了,你很smart啊! ");
break;
default:
System.out.println( "猜了半天才猜出来,小同志,尚须努力啊! ");
break;
}}
}//while(shu==num);//从结果看,这样的shu不行,那再怎么改下让判断成立呢?
while(true);//再试下这个
//可猜对后,再怎么才能自动退出呢????
}
}
1,可它不能像上面那个那样正常退出,得自己强制退出,怎么办呢???我主要是想自己练习这个do-while的应用。