2016-11-16 25 views
0

我想在某個時候重新啓動程序,如果輸入了錯誤的電子郵件地址,用戶將不必完全重新啓動程序,以防他們拼寫錯誤,並且didn沒有注意到。這是我迄今爲止如何讓程序重新啓動後不正確的輸入java

public static void main(String[] args) { 
    String grade = JOptionPane.showInputDialog(null, "Please Specify Your Grade"); 
    String First_name = JOptionPane.showInputDialog(null, "What is your First Name?"); 
    String Last_name = JOptionPane.showInputDialog(null, "What is your Last Name?"); 
    String message = "You are a " + grade + "\n" 
      + "Your Name is " + First_name + " " + Last_name; 
    JOptionPane.showMessageDialog (null, message); 
    String email = JOptionPane.showInputDialog (null, "enter email"); 
     if (email.contains("@branfordschools.org")){     
      JOptionPane.showMessageDialog(null,"Password Accepted"); 
     } else { 
      JOptionPane.showMessageDialog(null,"Password Incorrect, Program Closing"); 
      System.exit(0); 
     } 

鑑於Java沒有GOTO(要去使用它,直到它引起的問題,程序是隻爲一個畢業演講)我將如何去使它回去這裏?

String email = JOptionPane.showInputDialog (null, "enter email"); 
     if (email.contains("@branfordschools.org")){     
      JOptionPane.showMessageDialog(null,"Password Accepted"); 
     } else { 
      JOptionPane.showMessageDialog(null,"Password Incorrect, Program Closing"); 
      System.exit(0); 
+0

使用do-雖然loop.Google it.Basic東西。 – Inconnu

+0

您正在尋找的概念被稱爲**循環**。換句話說:一些內置的替代gotos。例如,請參閱http://stackoverflow.com/questions/10834202/do-while-syntax-for-java – GhostCat

+0

您可能需要endsWith()而不是contains。因爲您當前的代碼邏輯會使green @ brandfordschool.orgRANDOMSTUFF無任何問題。 – DejaVuSansMono

回答

2

下面是一個簡單的while循環,將繼續要求電郵,直到它包含 「@ branfordschools.org」

String email; 
    Boolean validEmail = False; 

    while(!validEmail) 
    { 
     //ask the user for the email 
     email = JOptionPane.showInputDialog (null, "enter email"); 
     if (email.contains("@branfordschools.org")){     
      JOptionPane.showMessageDialog(null,"Password Accepted"); 
      validEmail = True; 
     } else { 
      JOptionPane.showMessageDialog(null,"Password Incorrect, Please Re-Enter Password"); 
     } 
    } 
0

這應該工作;

boolean validInput = false; 
    while (!validInput) { 
     String email = JOptionPane.showInputDialog(null, "enter email"); 
     if (email.contains("@branfordschools.org")) { 
      validInput = true; 
      JOptionPane.showMessageDialog(null, "Password Accepted"); 
     } else { 
      JOptionPane.showMessageDialog(null, "Password Incorrect, Program Closing"); 
     } 
    } 
相關問題