我正在做作業,我很近,但我遇到問題。我剛剛學會了如何在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;
}
}
當我運行此我不斷收到我的最後一次打印這是一個無效的數值。我是不是正確地從其他控制檯的主要方法導入代碼?
與println(String s)和print(String s)沒有任何關係。 – 2012-03-28 23:42:00
@Mike Samuel我剛開始寫代碼,並給了我需要的一些規格,所以我跳進了它。不是最簡潔的寫法,但我想以最簡單最難的順序來處理每種方法。 – 2012-03-28 23:50:26