在这种情况下,如何正确检查用户输入是否为Integer?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在这种情况下,如何正确检查用户输入是否为Integer?相关的知识,希望对你有一定的参考价值。

我有一个任务,我必须使用递归函数计算用户确定的Fibonacci数量,然后再使用迭代函数,然后比较执行每个函数所需的时间。

我完成了所有工作,除了,我无法检查有效的整数输入是否有效(参见reader()方法)。

import java.util.Scanner;
import java.lang.System;
import java.util.concurrent.TimeUnit;


/*
Class: FibonacciNumbers
Description: This class will take an integer from the user representing how many Fibonacci numbers they want to be calculated
             and printed. It will then calculate this using a recursive function, and output the Fibonacci sequence. It will
             then calculate the time elapsed (in nanoseconds) to perform the calculations and outputs, and will output this
             result. It will then repeat this entire process using an iterative function (for loop). This is mainly meant to
             show the difference in time between recursive and iterative functions.
*/
public class Fibonacci_Numbers {


    private static int n1 = 0, n2 = 1; // first two Fibonacci numbers (no need to calculate).


    /*
    Method: main
    Description: The primary method of the program. This introduces the program and the recursive and iterative functions to the
                 user. For the recursive function, it calls the reader function to get the integer representing the number of
                 Fibonacci numbers to be printed/calculated, then passes it to the recursive function. It also calculates and outputs
                 the time elapsed (in nanoseconds) to complete the recursive function. Before it calls the iterative function, it
                 resets the previously defined variables representing the first two numbers of the Fibonacci sequence to their
                 original values after they were altered within the recursive function. After calling and the completion of the
                 iterative function, it then exits the program.
    Usage: primary method in class FibonacciNumbers
    Parameters: String args[]
    Return Type: void
    */
    public static void main(String args[]) {

        System.out.println(); // blank line, for output formatting.
        System.out.println("Welcome to the Fibonacci Sequence Recursion/Iteration Comparison Program!"); // welcome message.
        System.out.println(); // blank line, for output formatting.
        System.out.println("RECURSIVE FUNCTION:"); // introduce recursive function.

        int n = reader(); // call reader function.

        long startTime = System.nanoTime(); // get and store time at beginning of recursive function.
        fibSeriesRec(n); // call recursive function.
        long endTime = System.nanoTime(); // get and store time at end of recursive function.
        long timeElapsed = endTime - startTime; // calculate total time elapsed during recursive function.
        System.out.println(); // blank line, for output formatting.
        System.out.println("Elapsed Time for Recursive Function: " + timeElapsed + " nanoseconds."); // print total time elapsed during recursive function.
        System.out.println(); // blank line, for output formatting.

        n1 = 0; // reset first number of Fibonacci sequence.
        n2 = 1; // reset second number of Fibonacci sequence.
        System.out.println("ITERATIVE FUNCTION:"); // introduce iterative function.
        fibSeriesIte(); // call iterative function.

        System.exit(0); // exit program.

    }


    /*
    Method: reader
    Description: Reads integer n from user to represent number of Fibonacci numbers to be calculated/printed, then returns n.
    Usage: called in main and fibSeriesIte methods
    Parameters: none
    Return Type: integer
    */
    private static int reader() {

        Scanner scanner = new Scanner(System.in); // create new scanner object to scan user input.
        System.out.println("How many Fibonacci numbers would you like to generate?: "); // prompt user to enter number.
        int n = scanner.nextInt(); // read user-entered number and store as integer.
        if (!scanner.hasNextInt() || n <= 0){ // if the user input is not an integer or is less than or equal to 0...
            System.out.println("Invalid integer. Please try again."); // print error message.
            reader(); // restart reader method.
        } 
        return n; // return user-entered integer.
    }


    /*
    Method: fibSeriesRec
    Description: Takes user-inputted integer n from main method (from reader method) and calculates and prints the Fibonacci sequence of n
                 numbers recursively. At the end of the function (within the if statement), the method calls itself again (for each number).
                 Each time through the function, n decreases by 1, and it will only run through the if statement while n is greater than 0.
                 Therefore, it will go through the function the original, user-inputted value of n times.
    Usage: called in main method as well as within itself, fibSeriesRec method (recursion)
    Parameters: integer n (from main, from reader. number of Fibonacci numbers to be calculated/printed)
    Return Type: void
    */
    private static void fibSeriesRec(int n) {

        if(n>0){ // if the number entered by user is greater than 0...

            System.out.print(n1 + " "); // print next Fibonacci number in sequence.
            int sum = n1 + n2; // calculate sum of previous two numbers in Fibonacci sequence.
            n1 = n2; // set value of first number to second number (move first number that will be added in calculation of next number in sequence over).
            n2 = sum; // set value of second number to the sum of previous two numbers (move second number that will be added in calculation of next number in sequence over).
            fibSeriesRec(n-1); // call to restart recursive function and decrease n by 1.
        }

    }


    /*
    Method: fibSeriesIte
    Description: Calls reader method to get a user-inputted integer to represent the amount of Fibonacci numbers to be printed in the
                 Fibonacci sequence. Will then use a for loop to iteratively calculate and output Fibonacci sequence. The time elapsed
                 during the for loop will be calculated and output as well.
    Usage: called in main method
    Parameters: none
    Return Type: void
    */
    private static void fibSeriesIte() {

        int n = reader(); // call reader function.
        long startTime = System.nanoTime(); // get and store time at start of iterative function.

        for (int i = 1; i <= n; i++) { // for loop that will run n times (number of Fibonacci numbers desired by user).

            System.out.print(n1 + " "); // print next Fibonacci number in sequence.
            int sum = n1 + n2; // calculate sum of previous two numbers in Fibonacci sequence.
            n1 = n2; // set value of first number to second number (move first number that will be added in calculation of next number in sequence over).
            n2 = sum; // set value of second number to the sum of previous two numbers (move second number that will be added in calculation of next number in sequence over).

        }

        long endTime = System.nanoTime(); // get and store time at end of iterative function.
        long timeElapsed = endTime - startTime; // calculate total time elapsed during iterative function.
        System.out.println(); // blank line, for output formatting.
        System.out.println("Elapsed Time for Iterative Function: " + timeElapsed + " nanoseconds."); // print total elapsed time during iterative function.

    }

}
答案

我将进入读者方法。

你是第一次得到int n = scanner.nextInt();,然后检查scanner.hasNextInt()但反之亦然。

 boolean isIntInput = scanner.hasNextInt();
 int n=-1;
 if (isIntInput ) { 
    n = scanner.nextInt();
 }else {
  //invalid input
 }
另一答案

您通过使用不同的功能和方法使程序复杂化。如果您希望在特定数字之间生成Fibonacci,请使用scanner类来获取用户输入。如果要从0和1开始。将扫描器类放入try catch并处理异常。

    Scanner scanner = new Scanner(System.in);
int number = 1;

while(true)
{
    try
    {
        option = scanner.nextInt();
    }
    catch (InputMismatchException exception)
    {
        System.out.println("Integers only, please.");
     //Use a  .next() token to escape the infinite loop
    }
}

对于这两个输入。然后为for循环和Fibonacci的基本业务逻辑创建一个函数。

 for (int i = 1; i <= n; i++) { // for loop that will run n times (number of Fibonacci numbers desired by user).

        System.out.print(n1 + " "); // print next Fibonacci number in sequence.

        int sum = n1 + n2; // calculate sum of previous two numbers in Fibonacci sequence.

        n1 = n2; // set value of first number to second number (move first number that will be added in calculation of next number in sequence over).

        n2 = sum; // set value of second number to the sum of previous two numbers (move second number that will be added in calculation of next number in sequence over).

    }

不要为您和系统复制程序以使其更加优化

以上是关于在这种情况下,如何正确检查用户输入是否为Integer?的主要内容,如果未能解决你的问题,请参考以下文章

如何检查之前的 Zsh 提示符中是不是没有输入任何内容

文件路径作为命令行参数

如何check AD user的信息?

检查用户输入的密码在7zip中是不是正确

jquery如何一边输入一边检查

检查提交的单选按钮是否为true - symfony