Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我应该如何循环这个程序?_Java_Loops - Fatal编程技术网

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

Java 我应该如何循环这个程序?,java,loops,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;

基本上我需要循环这个。我创建了一个游戏,在这个游戏中,你可以回答关于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());
  }
 }
import java.util.Scanner;
公共类游戏驱动程序
{
公共静态void main(字符串a[]
{
游戏g=新游戏();
扫描仪s=新的扫描仪(System.in);
字符串buf=null;
对于(int i=0;i<4;i++)
{
System.out.print(g.askQuestion());
buf=s.nextLine();
如果(g.确认(buf))
System.out.println(“你说得对!”);
其他的
System.out.println(“你错了…”);
}
System.out.println(“您得到了一个”+g.getScore());
}
}
您可以执行while(true)循环,然后如果用户输入quit,只需按如下方式中断循环:

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

根据我理解你的问题,你想在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);
}
charc='y';/'“y”表示用户希望再次访问
while(c=='y')//检查用户回复。
{
对于(int i=0;i<4;i++)
{
System.out.print(g.askQuestion());
buf=s.nextLine();
如果(g.确认(buf))
System.out.println(“你说得对!”);
其他的
System.out.println(“你错了…”);
}
System.out.println(“您得到了一个”+g.getScore());
System.out.println(“你想再来一次吗?”);
c=s.next.charAt(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();
        }

    }
}
import java.util.Scanner;
公共类游戏驱动程序{
公共静态无效玩家(扫描器){
游戏g=新游戏();
字符串buf=null;
对于(int i=0;i<4;i++){
System.out.print(g.askQuestion());
buf=s.nextLine();
如果(g.确认(buf))
System.out.println(“你说得对!”);
其他的
System.out.println(“你错了…”);
}
System.out.println(“您得到了一个”+g.getScore());
}
公共静态void main(字符串a[]{
扫描仪s=新的扫描仪(System.in);
试一试{
while(true){
System.out.print(“按Enter继续;键入Exit退出”);
如果(s.nextLine().equalsIgnoreCase(“退出”)){
s、 close();
打破
}
玩家;
}
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

这是保持所有代码逻辑不变的最短解决方案:

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));
    }
}
import java.util.Scanner;
公共类游戏驱动程序{
公共静态无效玩家(扫描器){
游戏g=新游戏();
字符串buf=null;
对于(int i=0;i<4;i++){
System.out.print(g.askQuestion());
buf=s.nextLine();
如果(g.确认(buf))
System.out.println(“你说得对!”);
其他的
System.out.println(“你错了…”);
}
System.out.println(“您得到了一个”+g.getScore());
System.out.print(“按Enter继续;键入Exit退出”);
如果(s.nextLine().equalsIgnoreCase(“退出”)){
s、 close();
系统出口(0);
}
玩家;
}
公共静态void main(字符串a[]{
玩家(新扫描仪(System.in));
}
}

将for循环替换为while循环,然后在执行g之前确认用户是否输入了“退出”确定,我是不是错过了什么?我不相信这是真的很新,需要帮助我有三个不同的代码这只是游戏driver@jake-威尔克斯按原样运行,并让我们知道结果。@jake wilks您能投票/标记作为解决方案吗?[接受答案]stackoverflow.com/help/accepted-answer;[投票]stackoverflow.com/help/why-vote@jake-威尔克斯按原样运行此代码,并告诉我们结果。我使用了此代码。谢谢,现在我必须将继承添加到我的整个程序中。您能顺便帮我一下吗?