JAVA猜数字游戏源程序
JAVA猜数字游戏源程序
代码贴在下面,猜大小,还有猜多久出来的
import java.util.Random;
import java.util.Scanner;
public class TestRandom
{
public static void main(String[] args)
{
Random r = new Random();
int i;
i = r.nextInt(10);
System.out.println("请猜10以内的数字");
long a=System.currentTimeMillis();
while (true)
{
Scanner number = new Scanner(System.in);
int f = number.nextInt();
if (i > f)
{
System.out.println("小了");
} else if (i < f)
{
System.out.println("大了");
} else
{
System.out.println("猜对了");
System.out.println("猜题用时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
break;
}
}
}
}
JAVA猜数字游戏源程序
Comments
Post a Comment