多个扫描器用于一个课程的程序,我不知道如何使用多个扫描器。

huangapple go评论69阅读模式
英文:

Mutliple scanners for a program for class, I can't figure out how to have more than one

问题

以下是翻译好的部分:

我需要在我的CS1400课程中为一个Java程序创建四个扫描器。我被告知要为每个扫描器使用不同的变量名,但是这样做只会导致程序崩溃,并显示错误信息“Exception in thread "main" java.util.NoSuchElementException。”我不确定我做错了什么,可能是相当简单的错误。我的Java和IntelliJ(课程要求使用)都已经更新。

第一个扫描器的简化代码:

System.out.println();
Scanner keyboard = new Scanner(System.in);
System.out.println("输入一个整数:");
int number = keyboard.nextInt();
System.out.println("你输入的是:" + number);
keyboard.close();

第二个扫描器的简化代码:

System.out.println();
Scanner scanObj = new Scanner(System.in);
System.out.println("输入一个数字:");
int id = scanObj.nextInt();
System.out.println("你输入的是:" + id);
scanObj.close();

免责声明:我知道这两个扫描器在做相同的事情,这只是我为了提问而编写的示例代码。

英文:

I need to make four scanners in a Java program for my CS1400 class. I was told to make each scanner have a different variable name, but doing that only made the program crash with an error reading "Exception in thread "main" java.util.NoSuchElementException." I'm not sure what I'm doing wrong, and it could be fairly simple. My Java and IntelliJ (required for the class) is all update.

Shortened for the first scanner:

System.out.println();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = keyboard.nextInt();
System.out.println("You entered: " + number);
keyboard.close();

Shortened code for the second scanner:

System.out.println();
Scanner scanObj = new Scanner(System.in);
System.out.println("Enter a number: ");
int id = scanObj.nextInt();
System.out.println("You entered " + id);
scanObj.close();

DISCLAIMER: I know both scanners are doing the same thing, it's just example code that I typed up for my question.

答案1

得分: 2

如果您正在使用控制台应用程序,建议仅使用一个Scanner。当您使用多个扫描仪时,存在因某些流/扫描仪未被正确关闭而导致的错误风险。

请参考此帖子:https://stackoverflow.com/questions/15443383/scanner-nosuchelementexception

英文:

If you are using a console application, it is advised to just use one Scanner. When you have multiple scanners, you run the risk of getting the error that you've already run into because of some streams/scanners not being properly closed.

Refer to this post: https://stackoverflow.com/questions/15443383/scanner-nosuchelementexception

答案2

得分: 0

如果你想要创建四个 Scanner 对象那么可以这样做

import java.util.Scanner;

class practice {
  public static void main(String ar[]) {

    Scanner obj1 = new Scanner(System.in);
    Scanner obj2 = new Scanner(System.in);
    Scanner obj3 = new Scanner(System.in);
    Scanner obj4 = new Scanner(System.in);

    System.out.println("1. 输入一个整数");
    int a = obj1.nextInt();
    System.out.println("2. 输入一个整数");
    int b = obj2.nextInt();
    System.out.println("3. 输入一个整数");
    int c = obj3.nextInt();
    System.out.println("4. 输入一个整数");
    int d = obj4.nextInt();

    System.out.println(a + "" + b + "" + c + "" + d);
  }
}

否则你可以使用单个 Scanner 对象

Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();

希望对你有所帮助
英文:

If you want to make four Scanner objects then it helps you:

import java.util.Scanner;

class practice
{
  public static void main(String ar[])
  {

    Scanner obj1=new Scanner(System.in);
    Scanner obj2=new Scanner(System.in);
    Scanner obj3=new Scanner(System.in);
    Scanner obj4=new Scanner(System.in);

    System.out.println("1. Enter an integer");
    int a=obj1.nextInt();
    System.out.println("2. Enter an integer");
    int b=obj2.nextInt();
    System.out.println("3. Enter an integer");
    int c=obj3.nextInt();
    System.out.println("4. Enter an integer");
    int d=obj4.nextInt();

    System.out.println(a+""+b+""+c+""+d);
  }

}

OtherWiase you can use single scanner object:

    Scanner sc = new Scanner(System.in);
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    int d=sc.nextInt();

I hope it would help you!

huangapple
  • 本文由 发表于 2020年8月29日 04:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63640273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定