简体   繁体   English

While 循环不会继续要求用户输入

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

I am very new to java and I need help.我对java很陌生,我需要帮助。 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.我知道使用while循环可以解决这个问题,但我不确定如何让它工作。

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.可以继续的事情目前仅for , whiledo/while语句,因此它会找到while (number % 2 != 0)并将继续它。

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.继续 while 循环意味着:直接跳回到条件number %2 != 0 ,对其进行评估,如果为真,则再次进入循环,如果为假,则跳到}

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.想必你的意图是再次询问用户,但你不得不环绕更多的代码循环:开始与print ,因为肯定sc.nextInt()必须在循环 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).那并不代表你会不会有一个number值来检查,但是这就是做/ while循环是:为了保证您的循环至少一次(所以你可以使用循环计算什么作为条件的一部分)。

You should also use the scanner inside the while loop in case the number is not odd.您还应该在while循环内使用扫描仪,以防数字不是奇数。

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.您的困惑似乎来自误解,即continue意味着回到while循环,而break是让您退出循环的原因。 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();我希望这个输出是你正在寻找的,你之前的代码不起作用的原因是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!这就是为什么你可以提示用户输入的原因,所以你必须循环它,此外,你可以根据用户在if语句中输入的内容给出特定的提示,希望这有帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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