这里是整个代码我如何创建随机数,以便随机生成numberneedingtobeguessed
。
public class Guessing {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int number;
int numberneedingtobeguessed = 26;
int done = 1;
System.out.println("I am thinking of a number in between 1 and 50 what is it?");
while (done < 2) {
number = input.nextInt();
if (number < numberneedingtobeguessed) {
System.out.println("Too low");
}
else if (number > numberneedingtobeguessed) {
System.out.println("Too high");
}
else if (number == numberneedingtobeguessed) {
System.out.println("You guessed correctly");
done = done + 1;
}
}
}
}
答案 0 :(得分:1)
import java.util.Random;
.........
Random ran = new Random();
int r = ran.nextInt(50) + 1;
这将生成0到49之间的随机数,然后在结尾添加1。