I'm a beginner in coding and wanted to train, and so I started doing exercises that I find on the internet, I finished one and was unsatisfied because of how easy it was, and created myself a challenge.
The exercise was: you type in a variable and it tells you if it is above a certain number, in this case it's 50, but here's the thing, I didn't want to type it in, I want it to be randomly generated, but I can't find a way to solve the problem, it blocks at nextInt
.
public class CheckPassFail { // saved as "CheckPassFail.java"
public static void main(String[] args) {
random r = new random ();
int Low = 1;
int High = 60;
int mark = r.nextInt(High-Low)+ Low;
System.out.println("The mark is " + mark);
if (mark>50) {
System.out.println("PASS");
} else {
System.out.println("Fail");
}
}
private static class random {
public random() {
}
}
}
Use Java's Random
class instead of defining your own private static class.
import java.util.Random;
public class CheckPassFail { // saved as "CheckPassFail.java"
public static void main(String[] args) {
Random r = new Random ();
int Low = 1;
int High = 60;
int mark = r.nextInt(High-Low)+ Low;
System.out.println("The mark is " + mark);
if (mark>50) {
System.out.println("PASS");
} else {
System.out.println("Fail");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.