samedi 24 octobre 2015

Multiple methods with a loop

I'm trying to get this program to work like a Magic Eight Ball using at least three methods and a loop. I am not familiar with arrays yet, so what I have is:

import java.util.*;
public class MagicEightBall
{
    //Input method
   public static void main(String[] args)
   {
       String answer = getResponse();
       startAsking();            
   }
   //Loop method
    public static void startAsking()
   {
       Scanner input = new Scanner(System.in); 
       System.out.println("Enter Question: ");
       System.out.println("Entering 'exit' ends program ");
       String question = input.nextLine();
       String exit = "exit";
       while (!question.equals(exit)) 
       {
            getResponse();
            System.out.print("Enter Question: ");
            question = input.nextLine();
       }
    }   //input.close();
   //Output method     
   public static String getResponse()   
   {
       int numberOfResponses = 10;
       int response = (int)(Math.random() * numberOfResponses);
       //String answer = getResponse();
       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");
                break;
       }
       return response;
    }   
}

No idea where I'm going wrong, but it either goes into an infinate loop, or does not return a single random answer.




Aucun commentaire:

Enregistrer un commentaire