[英]JOptionPane.showInputDialog loop (using do while loop)
我正試圖要求用戶提供4到10之間的整數。 如果他們的答案超出了那個范圍,它就會進入一個循環。 當用戶第一次正確輸入數字時它不會中斷並繼續執行else語句。 如果用戶在else語句中正確輸入數字,它將正確中斷。
如果第一次輸入正確,我怎么能讓它破裂呢?
**我是java的初學者,如果我在這里缺少傻話,請道歉。
public int companySize() {
int ansCompany;
do {
ansCompany = Integer.parseInt(JOptionPane.showInputDialog
("Please input the company size"));
if ( ansCompany <= 4 && ansCompany <= 10 ) {
break;
} else {
ansCompany = Integer.parseInt(JOptionPane.showInputDialog
("Please enter a valid company size"));
if ( ansCompany <= 4 && ansCompany <= 10 ) {
break;
} // ends nested if condition
} //ends else
}//ends do
while ( ansCompany < 4 || ansCompany > 10);
return ansCompany;
}// ends public int companySize()
我從main調用它如下:
public static void main(String[] args) {
UserInput getResult = new UserInput();
int company_size = getResult.companySize();
}// ends main
如果用戶第一次寫錯了值,我不確定為什么你需要兩個相同的對話框,因為最后只返回一個值(ansCompany)。
通過將do-while語句設置為break條件(小於4或大於10),它將循環直到用戶輸入正確的數字。
public int companySize() {
int ansCompany;
do {
ansCompany = Integer.parseInt(JOptionPane.showInputDialog
("Please input the company size"));
} while (ansCompany < 4 || ansCompany > 10);
return ansCompany;
}
while ( ansCompany < 4 || ansCompany > 10);
是低於3 或高於11的任何東西
你想要的while ( ansCompany >= 4 && ansCompany <= 10);
注意 :你想要這個選擇^^^^的原因是因為如果輸入大於>或等於=使> =比4和更高的意思。 同樣地,小於<或等於=使<=比意味着小於10的任何東西
|| 是指OR 。 這意味着輸入必須大於4或小於10.如果答案是11,則它傳遞第一個條件,因此傳遞if語句。 與3相同,它通過小於10的條件。
&&表示AND ,因此必須通過第一個條件和第二個條件。
從這個意義上說,你的if語句也是錯誤的;
if ( ansCompany <= 4 && ansCompany <= 10 ) {
應該
if ( ansCompany >= 4 && ansCompany <= 10 ) {
我寧願使用遞歸函數:
版本1:簡而言之:
public int companySize() {
final int result = Integer.parseInt(JOptionPane.showInputDialog("Please input the company size"));
return (result >= 4 && result <= 10) ? result : companySize();
}
版本2:但您也可以使用靜態常量
/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";
public int companySize() {
final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
return (result >= MIN && result <= MAX) ? result : companySize();
}
版本3:如果您想顯示錯誤消息:
/* don't be afraid of shared constants; sometimes they're very "useful idiots" */
/*public*/ static final int MIN = 4;
/*public*/ static final int MAX = 10;
/*public*/ static final String MESSAGE = "Please input the company size";
/*protected*/ static final String ERROR_MESSAGE = "Please input a valid company size";
public int companySize() {
final int result = Integer.parseInt(JOptionPane.showInputDialog(MESSAGE));
return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}
private int companySize(String errorMessage) {
final int result = Integer.parseInt(JOptionPane.showInputDialog(errorMessage));
return (result >= MIN && result <= MAX) ? result : companySize(ERROR_MESSAGE);
}
和
public static void main(String[] args) {
UserInput getResult = new UserInput();
int company_size = getResult.companySize();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.