使用用户输入在Java中声明方程的变量

时间:2015-05-20 19:59:50

标签: java user-input

import java.util.Scanner;

public class Main {

    static double a, b, c, x, y, AOS;

    public static void main(String[] args) {
        Main.getA();
    }

    public static void getA() {
        Scanner inputA = new Scanner(System.in);
        System.out.println("Input variable 'a'");
        a = inputA.nextDouble();
        inputA.close();

        System.out.println("A: " + a);

        Main.getB();
    }
    public static void getB() {
        Scanner inputB = new Scanner(System.in);
        System.out.println("Input variable 'b'");
        b = inputB.nextDouble();
        inputB.close();

        System.out.print("B: " + b);

        Main.getC();
    }
    public static void getC() {
        Scanner inputC = new Scanner(System.in);
        System.out.println("Input variable 'c'");
        c = inputC.nextDouble();
        inputC.close();

        System.out.print("C: " + c);

        Main.getAOS();
    }
    public static void getAOS() {
        AOS = (-b + Math.sqrt((b*b)-4*a*c)) / 2*a;
        System.out.println("AOS: " + AOS);

        Main.getPoint1();
    }
    public static void getPoint1() {
        x = AOS;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("Origin: (" + x + "," + y + ")");

        Main.getPoint2();
    }
    public static void getPoint2() {
        x = AOS + 1;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("1: (" + x + "," + y + ")");

        Main.getPoint3();
    }
    public static void getPoint3() {
        x = AOS - 1;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("2: (" + x + "," + y + ")");

        Main.getPoint4();
    }
    public static void getPoint4() {
        x = AOS + 2;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("3: (" + x + "," + y + ")");

        Main.getPoint5();
    }
    public static void getPoint5() {
        x = AOS - 2;
        y = (a*(x*x)) + (b*x) + c;
        System.out.println("4: (" + x + "," + y + ")");
    }
}

这是我的代码。我试图获取用户输入以选择变量a,b和c的值,以便程序可以运行方程式。数学有效,但我在第一次输入后收到错误。

1 个答案:

答案 0 :(得分:1)

当你关闭扫描仪时,你也关闭了底层的流(在你的情况下是System.in),然后即使在尝试从另一个扫描仪实例访问它之后你也无法再次读取它。

因此,最简单的方法是仅使用一个Scanner对象而不是多个。将代码更改为:

static double a, b, c, x, y, AOS;
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    Main.getA();
}

public static void getA() {
    System.out.println("Input variable 'a'");
    a = scanner.nextDouble();
    System.out.println("A: " + a);
    Main.getB();
}

public static void getB() {
    System.out.println("Input variable 'b'");
    b = scanner.nextDouble();    
    System.out.print("B: " + b);
    Main.getC();
}
public static void getC() {
    System.out.println("Input variable 'c'");
    c = scanner.nextDouble();
    System.out.print("C: " + c);
    Main.getAOS();
}
public static void getAOS() {
    AOS = (-b + Math.sqrt((b*b)-4*a*c)) / 2*a;
    System.out.println("AOS: " + AOS);
    Main.getPoint1();
}
public static void getPoint1() {
    x = AOS;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("Origin: (" + x + "," + y + ")");
    Main.getPoint2();
}
public static void getPoint2() {
    x = AOS + 1;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("1: (" + x + "," + y + ")");
    Main.getPoint3();
}
public static void getPoint3() {
    x = AOS - 1;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("2: (" + x + "," + y + ")");
    Main.getPoint4();
}
public static void getPoint4() {
    x = AOS + 2;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("3: (" + x + "," + y + ")");
    Main.getPoint5();
}
public static void getPoint5() {
    x = AOS - 2;
    y = (a*(x*x)) + (b*x) + c;
    System.out.println("4: (" + x + "," + y + ")");
}
}

您可能还想关闭扫描程序而不关闭System.in流。在这种情况下,如here所述,在CloseShieldInputStream中包装System.in