2012-11-04 42 views
1

所以我要求用戶一個月和一年。月份必須是十二個月中的一個,年份必須是數字,而不是字母。我試圖找出讓程序說「錯誤的輸入,再試一次」並提示再次輸入的最佳方法。以下是我正在爲月份部分使用的代碼部分。在java輸入錯誤後詢問用戶另一個提示

public class MonthLength { 
    public static void main(String[] args) { 

    int month = 0; 
    // Prompt the user to enter a month 
    SimpleIO.prompt("Enter a month name: "); 
    String userInput = SimpleIO.readLine(); 

     if (userInput.trim().toLowerCase().equals("january")) { 
     month = 1; 
     } else if (userInput.trim().toLowerCase().equals("february")) { 
     month = 2; 
     } else if (userInput.trim().toLowerCase().equals("march")) { 
     month = 3; 
     } else if (userInput.trim().toLowerCase().equals("april")) { 
     month = 4; 
     } else if (userInput.trim().toLowerCase().equals("may")) { 
     month = 5; 
     } else if (userInput.trim().toLowerCase().equals("june")) { 
     month = 6; 
     } else if (userInput.trim().toLowerCase().equals("july")) { 
     month = 7; 
     } else if (userInput.trim().toLowerCase().equals("august")) { 
     month = 8; 
     } else if (userInput.trim().toLowerCase().equals("september")) { 
     month = 9; 
     } else if (userInput.trim().toLowerCase().equals("october")) { 
     month = 10; 
     } else if (userInput.trim().toLowerCase().equals("november")) { 
     month = 11; 
     } else if (userInput.trim().toLowerCase().equals("december")) { 
     month = 12; 
     } 


    // Terminate program if month is not a proper month name 
    if (month < 1 || month > 12) { 
     System.out.println("Illegal month name; try again"); 
     return; 
    } 

這裏就是我與當年部分工作:

// Prompt the user to enter a year 
    SimpleIO.prompt("Enter a year: "); 
    userInput = SimpleIO.readLine(); 
    int year = Integer.parseInt(userInput); 

    //Here, trying to use hasNextInt to make sure input is an integer 
    //If it's not, need to give an error message and prompt input again 
    // public boolean hasNextInt() 

    //Prompt input again if year is negative 
    if (year < 0) { 
     System.out.println("Year cannot be negative; try again"); 
     return; 
    } 

    // Determine the number of days in the month 
    int numberOfDays; 
    switch (month) { 
     case 2: // February 
       numberOfDays = 28; 
       if (year % 4 == 0) { 
       numberOfDays = 29; 
       if (year % 100 == 0 && year % 400 != 0) 
        numberOfDays = 28; 
       } 
       break; 

     case 4: // April 
     case 6: // June 
     case 9: // September 
     case 11: // November 
       numberOfDays = 30; 
       break; 

     default: numberOfDays = 31; 
       break; 
    } 

    // Display the number of days in the month 
    System.out.println("There are " + numberOfDays + 
         " days in this month"); 
    } 
} 

看到的代碼後,我敢肯定,這將是更加明確我要問什麼。如果他們輸入的不是一個月的單詞,請提示他們並再次要求輸入。同樣的事情,如果他們進入一個不是整數的年份。提前致謝!

+0

爲了更好地幫助越早,張貼[SSCCE ](http://sscce.org/)。 –

回答

1

你必須做的是一個循環,它會一直提示用戶,直到嘴被正確插入。在以下幾行:

boolean correct_mouth = false; // Control variable 

while(!correct_mouth) 
{ 
    int month = 0; 
    // Prompt the user to enter a month 
    SimpleIO.prompt("Enter a month name: "); 
    String userInput = SimpleIO.readLine(); 

    ... 
    // If the mouth is indeed correct 
    // then correct_mouth = true; 
} 

然後你應用相同的想法來多年。

而不必在每月所有這些條件,我認爲這是最好 所有月份字符串添加到

ArrayList <String> all_mouths = new ArrayList <String>(); 

然後你只需要使用all_mouths.indexOf的字符串由用戶插入。如果它返回-1這個字符串不是一個有效的嘴,否則它會給你表示嘴的位置(你必須+1)。例如

mouth = all_mouths.indexOf(userInput); 
if(mouth != -1) correct_mouth = true; 

因此,完整的解決方案會是這樣的:

ArrayList <String> all_mouths = new ArrayList <String>(); 
all_mouths.add("january"); 
... // and so one 

int month = 0; // Control variable 

while(mouth <= 0) 
{ 

    // Prompt the user to enter a month 
    SimpleIO.prompt("Enter a month name: "); 
    String userInput = SimpleIO.readLine(); 

    mouth = all_mouths.indexOf(userInput.trim().toLowerCase()) + 1; 
} 
5

在一個循環中運行它,會做:

String userInput; 
int month; 
do{ 
    SimpleIO.prompt("Enter a month name: "); 
    userInput = SimpleIO.readLine(); 
    try{ 
     month = Integer.parseInt(userInput); 
    } catch(NumberFormatException e){ 
     continue; 
    } 
}while(month <= 0 || month > 12); 
相關問題