刚学java,模仿tomcat中的例子写了一个猜数字的程序,请多提意见,砸砸鸡蛋哈
lots 2003-10-02 06:09:07 /*
总觉得没用什么java的东西,但又不知道该怎么用,多砸鸡蛋哈
*/
import java.io.*;
import java.util.Random;
public class GuessNO
{
//随机数生成器.
static int randomNO()
{
Random random=new Random();
int x=random.nextInt(100);
return x;
}
//错误输入判断器.
static int judge() throws IOException
{
int no;
do
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("请输入一个在 1~100 之间的整数!");
String text = input.readLine();
no=new Integer(text).intValue();
}
while(no>100||no<0);
return no;
}
//main
public static void main(String[] args) throws IOException
{
int times=0;
int i=1,j;
int[] pass = new int[12];
String[] passed = new String[9];
Random random=new Random();
int tmpNo=randomNO();
// 开关
// System.out.println(tmpNo);
int rightNo=judge();
//判断大小
boolean flag = false;
int flag2=0;
do
{
times++;
if(rightNo>tmpNo)
{
pass[times]=rightNo;
passed[times]="大了";
for(j=1;j<=times;j++)
{
System.out.println("您第"+(j)+"次所猜数为:"+ pass[j] +passed[j]);
}
System.out.println("您第"+times+"次猜大了");
rightNo=judge();
continue;
}
if(rightNo<tmpNo)
{
pass[times]=rightNo;
passed[times]="小了";
for(j=1;j<=times;j++)
{
System.out.println("您第"+(j)+"次所猜数为:"+ pass[j] +passed[j]);
}
System.out.println("您第"+times+"次猜小了");
rightNo=judge();
continue;
}
if(rightNo==tmpNo)
{
System.out.println("恭喜答对了,正确数为"+tmpNo+",累计"+times+"次.");
break;
}
}
while(!flag);
}
}