While loop to check user input for only positive integers

跟風遠走 提交于 2019-12-11 11:57:36

问题


I need help implementing a loop that keeps telling the user to enter only positive integers, but I don't know where to start. Can someone help me.

import java.util.Scanner;

public class GCD {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a positive integer: ");
      int firstN = input.nextInt();

    System.out.print("Enter another positive integer: ");
      int secondN = input.nextInt();

    System.out.println("The GCD of " + firstN + " and " + secondN + " is: " + gCd(firstN, secondN));

  } 

public static int gCd(int firstN, int secondN) {
    if(secondN == 0) {
        return firstN;
    } else 
    return gCd(secondN, firstN%secondN);
}

}

回答1:


In your situation, you need the user to enter something at least once. However, you want to keep asking the user to enter in something until it's valid.

In my opinion, a do-while loop is the clearest way to code this logic, as it does exactly what you want without any extraneous hacks.

For example,

    Scanner input = new Scanner( System.in );
    int firstN;
    do {
        System.out.println( "Enter a positive integer:" );
        firstN = input.nextInt();
    } while ( !(firstN > 0) ); 

The do-while loop works by executing the "do" part first and then checking the "while" condition. So, you first ask the user for a number, and if you don't like it, you just keep asking them for another one.

Let me know if anything is unclear!

Edit:

If you want to give the user an error message when the number is negative, then you need to consider a modified algorithm. You want to

  1. Ask the user for a number (without displaying an error message)
  2. As long as the number is not positive, tell the user it's not acceptable and ask for another one.

I assume you know about the various loops in Java like for, while, do-while. See you if you can code the logic for this one on your own and comment if you're having trouble.




回答2:


 int firstN=0;
 while(firstN<=0){
       System.out.print("Enter a positive integer: ");
       firstN = input.nextInt();
   };
 //implement a similar loop for secondN

The loop will continue till firstN is positive




回答3:


while(input.hasNext()) {

    if(input.hasNextInt()) {
        x = input.nextInt();
        if(x < 0) {
            // return or
            // throw exception or whatever your requirements is.
        }
        // your business logic
     }
}



回答4:


This should run in the main

    System.out.print("Enter a positive integer: ");
    Scanner input = new Scanner(System.in);
    int posInt;
    while((posInt=input.nextInt())<=0){
        System.out.println("Inter only positive integers.");
    }
    System.out.println("You entered the number:"+posInt);



回答5:


why don't you try :

Public static int gCd(int firstN, int secondN) {
    if(secondN < 0|| firstN< 0) {
       print("enter a positive value");
    } else 
    return firstN%secondN;
}


来源:https://stackoverflow.com/questions/35029469/while-loop-to-check-user-input-for-only-positive-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!