Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 - Fatal编程技术网

Java 我让它在一个完整的循环中运行,不';我不知道如何让它停止

Java 我让它在一个完整的循环中运行,不';我不知道如何让它停止,java,Java,我的老师让我们用Java8创建一个Magic8Ball程序。我们必须使用3个方法,一个main、一个processing和一个output,我们需要在这些方法之间传递参数。输出需要使用开关语句,我们需要在那里有一个while语句,并且需要随机生成答案。我有所需的一切,但当我试图运行程序时,它卡在while循环中,我不知道我做错了什么。 以下是我所拥有的: import java.util.*; public class Magic8Ball { public static void ma

我的老师让我们用Java8创建一个Magic8Ball程序。我们必须使用3个方法,一个main、一个processing和一个output,我们需要在这些方法之间传递参数。输出需要使用
开关
语句,我们需要在那里有一个
while
语句,并且需要随机生成答案。我有所需的一切,但当我试图运行程序时,它卡在while循环中,我不知道我做错了什么。 以下是我所拥有的:

import java.util.*;
public class Magic8Ball {
    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Would you like to ask a question? Y or N: ");
        char answer = input.next().charAt(0);
        char Y = Character.toUpperCase(answer);
        process(answer, Y);
    }
    public static void process(char a, char Yes) {
        if (a != Yes) {
            System.out.println("Thank you, goodbye.");
        } else {
            while(a==Yes) {
                System.out.print("Ask your question: ");
                Random random = new Random();
                int ran = random.nextInt(8-1+1)+1;
                output(ran);
            }
        }
    }
    Public static int output(int r) {
        switch (r) {
        case 1: System.out.println("Out of memory, try again later); break;
        case 2: System.out.println("The probability matrix supports you."); break;
        case 3: System.out.println("That does not compute."); break;
        case 4: System.out.println("System error, try again later"); break;
        case 5: System.out.println("Siri says yes."); break;
        case 6: System.out.println("The asnwer could not be found on the internet."); break;
        case 7: System.out.println("Wikilinks claims it is true."); break;
        case 8: System.out.println("Siri says no."); break;
        default: System.out.println("The system is not responding, try again later"); break;
        }
        return r;
    }
}

首先。您的代码中有很多打字错误,例如,您必须在方法
output()
的第一个print语句末尾关闭“打印”。第二个错误,Java区分大小写,因此
Public
Public
不同

现在,让我们尝试为这个问题提供一个简单的解决方案。简单的解决方案可能是:在while循环的末尾添加一个返回,如下面的代码所示

while(a == Yes) {
   System.out.print("Ask your question: ");
   Random random = new Random();
   int ran = random.nextInt(8-1+1)+1;
   output(ran);
   return;
}
这样,在打印第一个输出后就可以停止循环

请记住,
return
是一个“魔法世界”,您可以使用它来告诉您的方法停止并返回结果(在这种情况下,您没有结果,因为该方法返回一个void)


它对我有效,请告诉我这是否是解决您问题的正确解决方案;-)

您在循环中既没有更新
a
也没有更新
Yes
。当您要退出时,必须更新循环中
a
的值……否则它将是一个无限循环条件为
而(a==Yes)
并且您从不更改
a
Yes
(顺便说一句,我不知道您为什么要从一开始就进行此测试。它检查输入是否等于输入的大写版本。)我开始了一个答案,但是你的代码充满了bug和逻辑缺陷。请一步一步地重新思考逻辑。用英语写下什么时候发生了什么以及比较是什么。你会从中学到更多。把你的while循环放在读线上。而不是放在方法中。@Lisa S如果你接受了上一篇文章的答案,你会知道的你将来的问题可能会得到更好的回答。你为什么要使用返回和while循环???有一个正确的布尔语句更有意义。在这种情况下,它只运行一次并返回。while语句有什么用呢???是的,你是对的,但我试图保持结构不变,而不修改太多代码。我明白了n在Lisa的另一篇文章中,她说如果老师使用不同的方法,他/她会扣分数。他扣分数是件好事。你应该学会正确,而不是错误。不,老师扣分数(感谢你这个世界;-))如果shee修改了代码的结构。例如,在另一个问题中,她只能用于循环,而不能用于while。因此,基于while的更好解决方案不是一个好的解决方案。
import java.util.*;
public class Magic8Ball {

    public static void main(String[]args) {

        System.out.print("Would you like to ask a question? Y or N: ");
        Scanner infeedScanner = new Scanner(System.in); 
        char input = infeedScanner.next().charAt(0);
        process(input, 'Y');
    }

    public static void process(char a, char Yes) 
    {
       if (a != Yes) 
       {
         System.out.println("Thank you, goodbye.");
       }
       else 
       {
           boolean bContinue = true;

           while(bContinue) 
           {
               System.out.print("Ask your question: ");
               Random random = new Random();
               int ran = random.nextInt(8-1+1)+1;
               output(ran);

               System.out.print("Would you like to ask another question? Y or N: ");
               Scanner infeedScanner = new Scanner(System.in);

               if (infeedScanner.next().equalsIgnoreCase("n"))
               {
                   bContinue = false;
               }
           }
       }
    }

    public static int output(int r) 
    {     
        switch (r) {
            case 1: System.out.println("Out of memory, try again later"); break;
            case 2: System.out.println("The probability matrix supports you."); break;
            case 3: System.out.println("That does not compute."); break;
            case 4: System.out.println("System error, try again later"); break;
            case 5: System.out.println("Siri says yes."); break;
            case 6: System.out.println("The answer could not be found on the internet."); break;
            case 7: System.out.println("Wikilinks claims it is true."); break;
            case 8: System.out.println("Siri says no."); break;
            default: System.out.println("The system is not responding, try again later"); break;
        }

        return r;   
    }
}