我让它完整循环,并且不知道如何让它停止

时间:2016-03-04 13:42:57

标签: java

我的老师要求我们在Java 8中创建一个神奇的8球程序。我们必须使用3种方法,主要方法,处理方法和输出方法,我们需要在方法之间传递参数。输出需要使用switch语句,我们需要在其中有一个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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

首先。您的代码中有很多拼写错误,例如您必须在方法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)。

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