2014-04-13 21 views
1

如何提示用戶循環代碼是循環否退出和錯誤輸入打印錯誤輸入並返回語句即。 「你想輸入另一個名字:」在Java中提示用戶是或否輸入

import java.util.Scanner; 

public class loop { 
public static void main(String[] args){ 
    Scanner kbd = new Scanner (System.in); 
    String decision; 
    boolean yn; 
    while(true){ 

     System.out.println("please enter your name"); 
     String name = kbd.nextLine(); 

     System.out.println("you entered the name" + name); 

     System.out.println("enter another name : yes or no"); 
     decision = kbd.nextLine(); 

     switch(decision){ 
     case "yes": 
      yn = false; 
      break; 
     case "no": 
      yn = true; 
      break; 
     default : 
      System.out.println("please enter again "); 
      return default; 
     } 
    } 
    } 
} 
+0

那你究竟是「循環的代碼是循環......」是什麼意思? – mok

+0

如果用戶輸入是通過代碼從循環開始 – user3443401

回答

2
  1. 如果不使用Java 7,你不能使用開關串
  2. 變化while (true)while (yn)所以它會停止時他鍵入「否」,並將boolean yn;更改爲boolean yn = true; 並更改案例中的規則。

    case "yes": 
        yn = false; 
        break; 
    case "no": 
        yn = true; 
        break; 
    

    yn = true;如果 「是」;

    yn = false; if「no」;

    你可以改變內部的條件,用while (!yn),但更直觀的讓yntrue如果是的話;如果沒有,則爲false

  3. return default;沒有多大意義,如果你想要讓錯誤的情況下,用戶重複好..你應該做一個新的while (true)重複,直到他寫道正確的。我會寫另一種方法。

這是你如何能做到這一點

Scanner kbd = new Scanner (System.in); 

String decision; 

boolean yn = true; 
while(yn) 
{ 
    System.out.println("please enter your name"); 
    String name = kbd.nextLine(); 

    System.out.println("you entered the name" + name); 

    System.out.println("enter another name : yes or no"); 
    decision = kbd.nextLine(); 


    switch(decision) 
    { 
     case "yes": 
      yn = true; 
      break; 

     case "no": 
      yn = false; 
      break; 

     default: 
      System.out.println("please enter again "); 
      boolean repeat = true; 

      while (repeat) 
      { 
       System.out.println("enter another name : yes or no"); 
       decision = kbd.nextLine(); 

       switch (decision) 
       { 
        case "yes": 
         yn = repeat = true; 
         break; 

        case "no": 
         yn = repeat = false; 
         break; 
       } 
      } 
      break; 
    } 
} 

是的,它會重複decision代碼,但它是如何創建的,我認爲這是應該做的唯一途徑。

2

是的,但你想要while(yn),不while(true),這繼續下去。 break只從switch聲明中突破。


好吧,試試這個:

import java.util.Scanner; 

public static void main(String args[]){ 
    Scanner s = new Scanner(System.in); 

    boolean checking = true, valid = true; 
    String[] names = new String[50]; 
    int i = 0; 

    while(checking){ 
    System.out.println("Enter name..."); 
    me = s.nextLine(); 
    System.out.println("You entered " + me + "."); 
    while(valid){ 
     System.out.println("Enter another? y/n"); 
     you = s.nextLine(); 
     if(you.equals("n")){ 
     valid = false; 
     checking = false; 
     }else if you.equals("y")){ 
     names[i] = you; 
     i++; 
     valid = false; 
     }else{ 
     System.out.println("Sorry, try again (y/n)..."); 
     } 
    } 
    } 
} 
+0

我只需要循環最後一個判定語句,例如 如果用戶輸入是肯定比循環語句 如果用戶輸入否比退出語句 如果輸入錯誤比循環輸入輸入是或否 – user3443401

+0

@ user3443401好吧,看看我添加的作品。 – AstroCB

1
boolean input = true; 
while(input){ 
//ask for name 
//print message saying you entered xyz 
//ask if user wants to enter other name 
//if user enters no 
//set input = false; 
//else continue 
} 

嘗試這樣做

+0

錯誤的用戶輸入 – user3443401

+0

那麼它取決於你想如何實現它,如果你想退出的用戶輸入錯誤,你可以打破循環,否則你可以讓用戶在閱讀他的輸入後再次輸入它,如果它是錯誤 –

1

而是再突破,雖然(真),你需要說while(!yn)爲您YN設置爲false,當用戶輸入肯定的。

+0

錯誤的用戶輸入信息 – user3443401

+0

你現在用'return default'但我想你只是想循環,所以'打破';' – jedison

+0

你能爲我編碼嗎?因爲在默認情況下使用中斷讓我回到代碼開始輸入名稱 – user3443401

0

我剛剛創建了一個小班YesNoCommandPrompt是做到了這一點:

private String prompt; 
private Supplier<T> yesAction; 
private Supplier<T> noAction; 

public YesNoCommandPrompt(String prompt, Supplier<T> yesAction, Supplier<T> noAction) { 
    this.prompt = prompt; 
    this.yesAction = yesAction; 
    this.noAction = noAction; 
} 

// example usage 
public static void main(String[] args) { 
    YesNoCommandPrompt<String> prompt = new YesNoCommandPrompt<>(
      "Choose", 
      () -> {return "yes";}, 
      () -> {return "no";}); 

    System.out.println(prompt.run()); 
} 

public T run() { 
    final String yesOption = "y"; 
    final String noOption = "n"; 
    try (Scanner scanner = new Scanner(System.in)) { 
     while (true) { 
      System.out.print(prompt + " [" + yesOption + "/" + noOption + "]: "); 
      String option = scanner.next(); 
      if (yesOption.equalsIgnoreCase(option)){ 
       return yesAction.get(); 
      } 
      else if (noOption.equalsIgnoreCase(option)){ 
       return noAction.get(); 
      } 
     } 
    } 
}