Below is some simple coding I am trying to practice on. I am currently going through Java for Dummies vol. 4 and trying out some simple progs.
I was expecting this program to:
1. Ask user for int between 1-10
2. Take user number and multiply that number times 0, 1, 2, 3, ... each iteration being its own line.
i.e. 3 times 0 is 0
3 times 1 is 3
3 times 2 is 6
etc.
3. If the number is NOT between 1 and 10, prompt the user to enter a number between 1 and 10 again.
ISSUE:
If the user enters an int between 1 and 10, all works fine.
If the user enters an int <1 || >10, it prompts for a reenter.
- The NEXT time, it doesn't matter what the number is. It will multiply the number regardless of range.
QUESTION:
Why is the program working the FIRST time through if a user enters an out of range int but not subsequent iterations?
I feel that I am supposed to be using an 'else if' but still have not fully grasped those. When I try to implement an 'else if' under the current 'if', I get an error. Is the 'else if' supposed to be inside the {} of an if statement. The setup is confusing me somewhat. Thanks
Code:import java.lang.System;
import java.util.Scanner;
public class LoopTest{
public static void main(String[] args){
System.out.print("Give me a number ");
System.out.println("between 1 and 10...");
Scanner myScanner = new Scanner(System.in);
int inputNumber = myScanner.nextInt();
if((inputNumber >=1) && (inputNumber <=10)){
for(int count = 0; count <=10; count++){
System.out.println(inputNumber + " times " + count + " is equal to " + inputNumber*count);
}
}
while((inputNumber < 1) || (inputNumber > 10)){
System.out.println("Please try again.");
System.out.println("Please pick a number between 1 and 10");
int reTry = myScanner.nextInt();
for(int count = 0; count <= 10; count++){
System.out.println(reTry + " times " + count + " is equal to " + reTry*count);
}
}
System.out.println("Okay. We're done!");
}
}
