Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

what is wrong with the following code? public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random();

what is wrong with the following code?

public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random();

public static String getAnswer(int category, int answer) { if (category >= 74) { if (answer == 0) { return "As I see it, yes." ; } else if (answer == 1) { return "Signs point to yes." ; } else if (answer == 2) { return "Outlook good." ; } else if (answer == 3) { return "Without a doubt." ; } else if (answer == 4) { return "You may rely on it." ; } } else if (category >= 24 && category < 74) { if (answer == 0) { return "Reply hazy, try again." ; } else if (answer == 1) { return "Ask again later." ; } else if (answer == 2) { return "Better not tell you now." ; } else if (answer == 3) { return "Cannot predict now." ; } else if (answer == 4) { return "Concentrate and ask again." ; } } else if (category < 24) { if (answer == 0) { return "Dont count on it." ; } else if (answer == 1) { return "My reply is no." ; } else if (answer == 2) { return "My sources say no." ; } else if (answer == 3) { return "Outlook not so good." ; } else if (answer == 4) { return "Very doubtful." ; } } }

// ************** don't modify below this line ************************ public static void main(String[] args) { printSpash(); run(); }

public static void printSpash() { System.out.println("88888888888888888888"); System.out.println("8 Magic 8 Ball 8"); System.out.println("8 Ask away 8"); System.out.println("88888888888888888888"); System.out.println(" And the answer is... "); }

public static void run() { int cat = rnd.nextInt(100); int an = rnd.nextInt(5); System.out.println("category num: " + cat + " answer num: " + an); System.out.println(getAnswer(cat, an)); // gets 0-99 and 0-4 (one less than what you put in) System.out.println(); System.out.print("Ask another question (Yes/No?): "); String input = scanner.nextLine(); if(input.toLowerCase().startsWith("n")) { // this ia fun trick, worth remembering it. System.out.println("Thank you for playing!"); return; } System.out.println(" And the answer is... "); run(); // continue looping! }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxiv Special Issue On Database And Expert Systems Applications Lncs 9510

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Hendrik Decker ,Lenka Lhotska ,Sebastian Link

1st Edition

366249213X, 978-3662492130

More Books

Students also viewed these Databases questions

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago