方法,循环和randoms,我的

时间:2015-10-24 01:40:45

标签: java loops methods

我在学校有一项任务,创建一个程序,至少有三个独立的方法,就像一个'魔术八球'。我已经创建了方法,将10个'响应'设置为'switch'语句,基于在一个单独的方法,生成一个0到9之间的随机数。我需要的是一个'for','while'或'do while'循环方法,它应该继续提问,直到输入'exit'这个词为止一个问题。然后我需要一种方法将所有这些方法联系在一起,以便它们正常工作。我使用一个名为BlueJ的程序(按照教授的指示)

到目前为止我所拥有的是:

import java.util.*;
public class MagicEightBall
{
    //Input method
   public static void main(String[] args)
   {

       startAsking();            
   }
   //Loop method
    public static void startAsking()
   {
       do
       {
           Scanner input = new Scanner(System.in); 
           System.out.print("Enter Question: ");
           System.out.println("Entering 'exit' ends program ");
            if(input.nextLine().equalsIgnoreCase("exit"))
            break;
            System.out.println(getResponse());
       }
        while(true);
        //input.close();
   }   
   //Output method     
   public static String getResponse()   
   {
       int numberOfResponses = 10;
       int response = (int)(Math.random() * numberOfResponses);
       String responseString;
       switch (response)
       {
            case 1: System.out.println("Of course!  H-A-L said so");
            case 2: System.out.println("Yes, my young Padawan!");
            case 3: System.out.println("V-ger has informed me that your answer is 'Yes'");
            case 4: System.out.println("Mr. Spock says 'Not a chance, Captain'");
            case 5: System.out.println("Only when Pi = apple would that be a 'Yes'");
            case 6: System.out.println("There is no try, only do, or do not");
            case 7: System.out.println("You know 'Big Brother' heard you ask that question?");
            case 8: System.out.println("SyStEm MaLfUnCtIoN! pLeAsE tRy l8r");
            case 9: System.out.println("No.  That would cause a food fight");
            default: System.out.println("I'm sorry, it's time for my oil bath");
       }
       return responseString;
   }   
}

如何完成此任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我构建你的代码,然后运行它; 阅读有关你的问题似乎做了所有事情(但不是100%正确),在这个步骤中: 1问你一个问题 2回答你随机
3如果被问到“退出”他会停止程序

你能更好地解释一下你所需要的程序吗?现在不做?

现在我可以说你使用变量来追加问题,而不是使用变量来循环使用go还有getresponse方法中不需要的东西

import java.util.*;
public class NewClass
{
    //Input method
   public static void main(String[] args)
   {

       startAsking();            
   }
   //Loop method
    public static void startAsking()
   {
       do
       {

           Scanner input = new Scanner(System.in); 
           System.out.print("Enter Question: ");
           System.out.println("Entering 'exit' ends program ");
            if(input.nextLine().equalsIgnoreCase("exit"))
            break;
           getResponse();

       }
        while(true);
        //input.close();
   }   
   //Output method     
   public static void getResponse()   
   {
       int numberOfResponses = 10;
       int response = (int)(Math.random() * numberOfResponses);
       System.out.println(response);

       switch (response)
       {
            case 1: System.out.println("Of course!  H-A-L said so");break;
            case 2: System.out.println("Yes, my young Padawan!");break;
            case 3: System.out.println("V-ger has informed me that your answer is 'Yes'");break;
            case 4: System.out.println("Mr. Spock says 'Not a chance, Captain'");break;
            case 5: System.out.println("Only when Pi = apple would that be a 'Yes'");break;
            case 6: System.out.println("There is no try, only do, or do not");break;
            case 7: System.out.println("You know 'Big Brother' heard you ask that question?");break;
            case 8: System.out.println("SyStEm MaLfUnCtIoN! pLeAsE tRy l8r");break;
            case 9: System.out.println("No.  That would cause a food fight");break;
            default: System.out.println("I'm sorry, it's time for my oil bath");
       }


   }   
}

不关心我添加的一些输出来控制循环