2015-05-07 28 views
0

基本上我需要循環這個。我創建了一個遊戲,您可以在runescape中回答關於魚的問題,並且我希望它能像說'再去一次'那樣循環。我會怎麼做呢?我應該如何循環這個程序?

import java.util.Scanner; 
public class GameDriver 
{ 
    public static void main(String a[]) 
{ 
    Game g = new Game(); 
    Scanner s = new Scanner(System.in); 
    String buf = null; 

    for(int i = 0; i < 4; i++) 
    { 
    System.out.print(g.askQuestion()); 
    buf = s.nextLine(); 

    if(g.confirm(buf)) 
     System.out.println("You're Right!"); 
    else 
     System.out.println("You're Wrong..."); 
    } 

    System.out.println("You got a " + g.getScore()); 
    } 
} 

回答

0

PFB完整的代碼爲「你又來了」計劃:

import java.util.Scanner; 

public class GameDriver { 

    public static void Gamer(Scanner s) { 

     Game g = new Game(); 

     String buf = null; 

     for (int i = 0; i < 4; i++) { 
      System.out.print(g.askQuestion()); 
      buf = s.nextLine(); 

      if (g.confirm(buf)) 
       System.out.println("You're Right!"); 
      else 
       System.out.println("You're Wrong..."); 
     } 

     System.out.println("You got a " + g.getScore()); 

    } 

    public static void main(String a[]) { 

     Scanner s = new Scanner(System.in); 

     try { 

      while (true) { 
       System.out.print("Press Enter to continue; Type Exit to quit"); 

       if (s.nextLine().equalsIgnoreCase("exit")) { 
        s.close(); 
        break; 
       } 

       Gamer(s); 
      } 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 
+0

@ jake-wilks運行這是原樣並讓我們知道結果。 – Rajesh

+0

工作完美謝謝!!!! –

+0

@ jake-wilks您可以請投票/標記爲解決方案嗎? [接受答案] stackoverflow.com/help/accepted-answer; [投票] stackoverflow.com/help/why-vote – Rajesh

2

你可以做,而(真)循環,然後如果用戶輸入退出剛剛突破,像這樣的循環:

if (buf.equals("quit")) 
    break; 
+0

我會在哪裏進入,在我的碼? –

+0

你用while循環替換for循環,然後再做g.confirm驗證用戶是否進入「退出」 –

+0

你想看看我的其他代碼嗎? –

0

按我理解你的問題,你要循環的對如果我錯了,請仔細糾正我。

char c='y';//'y' means user wants to go again 
while(c=='y')// to check the user reply. 
{ 

    for(int i = 0; i < 4; i++) 
    { 
     System.out.print(g.askQuestion()); 
     buf = s.nextLine(); 

     if(g.confirm(buf)) 
      System.out.println("You're Right!"); 
     else 
      System.out.println("You're Wrong..."); 
     } 

     System.out.println("You got a " + g.getScore()); 
     System.out.println("Do you wanna go again?"); 
     c=s.next.charAt(0); 
} 
0

這是最短的解決方案讓您的所有代碼邏輯是:

import java.util.Scanner; 

public class GameDriver { 

    public static void Gamer(Scanner s) { 

     Game g = new Game(); 

     String buf = null; 

     for (int i = 0; i < 4; i++) { 
      System.out.print(g.askQuestion()); 
      buf = s.nextLine(); 

      if (g.confirm(buf)) 
       System.out.println("You're Right!"); 
      else 
       System.out.println("You're Wrong..."); 
     } 

     System.out.println("You got a " + g.getScore()); 

     System.out.print("Press Enter to continue; Type Exit to quit"); 

     if (s.nextLine().equalsIgnoreCase("exit")) { 
      s.close(); 
      System.exit(0); 
     } 

     Gamer(s); 
    } 

    public static void main(String a[]) { 
     Gamer(new Scanner(System.in)); 
    } 
} 
+0

@ jake-wilks按原樣運行此代碼並讓我們知道結果。 – Rajesh

+0

我用這個謝謝你,現在我必須將繼承添加到我的整體程序中,你可以幫我解決這個問題嗎? –