2017-02-27 98 views
-1

在demo方法在一次迭代後輸入爲'y'後調用後,它應該再次請求繼續嗎? (y/n)請幫助!! Thnxx提前調用函數或返回主方法

public void demo() 
     { 
      System.out.println("Withdrawal or deposit? (w/d) :"); 
      char choice = s.next().charAt(0); 
      System.out.println("Checking or Savings? (c/s) :"); 
      char choicetwo = s.next().charAt(0); 
      System.out.println("Amount? : "); 
      int amt = s.nextInt(); 
     } 

    public static void main(String[] args) 
    { 
     Scanner s = new Scanner(System.in); 
     Child c = new Child(); 
     c.display(); 
     c.demo(); 
     System.out.println("Continue? : "); 
     char input = s.next().charAt(0); 
     while(input == 'y') 
     { 
      c.demo(); 
     } 

回答

0
System.out.println("Continue? : "); 
    char input = s.next().charAt(0); 
    while(input == 'y') 
    { 
    c.demo(); 
    System.out.println("Continue? : "); 
    input = s.next().charAt(0); 
    } 
+0

儘管這段代碼可以解決這個問題,[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)真的有幫助以提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan

0

你需要把變量賦值到while循環:

 System.out.println("Continue? : "); 
     char input = s.next().charAt(0); 
     while(input == 'y') 
     { 
      c.demo(); 
      System.out.println("Continue? : "); 
      input = s.next().charAt(0);     
     } 
+0

哎呀!我怎麼能錯過thankxx @ninnemannk –