【问题标题】:Using user input to declare variables for equations in Java [duplicate]使用用户输入在Java中声明方程的变量[重复]
【发布时间】:2015-08-02 06:25:05
【问题描述】:
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 的值,以便程序可以运行方程。数学有效,但在第一次输入后出现错误。

【问题讨论】:

  • 你的错误是什么?
  • 关闭System.in后无法读取。即使您创建一个新的Scanner 指向它,您也已经关闭了流。

标签: java user-input


【解决方案1】:

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

因此,最简单的方法是只使用一个 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 流的情况下关闭 Scanner。在这种情况下,将 System.in 包装在 CloseShieldInputStream 中,正如 here 所解释的那样

【讨论】:

  • @azurefrog,感谢您的留言。当然我会编辑它。
  • 我喜欢CloseShieldInputStream 的想法。我以前没有遇到过。这将允许您严格限制输入 Scanners,而不是依赖于单个静态 Scanner
  • @azurefrog,是的,这是个好主意。我相信它(想法)可以以类似模式的方式重复使用
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 2018-10-02
相关资源
最近更新 更多