2015-04-22 55 views
-1

我的代碼沒有看到來自第二類的輸入數字。它一直認爲所有的輸入都是零,所以它沒有完成程序。我哪裏做錯了?它不讀取我在原始類中的數字輸入

我試過了幾個不同的東西,我可以想到,但它沒有奏效。它不斷輸出錯誤/無法解析,因爲所有數字都是零而不是輸入。

第二類只是數字輸入。

private static int a, b, c, d, e, f; 

public static void main(String args[]) { 
    System.out.println("Please enter numbers for 'a, b, c, d, e, f' "); 
    Chapter9_2ndclass c2 = new Chapter9_2ndclass(a, b, c, d, e, f); 
    Exercise_09_11 Chapter9_2ndclass = new Exercise_09_11(a, b, c, d, e, f); 
    // Exercise_09_11 getY = new Exercise_09_11(a, b, c, d, e, f); 
    // //constructors 

    if (Chapter9_2ndclass.isSolvable()) { 
     Chapter9_2ndclass.getX(); 
     Chapter9_2ndclass.getY(); 
    } else { 
     System.out.println("Unsolvable"); 
    } 

} 

public Exercise_09_11(int a, int b, int c, int d, int e, int f) { 
    this.a = a; 
    this.b = b; 
    this.c = c; 
    this.d = d; 
    this.e = e; 
    this.f = f; 
} 

public boolean isSolvable() { 
    double isSolvable = ((a * d) - (b * c)); 
    System.out.println(isSolvable); 
    if (isSolvable != 0) { 
     System.out.println("True"); 
     return true; 
    } else { 
     System.out.println("False"); 
     System.out.println("The 'isSolvable' number is, " + isSolvable); 
     return false; 
    } 
} 

public int getY() { 
    int Y = ((a * f) - (e * c))/((a * d) - (b * c)); 
    return Y; 
} 

public int getX() { 
    int X = ((e * d) - (b * f))/((a * d) - (b * c)); 
    return X; 
} 

回答

1

您正在創建的Chapter9_2ndclassExercise_09_11一個新的實例,並通過它們的構造函數分配給它的private static int秒。

原始int默認爲0當在此上下文中未分配時,因此您將獲得全部0 s。

您可能想要通過在main方法中解析命令行執行參數int來分配它們。

1

所有變量都爲零,因爲它們不被初始化,你通過這導致INT即0要傳遞的默認值的變量的Chapter9_2ndclass的構造器和Exercise_09_11,並將它們初始化爲0

0

的問題是,你從不讀取用戶的任何輸入並設置值。具體來說,靜態int聲明a,b,c,d,e和f默認爲0並且從不設置。