简体   繁体   中英

While loop will not continue to ask the user for an input

I am very new to java and I need help. Basically, I have a program that asks the user to input a number. When the number is input, it takes a sum of all of the odd numbers before that number and adds them up. What I'm trying (and failing) to do is, make another loop whereby, when the user is prompted to ask for a number to sum up the odd numbers, I want to make it so that it will only continue when an odd number is entered, otherwise it will keep repeatedly asking the user until they enter an odd number. I know that using a while loop will solve this issue, but I'm not sure how to get it to work.

Here's my code:

import java.util.Scanner;

public class OddCalculator {
    private static Scanner sc;
    public static void main(String[] args) 
    {
        int number, i, oddSum = 0;
        sc = new Scanner(System.in);
        System.out.print(" Please Enter any Number : ");
        number = sc.nextInt();  
        while (number % 2 !=0) //HERE IS WHERE IM HAVING THE ISSUE
        {
            continue;
        }
        for(i = 1; i <= number; i++)
        {
            if(i % 2 != 0)
            {
                oddSum = oddSum + i; 
            }
        }
        System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
    }
}

Thanks in advance!

continue; as a statement scans 'upwards and outwards' for the first construct that can be continued. Things that can be continued are currently only for , while and do/while statements, so it finds while (number % 2 != 0) and will continue it.

To continue a while loop means: Jump straight back to the condition number %2 != 0 , evaluate it, and then enter the loop again if it is true, or hop to the } if it is false.

So, your code checks if the number is odd. If it is, it will .. continue. So, it will.. check if the number is odd. If it is, it will check if the number is odd. If it is, it will check if the number is odd.... forever.

Presumably your intent is to ask the user again, but then you'd have to wrap the loop around more code: Start with the print , because certainly sc.nextInt() needs to be inside the loop. That does mean you won't have a number value to check, but that's what do/while loops are for: To guarantee you loop at least once (and so that you can use anything calculated in the loop as part of the condition).

You should also use the scanner inside the while loop in case the number is not odd.

while (number % 2 !=0) {
    number = sc.nextInt(); // Use here as well to keep asking for a number until is odd
}
    

Your confusion seems to be coming from misunderstanding that continue means going back to the while loop, and break is what gets you out of the loop. Does this work for you?

System.out.println(" Please Enter any Number : ");
number = sc.nextInt();

// keep asking for a number for as long as it is even (condition is false on odd)
while (number % 2 == 0) {
   System.out.println("Please enter another number: ");
   number = sc.nextInt();
}

System.out.println(number + " is now odd!");

I hope this output is what you are looking for, the reason why your previous code doesn't work is that number = sc.nextInt(); is the reason why you can prompt the user for an input, so you have to loop it, furthermore, you can give a specific prompt base on what the user has inputted in the if statement, hope this helps!

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
     // int number, i, oddSum = 0;
    int number, i, oddSum = 0;
    Scanner sc = new Scanner(System.in);
    System.out.print("Please Enter any Number : ");
    do{
      number = sc.nextInt();
      if(number % 2 == 0){
        System.out.print("Please Enter an odd number!: ");
      }
    }while(number % 2 == 0);
    for(i = 1; i <= number; i++)
      {
          if(i % 2 != 0)
          {
              oddSum = oddSum + i; 
          }
      }
    System.out.println("\nThe Sum of Odd Numbers up to "  + number +   " = "  + oddSum);
  }
}

Output:

Please Enter any Number : 2
Please Enter an odd number!: 2
Please Enter an odd number!: 3

The Sum of Odd Numbers up to 3 = 4

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM