Java中使用Scanner类输入

5

我正在编写一个程序将给定的整数约分为最简形式。但是,在程序的子方法中通过Scanner类获取输入时出现了错误。下面是代码:

package CodeMania;

import java.util.Scanner;

public class Question5 
{
public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);
    int T=sc.nextInt();// number of test cases
    sc.close();
    if(T<1)
    {
        System.out.println("Out of range");
        System.exit(0);
    }
    for(int i=0;i<T;i++)
    {
    ratio();//line 19
    }

}
static void ratio()
{
    Scanner sc1=new Scanner(System.in);
    int N=sc1.nextInt();//line 26
    if((N>500)||(N<1))
    {
        System.out.println("Out of range");
        System.exit(0);
    }
    int a[]=new int[N];
    for(int i=0;i<N;i++)
    {
        a[i]=sc1.nextInt();
    }
    int result = a[0];
   for(int i = 1; i < a.length; i++)
        {
    result = gcd(result, a[i]);
    }
    for(int i=0;i<N;i++)
    {
        System.out.print((a[i]/result)+" ");
    }
    sc1.close();
}
static int gcd(int a, int b)
{
    while (b > 0)
    {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
}

错误是--
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at CodeMania.Question5.ratio(Question5.java:26)
    at CodeMania.Question5.main(Question5.java:19)

在这里,我使用了 2 个单独的扫描器对象 sc 和 sc1 来从控制台接收输入。然而,如果我在类范围内声明一个公共静态类型 Scanner 对象,然后仅使用一个 Scanner 对象来接收整个程序的输入,则程序会按要求正常工作而不出错。

为什么会这样呢...?


看看这个答案,我认为你遇到了同样的问题。 - Alin Pandichi
4个回答

5
这个错误的原因是在扫描器上调用.close()方法也会关闭inputStream System.in,但是实例化一个新的Scanner不会重新打开它。
你需要在方法参数中传递单个扫描器,或将其作为静态全局变量。

我知道整个程序只需要传递一个扫描器,但我不知道为什么不能使用多个扫描器...谢谢你给出的原因。 - Suyash Tilhari

0

由于您的main()ratio()方法使用了Scanners,它们会抛出异常。当异常发生时,程序的正常流程被打断,程序/应用程序会异常终止,这是不推荐的,因此必须处理这些异常。 异常可能发生的原因有很多,以下是一些异常发生的情况。

A user has entered invalid data.

A file that needs to be opened cannot be found.

A network connection has been lost in the middle of communications or the JVM has run out of memory.

您可以通过使用Try/Catch块来处理这些异常,或者在方法定义后使用单词throws来处理它们,在您的情况下,这两种方法将是这样的:

使用Try/Catch:

  public static void main()
  {
    try{
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();// number of test cases
        sc.close();
       }
    catch(NoSuchElementException e){
    System.out.print("Exception handled" + e);
    //rest of method
    }
    static void ratio(){
    try{
    Scanner sc1=new Scanner(System.in);
    int N=sc1.nextInt();}
    catch(NoSuchElementException e){
    System.out.print("Exception handled" + e);}
    //rest of method
    }

使用 "throws" 关键字:

  public static void main()throws Exception{

       //rest of method
     }
   static void ratio()throws Exception
   { 
    //rest of method
   }

0

试试这个。你可以将扫描器作为参数传递。

package stack.examples;

import java.util.Scanner;

public class Question5 {
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();// number of test cases
    if (T < 1) {
        System.out.println("Out of range");
        System.exit(0);
    }
    for (int i = 0; i < T; i++) {
        ratio(sc);// line 19
    }
    sc.close();
}

static void ratio(Scanner sc1) {
    int N = sc1.nextInt();// line 26
    //Your Logic
}

static int gcd(int a, int b) {
    while (b > 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

}


0
import java.util.*;
public class Understanding_Scanner
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Please enter your name");
String name=sc.next();
System.out.println("Your name is:"+name);
}
}

现在来解释这个东西,我们需要从Java Utility包中导入一个扫描器类,这可以通过第一行的代码实现;第二行创建一个类NOTE(类名不必以大写字母开头),现在来到主题——扫描器类。因此,我们必须在程序中创建一个扫描器类,并使用第四行给出的代码进行操作... 在此语句中,“sc”是一个对象,它存储了扫描器类的值,如果您想在扫描器类中执行任何操作,可以通过对象“sc”进行。*注意(您可以将对象命名为任何名称,例如:poop、bla等)... 然后我们有这个有趣的命令 System.in,它允许用户在运行时通过键盘或其他输入设备编写任何语句.... String name = sc.next() 这一行帮助我们在运行时编写任何字符串,该字符串将存储在名称变量中。

这就是扫描器类啦,希望理解起来容易。

祝福!继续编码 :-)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接