我应该如何循环这个程序?

时间:2015-05-07 19:26:22

标签: java loops

基本上我需要循环这个。我创建了一个游戏,你在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());
  }
 }

4 个答案:

答案 0 :(得分:2)

你可以做while(true)循环然后如果用户进入退出就像这样打破循环:

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

答案 1 :(得分:0)

据我理解你的问题,你想循环for循环。如果我错了,请纠正我。

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);
}

答案 2 :(得分: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();
        }

    }
}

答案 3 :(得分: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));
    }
}