for my first Java homework assignment, I have to write a simple program that tests for user input of 0 to break a while loop. I think I have the problem figured out, but I can't get rid of the "else with no if" error.
import java.util.Scanner;
class WhileLoopWithBreak {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Please enter the number 0.");
n = stdin.netxtInt();
if ( n != 0 ) {
System.out.println("Sorry, that was not the correct number. Please try again.");
continue;
else if ( n = 0 ) {
System.out.println("Yes, that was the correct number!");
break;
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire