2015-05-20 109 views
-1
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的值,以便程序可以運行方程式。數學可行,但在第一次輸入後出現錯誤。使用用戶輸入聲明Java中變量的變量

+7

你的錯誤是什麼? – CubeJockey

+2

關閉後無法從System.in中讀取。即使你創建了一個新的'Scanner'指向它,你已經關閉了流。 – azurefrog

回答

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流。在這種情況下將System.in換成CloseShieldInputStream,如here

+0

@azurefrog,謝謝你的提示。當然我會編輯它。 – alainlompo

+1

我喜歡'CloseShieldInputStream'的想法。我之前沒有遇到過這種情況。這將允許你嚴格地限制你的輸入'掃描儀',而不是依靠一個靜態的'掃描儀'。 – azurefrog

+0

@azurefrog,是的,這是一個好主意。我相信它(這個想法)可以以類似於模式的方式重用 – alainlompo