2012-11-23 50 views
1
import java.util.Scanner; 
public class InteractiveRectangle 
{ 
public static void main(String[] args) 
{ 
    do 
    { 
     int length = readInteger ("For Length "); 
     System.out.println(); 
     int width = readInteger ("For Width "); 
     printRectangleDetails(length,width);// existing code goes here 
    } 
    while (keepGoing()); 

    System.out.println("Goodbye, friend"); 
} 


/** 
* returns the details of the rectangle 
* @param height the height of the rectangle 
* @param width the width of the rectangle 
*/ 
public static void printRectangleDetails (int length, int width) 
{ 
    System.out.println ("This is the length of the rectangle " + length); 

    System.out.println ("This is the width of the rectangle " + width); 

    System.out.println (("This is the perimeter of the rectangle " + (length + width))); 

    System.out.println (("This is the area of the rectangle " + (length * width))); 
} 

/** 
* Read in an integer and return its value 
* @param the prompt to be shown to the user 
*/ 
public static int readInteger(String prompt) 
{ 
    System.out.println (prompt); 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Enter an integer"); 

    while (!scan.hasNextInt()) // while non-integers are present 
    { 
     scan.next(); 
     System.out.println ("Bad input. Enter an integer."); 
    } 
    int input = scan.nextInt(); 
    return input; 
} 

/** 
* Read a positive integer and return its value 
* @param the prompt to be shown to the user 
*/ 
public static int readPositiveInteger(String prompt) 
{ 
    System.out.println (prompt); 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Enter an integer"); 
    boolean positive = false; 

    while (scan.hasNextInt() && positive == false) 
    { 
     int input = scan.nextInt(); 
     if (input > 0) 
     { 
      positive = true; 
      { 
       return input; 
      } 
     } 
     else 
     { 
      System.out.println ("Bad input enter an integer."); 
      positive = false; 
      scan.nextLine(); 

     } 

    } 
    return 0; 
} 

/** 
* Ask the user whether or not to spawn another rectangle 
* and returns the result as a boolean 
*/ 
public static boolean keepGoing()  
{ 
    Scanner scan = new Scanner(System.in); 
    boolean inputRead = false; 
    boolean result = false; 
    System.out.println ("Do you want to process another rectangle?"); 
    scan.next(); 
    String input = scan.next(); 

    if (input == "y") 
    { 
     inputRead = true; 
     result = true; 

    } 
    else if (input == "n") 
    { 
     inputRead = true; 
     result = false; 

    } 
    else 
    { 
     System.out.println("Bad input please try again!"); 
     scan.nextLine(); 
    } 
    return result; 

} 

}方法 - 繼續下去

我想要的程序詢問用戶是否要產生另一個矩形,並繼續下去,直到用戶回答與「N」這個問題。 Atm運行程序時,矩形只產生一次,所以我認爲我的keepGoing方法存在問題。 任何幫助,將不勝感激, 謝謝!

回答

2

是有幾個問題: -

  • 首先使用的是==運營商,這將永遠是假的比較字符串。使用equals方法: -

    if (input.equals("y")) // or, if (input.equalsIgnoreCase("y")) 
    
  • 其次,你不應該使用scan.next()方法,您使用的方式。你的第一個scan.next應該分配給input,作爲你的第二scan.next包含換行符: -

    System.out.println ("Do you want to process another rectangle?"); 
    // scan.next(); // Should not be here. 
    String input = scan.next(); 
    scan.next();  // Change the order 
    

    或者,只是使用scan.nextLine(): -

    System.out.println ("Do you want to process another rectangle?"); 
    String input = scan.nextLine(); 
    
  • 第三,在你的else部分,你可以再次調用您的keepGoing方法,而不是在那裏讀取輸入: -

    else 
    { 
        System.out.println("Bad input please try again!"); 
        return keepGoing(); 
    } 
    
  • 另外,在您的if-else中,不要將布爾值設置爲變量,您可以直接從那裏返回。

所以,在所有的,你可以改變你if-else if-else到: -

if (input.equals("y")) { 
    return true; 
} 
else if (input.equals("n")) { 
    return false; 
} 
else 
{ 
    System.out.println("Bad input please try again!"); 
    return keepGoing(); 
} 

然後,你不需要那些boolean變量: - inputReadresult。只要刪除它們。並從method的末尾刪除return聲明。現在它將是unreachable code

+0

謝謝,你所說的話是有道理的。但是,有一件事,當它詢問我是否想要產生另一個矩形時,我必須先輸入'y'然後輸入'y',然後才能運行,有什麼想法? –

+0

是的,因爲你沒有在你的輸入中存儲'first scan.next()'。看到我的第二點。當用'scan.next()'讀取時,第一個包含'input',第二個'scan.next()'包含'newline'。 –

+0

現在啊,工作得很好,非常感謝! –

4

if (input == "y")

總是equals()

比較字符串需要

if ("y".equals(input))

if ("y".equalsIgnoreCase(input)) // will also allow Y

相應地更改其他檢查。

1

總是比較.equals字符串()

if (input == "y") 

更換到

if (input.equals("y")) 
1

兩個字符串使用==運營商都是平等的您正在檢查。總是使用equals方法檢查字符串是否相等。

if (input == "y") 

應該

if (input.equals("y")) 

,並在地方休息爲好,

==運營商檢查,如果兩個String引用指向同一個String對象。 equals方法確定兩個String對象是否有意義相等。

1

此代碼:

if (input.equals("y")) 
{ 
    inputRead = true; 
    result = true; 

} 
else if (input.equals("n")) 
{ 
    inputRead = true; 
    result = false; 

} 

應該解決的問題。請記住,Java中的對象通過引用進行比較,而不是通過值進行比較