【问题标题】:For two integer user inputs, how to detect that a string has been passed on the very first input?对于两个整数用户输入,如何检测第一个输入是否传递了字符串?
【发布时间】:2020-03-14 21:48:58
【问题描述】:

这是我的代码。检查用户输入“开始”和“停止”之间的素数的程序。

import java.util.*;
public class test
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter start: ");
            String start = sc.nextLine();
            if(start.equalsIgnoreCase("stop"))
            {
                break;
            }
            System.out.println("Enter stop: ");
            String stop = sc.nextLine();
            if(stop.equalsIgnoreCase("stop"))
            {
                break;
            }
            try
            {
                int s1 = Integer.parseInt(start);
                try
                {
                    int s2 = Integer.parseInt(stop);
                    for(int i = s1; i<=s2;i++)
                    {
                        if(p1.isPrime(i))
                        {
                            System.out.print(i+" ");
                        }
                    }
                    System.out.println("");
                }
                catch(java.lang.NumberFormatException er)
                {
                    System.out.println("Invalid Input");
                }
            }
            catch(java.lang.NumberFormatException er1)
            {
                System.out.println("Invalid Input");
            }
        }   
    }
}

在开始输入时,如果我输入任何字符串,我希望代码立即检测 NumberFormatException 并再次请求相同的输入。

相反,它接受两个输入,无论是字符串还是整数,然后才评估它是否是字符串。

我不使用sc.nextLine(),因为我想要停止功能。如果我在任何地方输入“stop”,我希望程序停止执行。

【问题讨论】:

    标签: java string input try-catch numberformatexception


    【解决方案1】:
    public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            int start = -1;
            int stop = -1;
            while (true)
            {
                if (start == -1)
                {
                    System.out.println("Enter start: ");
                    if (sc.hasNextInt())
                    {
                        start = sc.nextInt();
                    } else if (sc.nextLine().equalsIgnoreCase("stop")) break;
                    else
                    {
                        System.out.println("Invalid Input");
                        continue;
                    }
                }
                if (stop == -1)
                {
                    System.out.println("Enter stop: ");
                    if (sc.hasNextInt())
                    {
                        stop = sc.nextInt();
                    } else if (sc.nextLine().equalsIgnoreCase("stop")) break;
                    else
                    {
                        System.out.println("Invalid Input");
                        continue;
                    }
                }
                for (int i = start; i <= stop; i++)
                {
                     // Prime number method call here
                    System.out.print(i + " ");
    
                }
                System.out.println();
                start = stop = -1;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多