package test;
import java.util.Random;
import java.util.Scanner;
public class GuessNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer randomNum = new Random().nextInt(100), count = 0;
long start = System.currentTimeMillis();
boolean bingo = false;
while (!bingo) {
try {
System.out.print("猜猜这个数:");
count++;
Integer theNum = sc.nextInt();
switch (theNum.compareTo(randomNum)) {
case 0:
bingo = true;
break;
case 1:
System.out.println("大了+++++");
break;
case -1:
System.out.println("小了-----");
break;
default:
break;
}
// int res = theNum - randomNum;
// if (res > 0) {
// System.out.println("大了~~");
// } else if (res < 0) {
// System.out.println("小了~~");
// } else {
// System.out.println("猜对了!!!猜了" + count + "次,耗时:"
// + ((System.currentTimeMillis() - start) / 1000.0)
// + "秒");
// }
} catch (Exception e) {
System.err.println("请输入数字");
}
}
System.out.println("猜对了!!!猜了" + count + "次,耗时:"
+ ((System.currentTimeMillis() - start) / 1000.0) + "秒");
sc.close();
}
}
/*
* 1.创一个Demo类
* 2.创建一个随机数
* 3.调用random的方法nextInt(10);
* 4.键盘录入 为整数类型调用input.nextInt();
* 5.创建一个while循环 一直猜到位置
*
*/
public class Demo {
public static void main(String[] args) {
//创建Random对象
Random ran = new Random();
//随机获取int范围内的一个数 0-10的随机数
int i = ran.nextInt(10);
//键盘录入 为整数类型
Scanner input =new Scanner(System.in);
int a=0;
//创建while循环
while (true) {
a= input.nextInt();
if(a==i){
System.out.println("恭喜你猜中了");
return;
}
if(a>i){
System.out.println("恭喜你猜大了");
}
if(a
System.out.println("恭喜你猜小了");
}
}
}
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int beGuess = random.nextInt(10);
System.out.println("请开始输入所猜的数,范围0-9");
long start = System.currentTimeMillis();
int guess = -1;
while (true) {
guess = scanner.nextInt();
if(guess==beGuess){
break;
}else if(guess > beGuess){
System.out.println("很遗憾,猜的有些大了!请继续!");
}else if(guess < beGuess){
System.out.println("很遗憾,猜的有些小了!请继续!");
}
}
System.out.println("恭喜猜对!用时:"+(System.currentTimeMillis()-start)+"毫秒");
}
是想表达什么,写些代码就可以了 ,呵呵