mercredi 6 avril 2016

Issues with creating a random response using a switch statement

I am trying to create a Magic 8 Ball in JavaScript. The requirements are to use a switch statement with a random number generator and pass information through multiple methods. The constraint is that I can't use arrays or "var". The issue I'm having is that when I try to compile, I am getting the "String cannot be converted to int" error. I've tried numerous ways to convert .toString throughout the code but then I receive the error, "cannot be dereferenced". Any guidance on how I can achieve the goal (user inputs anything, system spits back a random response through multiple methods and a random switch statement without using an array or "var") is greatly appreciated. Here is what I have so far:

   public class Magic   
   {
    public static void main(String[] args)
    {
        String input;
        String outputString = getInput();
        System.out.println(outputString);
    }
    public static String getInput()
    {
        Scanner in = new Scanner(System.in);
        String input;
        System.out.println("Please enter a question");
        input = in.nextLine();
        String outputString = getOutputString();
        return outputString;  
    }
    public static String getOutputString()
    {
        int numberOfResponses = 3;
        int response = (int)(Math.random() * numberOfResponses);
            switch (response)
            {
                case 1: response = "Random 1";
                break;
                case 2: response = "Random 2";
                break;
                case 3: response = "Random 3";
                break;
            }
        return response;      
    }
 }




Aucun commentaire:

Enregistrer un commentaire