[Java]代码
import java.util.*;
public class Guess{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Random random = new Random();
System.out.print("\n 请输入随机数范围:0—");
int index = in.nextInt();
int temp = random.nextInt(index);
int count = 0;
int first = 0;
int end = index;
for(int i = 0;;i++){
System.out.print("\n请输入数字:");
int d = Random1(first,end);
System.out.print(d);
if(d < temp){
System.out.println("\n有点小了,请继续!");
first = d;
count++;
continue;
}
if(d > temp){
System.out.println("\n有点大了,请继续!");
end = d;
count++;
continue;
}
if(d == temp){
System.out.println("\n恭喜你,猜对了!");
break;
}
}
System.out.println("\n你一共用了"+count+"次!");
}
public static int Random1(int first, int end ){
Random random = new Random();
return (random.nextInt((end-first)) + first);
}
}
|
|