Reputation: 13
Im trying to Write a Java program that generates 100 random numbers in the range of 0-100 and counts how many are equal to or greater than a value entered by the user. I'm limited by not being permitted to use Arrays, and having to slot in a for loop and limiting a persons input for this to a number between 30 and 70 before checking it against how many of the random are equal to or above that input
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter a value between 30 and 70: ");
int i = in.nextInt();
do {
System.out.println("The value is out of range, please re-enter: ");
} while (i <= 30 || i >= 70);
break;
do {
System.out.print("The value entered by the user is: " + i);
in.nextLine();
do {
for (int ia = 0; ia < 3; ia++){
int random = (int)(10.0 * Math.random());
System.out.print("There are " + random + " numbers larger than " + i);
break;
}while (i >= 31 || i <= 69);
break;
while (true);
in.close();
}
}
}
}
Im riddled with errors such as Syntax error, insert "}" to complete Block Syntax error, insert "while ( Expression ) ;" to complete DoStatement and cant figure out what to do to get this program to run. I can tell its only getting worse the more } I add, but I cant think of what to do
Upvotes: 1
Views: 368
Reputation: 732
Break out your three loops into methods. Then it is much clearer for nesting them. Write pseudocode like this:
class {
static main () {
do {
ask user for more input;
getInput ();
if (input signals end) {
break;
}
count (input);
if (count says input was bad) {
report problem to user;
}
} while (computer's patience has not worn out);
}
static count (input) {
check input;
if (input is bad) {
return;
}
for (i 0 to 99) {
generate random number;
if (number >= input) {
count++;
}
}
report or return count;
}
Soon programming tasks will get easier if you write lots of pseudocode. Before long you will be writing them in your head. It's the way to go.
When in difficulty programming do these three things: 1) Write pseudocode 2) Refine pseudocode into sorta-kinda-maybe working code, 3) Walk through the code by hand. Then repeat 2) and 3) until the code compiles and is ready for debugging.
Soon you'll be doing the walk-throughs in your head.
Upvotes: 2