在 java 中扫描的问题

Problems with scanning in java

提问人:SpeedRunHunter 提问时间:12/1/2020 更新时间:6/10/2021 访问量:135

问:

我接到一个任务,我必须计算给定对象的周长和面积,这是由用户确定的,并附带数据 - 边长、半径等。要做到这一点,我必须按照老师说的做一个“GUI”,要做到这一点,我必须使用扫描仪。

根据 NetBeans 的说法,每次我尝试进行第二次扫描时,在用户选择我们正在处理的对象之后,当它到达用户应该输入有关其对象的数据的部分时,它总是崩溃,并出现 java.util.NoSuchElementException 错误。我浏览了它,甚至在工作的扫描仪中复制了它,但无济于事。

以下是完整的代码:

package Methods2;
import java.util.Scanner;
public class Methods2 {
public static void main(String[] args) {
    //initialization
    int decider;
    Scanner input1;
    
    //defining
    input1 = new Scanner(System.in);
    System.out.println("Choose from these options to find the perimeter and area of any of these:\n1. Circle\n2. Square\n3. Rectangle");
    decider = input1.nextInt();
    input1.close();
    
    //decision
    if (decider == 1) {
        circle();
    } else if (decider == 2) {
        square();
    } else if (decider == 3) {
        rectangle();
    } else {
        System.out.println("There aren't any other options, other than these three.");
    }
}
public static void circle() {
    //method specific initialization
    int radius;
    double pi;
    double perimeter;
    double area;
    Scanner input2;
    
    //define
    pi = 3.14;
    input2 = new Scanner(System.in);
    System.out.println("Please type in the radius of the circle!");
    radius = input2.nextInt(); //these are where my problem's lie
    input2.close();
    
    //calculate
    perimeter = 2 * radius * pi;
    area = radius * radius * pi;
    
    //print
    System.out.println("The perimeter of this circle is: " + perimeter);
    System.out.println("The area of this circle is: " + area);
}
public static void square() {
    //method specific initialization
    int a;
    int perimeter;
    int area;
    Scanner input3;
    
    //define
    input3 = new Scanner(System.in);
    System.out.println("Please type in one side's length of the square!");
    a = input3.nextInt(); //these are where my problem's lie
    input3.close();
    
    //calculate
    perimeter = 4 * a;
    area = a * a;
    
    //print
    System.out.println("The perimeter of this circle is: " + perimeter);
    System.out.println("The area of this circle is: " + area);
}
public static void rectangle() {
    //method specific initialization
    int a;
    int b;
    int perimeter;
    int area;
    Scanner input4;
    
    //define
    input4 = new Scanner(System.in);
    System.out.println("Please type in one of the sides' length of the rectangle!");
    a = input4.nextInt(); //these are where my problem's lie
    System.out.println("Now type the other, non-equal side, compared to the previous one!");
    b = input4.nextInt(); //these are where my problem's lie
    input4.close();
    
    //calculate
    perimeter = 2 * (a + b);
    area = a * b;
    
    //print
    System.out.println("The perimeter of this circle is: " + perimeter);
    System.out.println("The area of this circle is: " + area);
}
}

我曾想过它是多个 Scanner,但在我意识到,变量不会在方法之间转移,除非它们在类中定义,否则它很快就被当作理论抛弃了。此外,NetBeans 没有标记该行的任何问题,因此它对我来说更没有意义。

Java 方法 java.util.scanner

评论

0赞 daniu 12/1/2020
如果要关闭 ,则会关闭其基础输入流,在您的情况下为 。如果它已关闭,则不能使用它来馈送新的(在您的形状方法中)。删除 or 保留相同的内容以读取进一步的输入。ScannerSystem.inScannerinput1.close()Scanner
1赞 BeUndead 12/1/2020
1.方法不一定是,它们可以得到你想要的东西。2. 方法可以有参数,因此您可以在它们之间传递内容,而无需将它们存储为字段。voidreturn
0赞 SpeedRunHunter 12/1/2020
谢谢你们俩!我将删除关闭。至于退货或作废;我们的老师不太喜欢我们在代码中包含任何我们还没有学到的东西,尤其是对于测试。虽然我很欣赏这个提示,但到目前为止我还不能真正使用它。

答:

0赞 FairOPShotgun 6/10/2021 #1

您的代码“停止”扫描程序的原因是您添加了 .它的作用是关闭扫描仪。扫描仪关闭后,您将无法再次打开它。根据您的代码,您可以使用 Scanner..即使在它关闭之后。因此,为了解决您的问题,请删除以下行:input1.close();.close()

input1.close();

这是您应该删除该行的位置的特写:

 //initialization
    int decider;
    Scanner input1;
    
    //defining
    input1 = new Scanner(System.in);
    System.out.println("Choose from these options to find the perimeter and area of any of these:\n1. Circle\n2. Square\n3. Rectangle");
    decider = input1.nextInt();
    //input1.close(); REMOVE THIS LINE