java中的扫描问题

q8l4jmvw  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(142)

我有一个任务,我必须计算一个给定物体的周长和面积,这是由用户决定的,并附带数据-边长,半径等。要做到这一点,我必须做一个“图形用户界面”如我的老师所说,要做到这一点,我必须使用扫描仪。
根据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没有标记出该行有任何问题,所以对我来说就更没有意义了。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题