Results 1 to 2 of 2

Thread: Magic Eightball

  1. #1
    sachmow is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Smile Magic Eightball

    Hi, guys this is my first java program and i'm having some trouble with a loop. The first question we are supposed to ask is how many questions you would like to ask the eightball and then thats how many times the program is supposed to loop. I can't figure out how to do this for the life of me. Please Help


    import java.util.Scanner;

    import java.util.Random;

    public class scannerinput {




    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Random generator = new Random();
    int X = generator.nextInt(20)+1;
    int intQuestion;

    Scanner SC = new Scanner(System.in);

    {
    System.out.print("How many questions do you wish to ask?-----");
    intQuestion = SC.nextInt();

    SC.nextLine();

    }
    System.out.print("Ask A Yes or No question and you will receive an answer-----");
    String Question1 = SC.nextLine();
    {
    if (X == 1)

    System.out.println("As I see it, yes");

    if (X == 2)

    System.out.println("It is certain");

    if (X == 3)

    System.out.println("It is decidely so");

    if (X == 4)

    System.out.println("Most likely");

    if (X == 5)

    System.out.println("Outlook good");

    if (X == 6)

    System.out.println("Signs point to yes");

    if (X == 7)

    System.out.println("Without a doubt");

    if (X == 8)

    System.out.println("Yes");

    if (X == 9)

    System.out.println("Yes - definitely");

    if (X == 10)

    System.out.println("You may rely on it");

    if (X == 11)

    System.out.println("Reply hazy, try again");

    if (X == 12)

    System.out.println("Ask again at a later time");

    if (X == 13)

    System.out.println("Better not tell you now");

    if (X ==14)

    System.out.println("Cannot predict now");

    if (X == 15)

    System.out.println("Concentrate and ask again");

    if (X == 16)

    System.out.println("Don't count on it");

    if (X == 17)

    System.out.println("My reply is no");

    if (X == 18)

    System.out.println("My sources say no");

    if (X == 19)

    System.out.println("Outlook not so good");

    if (X == 20)

    System.out.println("very doubtful");

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The general structure of a for loop is like so:
    Java Code:
        for (int i = 0; i < max; i++) {
          
        }
    where "max" above is the total number of times that the for-loop should loop. So simply substitute your int value there:
    Java Code:
        Scanner input = new Scanner(System.in);
        
        System.out.print("How many questions do you wish to ask?-----");
        int questionCount = input.nextInt();
        input.nextLine();
        
        for (int i = 0; i < questionCount; i++) {
           // code you want looped goes in here      
        }
    Oh and welcome to the forum, and please use code tags. :)

Similar Threads

  1. Jar files and a magic error.
    By dudejonne in forum Java Applets
    Replies: 2
    Last Post: 10-31-2009, 03:28 PM
  2. [SOLVED] Magic Points Game
    By jimmy7 in forum Java Applets
    Replies: 6
    Last Post: 04-27-2009, 12:40 AM
  3. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 05:04 AM
  4. Incompatible magic value 1008821359
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 03-21-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •