2012-03-28 41 views
0

我正在做作業,我很近,但我遇到問題。我剛剛學會了如何在eclipse中使用包,所以我有一個類從包中導入另一個類(我想我說的是對的) 主要提示用戶輸入一個-100到100之間的整數,我有驗證它的問題。我知道這個問題是我正在導入的地方,我只是不確定我需要去修復它的方向。導入?與包一起工作

這是我的主要代碼的一部分。 (我的問題與過去的幾行開始,如果你想跳過前面)

import myUtils.util.Console; 

    public class ConsoleTestApp 
    { 
     public static void main(String args[]) 
     { 
      // create the Console object 
      Console c = new Console(); 

      // display a welcome message 
      c.println("Welcome to the Console Tester application"); 
      c.println(); 

      // int 
      c.println("Int Test"); 
      int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101); 
      c.println(); 

在代碼

int i = c. 

最後一行我得到下我一個波浪線,我不使用,告訴我局部變量,所以我不確定在這種情況下究竟修復了什麼,因爲我試圖在另一個類中使用它。我需要創建一個對象嗎?

我有一個名爲Console的類,位於另一個包中,我相信我已正確導入。 這裏是我卡在我的控制檯類的代碼。

package myUtils.util; 

import java.util.Scanner; 

public class Console 

{ 
Scanner sc = new Scanner(System.in); 
public void print(String s) 
{ 
    System.out.println(); 
} 

public void println(String s) 
{ 
    System.out.println(); 

} 

public void println() 
{ 
    System.out.println(); 

} 

public int getIntWithinRange(String prompt, int min, int max) 
{ 

    int i = 0; 
    boolean isValid = false; 
    while (isValid == false) 
    { 
     System.out.println(prompt); 
     if (sc.hasNextInt()) 
     { 

      i = sc.nextInt(); 
       if (i < min) 
       { 
        System.out.println("Error! Please enter an integer greater than -100"); 
       } 

       else if (i > max) 
       { 
        System.out.println("Error! Please enter an integer less than 100"); 
       } 

       else 
        isValid = true; 
     } 

     else 
     System.out.println("Error! Invalid number value"); 
     sc.nextLine(); 

    } 
     // return the int 
     return i; 

} 

public double getDoubleWithinRange(String prompt, double min, double max) 
{ 

    int d = 0 ; 
    boolean isValid = false; 
    while (isValid == false) 
    { 
     System.out.println(prompt); 
     if (sc.hasNextInt()) 
     { 
      //if user chooses menu option less than 1 the program will print an error message 
      d = sc.nextInt(); 
       if (d < min) 
       { 
        System.out.println("Error! Please select menu option 1, 2, or 3"); 
       } 
       //if the user chooses a menu option greater than 3 the program will print an error 
       else if (d > max) 
       { 
        System.out.println("Error! Please select menu option 1, 2, or 3"); 
       } 
       //if the option is between 1 and 3 the menu option is valid 
       else 
        isValid = true; 
     } 

     else 
     System.out.println("Error! Invalid number value"); 
     sc.nextLine(); 

    } 
     // return the int 
     return d; 

} 


public String getRequiredString(String prompt) 
{ 
    return prompt; 

} 

public String getChoiceString(String prompt, String s1, String s2) 
{ 
    return s2; 

} 

public int getInt(String prompt) 
{ 
    return 0; 

} 

}

當我運行此我不斷收到我的最後一次打印這是一個無效的數值。我是不是正確地從其他控制檯的主要方法導入代碼?

+1

與println(String s)和print(String s)沒有任何關係。 – 2012-03-28 23:42:00

+0

@Mike Samuel我剛開始寫代碼,並給了我需要的一些規格,所以我跳進了它。不是最簡潔的寫法,但我想以最簡單最難的順序來處理每種方法。 – 2012-03-28 23:50:26

回答

2

這不是進口問題。如果你導入了錯誤的東西,你的程序不會首先編譯,或者至少不會啓動。

至於實際修理你的問題,我建議你看你認爲與最elsegetIntWithinRange相關的兩條線,並考慮其代碼實際上與else和代碼關聯不大。主要問題是ifelse只與一條線關聯,除非用花括號包圍多條線。所以,你應該修改的getIntWithinRange最後else看起來像

else { 
    System.out.println("Error! Invalid number value"); 
    sc.nextLine(); 
} 

編輯:爲響應更新的問題

我得到下我一個波浪線,告訴我,我我不使用局部變量,所以我不確定在這種情況下究竟修復了什麼,因爲我試圖在另一個類中使用它。我需要創建一個對象嗎?

現在,您所寫的代碼不會在main中使用i。這包括不將它傳遞給任何其他方法或任何構造函數。由於i是本地變量,因此無法在main之外訪問,所以Eclipse會警告您。 Eclipse提供的這種「扭曲的紅線」警告不會阻止你的代碼編譯和運行,但它的目的是幫助你在自己的代碼中發現錯誤。

從更廣泛的角度來看,由於未使用的本地化,出現了很多錯誤,Eclipse希望幫助您防止這些錯誤。考慮想要將二維數組的所有元素初始化爲某個用戶提供的數字(變量名稱比我在實際程序中使用的變量名稱更短的示例,但它們仍然得到該點):

public static final int N = 10; 

public void main(String[] args) { 
    int[][] a = new int[N][N]; 
    int n = /* Read number from the user */; 

    for (int i = 0; i < N; i++) { 
     for (int j = 0; j < N; j++) { 
      a[i][j] = i; // Bug - we meant to initialize to n 
     } 
    } 

    // ... 
} 

Eclipse對未使用的本地人進行標記可幫助您更輕鬆地找到這些類型的錯誤。

+0

謝謝!我認爲它解決了它。我沒有在我的代碼中看到。我剛剛編輯了我在第一堂課注意到的問題(我發佈的第一部分代碼)。我得到的代碼行的問題 int i = c 它告訴我我沒有使用本地變量。我需要關心嗎?我不確定該怎麼做,因爲我認爲我是將這些信息帶入其他班級。 – 2012-03-28 23:48:39

+0

@JeremyB我編輯了我的答案,以解釋爲什麼Eclipse顯示錯誤。一旦你通過將「i」傳遞給構造函數「將這些信息帶入另一個類中」,那條紅色的曲線就會消失。 – 2012-03-28 23:53:39

相關問題