2012-03-31 14 views
1

我正在學習包裝。我有兩個不同包中的類,我似乎對我的輸出有問題。使用單獨的程序包輸出程序

這裏與主類: 我的兩個問題是與底部兩節「需要串測試」和「字符串選擇測試」

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(); 

    // double 
    c.println("Double Test"); 
    double d = c.getDoubleWithinRange("Enter any number between -100 and 100: ", -101, 101); 
    c.println(); 

    // required string 
    c.println("Required String Test"); 
    String s1 = c.getRequiredString("Enter your email address: "); 
    c.println(); 

    // string choice 
    c.println("String Choice Test"); 
    String s2 = c.getChoiceString("Select one (x/y): ", "x", "y"); 
    c.println(); 
} 
} 

這裏是他們正在訪問的類的代碼。 (這是在主要的頂級進口的Console類)

package myUtils.util; 
import java.util.Scanner; 

public class Console 
{   
Scanner sc = new Scanner(System.in); 

/******************************************************/ 
public void println() 
{  

} 

/******************************************************/ 
public void print(String s) 
{ 
    System.out.println(s); 
} 

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

/*****************************************************/ 
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 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()) 
     {    
      d = sc.nextInt(); 
       if (d < min) 
       { 
        System.out.println ("Error! Please enter a number greater than -100"); 
       } 
       else if (d > max) 
       { 
        System.out.println ("Error! Please enter a number less than 100"); 
       } 
       else 
        isValid = true; 
     } 
     else 
     { 
      System.out.println("Error! Invalid number value"); 
      sc.nextLine(); 
     } 
    } 
    return d; 
} 

/*****************************************************/ 
public String getRequiredString(String prompt) 
{ 
    String R = ""; 
    boolean isValid = false; 
    while (isValid == false) 
    { 
     System.out.print(prompt); 
     R = sc.nextLine(); 
     if (R.equals("")) 
     { 
      System.out.println("Error! This entry is required. Try again."); 
     } 
     else 
     { 
      isValid = true; 
     } 
    } 
    return R; 
} 

public String getChoiceString (String prompt, String s1, String s2) 
{ 
    String s = ""; 
    boolean isvalid = false; 
    while (isvalid == false); 
    {   
     s = this.getChoiceString(prompt, s1, s2); 
     System.out.println(s); 
     s = sc.nextLine(); 
     if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2)) 
     { 
      System.out.println("Error! Entry must be '"+ 
       s1 +"', '"+ s2 +"', or '."+ "Try again.");  
     } 
     else 
     { 
      isvalid = true; 
     } 
    } 
    return s; 
}  

public int getInt(String prompt) 
{ 
    return 0; 
} 
} 

我與getRequiredString遇到的問題是我有一個行代碼,說

R = sc.nextLine(); 

這導致我的輸出打印我的錯誤消息,因爲它認爲用戶沒有輸入任何東西。當我將其更改爲簡單

R = sc.next(); 

代碼不會讓用戶輸入任何內容。我不確定如何解決這個問題。

我的getChoiceString問題是程序不打印任何東西。看起來這段代碼不會打印我的提示或任何其他元素。

我是新來的,這是家庭作業,所以任何提示,線索,或幫助走過我這個讚賞。

+0

請您分享一下''Console'類的getDoubleWithinRange'和'getIntWithinRange'方法。 – 2012-03-31 14:44:52

+0

@Korhan Ozturk當然,我現在將編輯。 – 2012-03-31 14:46:42

+0

@KorhanÖztürk我編輯了問題並將它們添加回控制檯類。你現在應該看到他們。 – 2012-03-31 14:49:30

回答

2

哎呀!你在你的while語句之後放了一個分號,這會導致程序停止。

while (isvalid == false); <--- 
{ 
    .... 
} 

我刪除它,發現你的代碼進入無限循環,因爲getChoiceString方法調用自身不必要的。因此我也編輯了它(註釋掉你的遞歸調用),它現在似乎正在工作!請嘗試以下操作:

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

    String s = ""; 
    boolean isvalid = false; 
    while (true) 
    { 
     if(isvalid == true) 
     { 
      break; 
     } 
     //s = this.getChoiceString(prompt, s1, s2); 
     //System.out.println(s); 
     System.out.println("Enter your String: "); 
     s = sc.nextLine(); 
     if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2)) 
     { 
      System.out.println("Error! Entry must be '"+ 
       s1 +"', '"+ s2 +"', or '."+ "Try again."); 


     } 
     else 
     { 
      isvalid = true; 
     } 
    } 
    return s; 

} 

編輯:另外在你的getRequiredString方法,使用R = sc.next();代替R = sc.nextLine();爲了使你的程序正常工作。請檢查以下內容:

System.out.print(prompt); 
R = sc.next(); 
System.out.println("Email address: " + R.toString()); 
if (R.equals("")) 
{ 
    System.out.println("Error! This entry is required. Try again."); 
} 
else 
{ 
    isValid = true; 
} 
+0

謝謝!我還能做些什麼來解決這個問題嗎? getRequiredString中的nextLine? – 2012-03-31 15:07:05

+0

我想我已經修復了這個問題。現在用戶可以輸入一個字符串,程序檢查並輸出一條錯誤信息,說用戶輸入應該等於s1或s2。親自試一試 – 2012-03-31 15:09:59

+0

哦,我正在談論getRequiredStringMethod。我認爲它的編碼是正確的,但由於某種原因,當我的程序顯示該方法的輸出時,它假定用戶只是按下回車鍵並沒有輸入任何內容。 – 2012-03-31 15:22:41