2013-10-28 52 views
0

我正在處理一個java入門介紹的分配問題,並且在用戶需要輸入多個輸入時遇到了一些困難。問題如下:詢問用戶是否需要輸入多個輸入

「要求用戶輸入一個數字,你應該使用一個輸入對話框來輸入這個數據,一定要把對話框中的字符串轉換成一個實數,程序需要跟蹤用戶輸入的最小號碼以及輸入的最大號碼,詢問用戶是否要輸入另一個號碼,如果是,則重複該過程,如果不是,輸出用戶輸入的最小號碼和最大號碼

此程序輸出的最大和最小數量的AT當用戶想退出程序的結束。

而且,你的程序應該考慮的情況下,當用戶只輸入一個NUM BER。在這種情況下,最小和最大的數字將是相同的。「

我的問題是,我無法弄清楚如何讓程序不斷詢問用戶是否要輸入另一個數字....對於許多(明顯),我知道我將不得不使用循環或什麼,但我是一個初學者,不知道從哪裏開始,任何幫助將不勝感激,並提前致謝!

這是我到目前爲止有:

包findingminandmax;

進口javax.swing.JOptionPane中;

公共類Findingminandmax {

public static void main(String[] args) 
{ 
     String a = JOptionPane.showInputDialog("Input a number:"); 
     int i = Integer.parseInt(a); 

     String b = JOptionPane.showInputDialog("Would you like to input another number? yes or no"); 

     if ("yes".equals(b)) { 
     String c = JOptionPane.showInputDialog("Input another number:"); 
     int j = Integer.parseInt(c); 

     int k = max(i, j); 

     JOptionPane.showMessageDialog(null, "The maximum between " + i + 
       " and " + j + " is " + k); 

    } else { 
      JOptionPane.showMessageDialog(null, "The maximum number is " + i); 
     } 

} 

public static int max(int num1, int num2) { 
    int result; 

    if (num1 > num2) 
     result = num1; 
    else 
     result = num2; 

    return result; 
} 

}

+0

看看[的同時和do-while語句(http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) – MadProgrammer

+0

考慮一個'做{}而();'或提示他們輸入stop或類似的東西,因爲你正在使用字符串。 – ChiefTwoPencils

回答

0
while(true) { 
    //do stuff 
    if ("yes".equals(b)) { 
     //do other stuff 
    } else { break; } 
} 
1
String b = JOptionPane.showInputDialog("Would you like to input another number? yes or no"); 
while(b.equalsIgnoreCase("yes")){ 
    String c = JOptionPane.showInputDialog("Input another number:"); 

    // your logic 
    b = JOptionPane.showInputDialog("Would you like to input another number? yes or no"); 
}    
    // then have your logic to print maximum and minimum number 

但要獲得是/否投入使用確認對話框,而不是輸入對話框

例如

int b = JOptionPane.showConfirmDialog(null, "Would you like to input another number? yes or no", "More Inputs", JOptionPane.YES_NO_OPTION); 
while (b == JOptionPane.YES_OPTION) { 
    // your logic 
}