Solved

Random output from an array

Posted on 2008-06-13
5
413 Views
Last Modified: 2013-12-14
Hey guys,

I need some help with a little project i've got. I've only been working with Java for about 3 months on and off, so be patient with me.

So far I have a loop that requests the user to enter the correct answer for a random multiplication which i've added to the post. However, my question is, I have an array of possible yes and no answers, yet how do I randomly generate those to the output terminal? My variables are:
 
String[] yList = { "Congratulations!", "Very well done", "A+", "Well done!"};
String[] nList = { "I'm sorry, the correct answer is ", "Not is not correct. The answer is: ", "Wrong. The answer is: "};
     


while(true){

   

    int max = 12;

    int max2 = 12;

  

    

    r1 = RanGen(max);

    r2 = RanGen(max2);

   

        System.out.println("What is " + r1 + " x " + r2 + "? : ");

    

    

    strNumber = jin.readLine();  //wait for user input

    Integer.parseInt(strNumber);

    int userInput = Integer.parseInt(strNumber);

  

    answer = r1*r2;

    

    if(answer == userInput)

    {

        System.out.println("Well done!");

        System.out.println("");

    }

    else

    {

        System.out.println("Sorry, the answer is: " + answer);

        System.out.println("");

    }

    if(userInput == 0)

    {

        System.exit(0);

  }

}

Open in new window

0
Comment
Question by:Arka3L
  • 3
  • 2
5 Comments
 
LVL 31

Assisted Solution

by:Frosty555
Frosty555 earned 125 total points
ID: 21781722
Doesn't look too hard. You've already done all the hard work. Now you just need to get a random number based on the size of your yList and nList arrays, and display one of the indexes in the array.

 if(answer == userInput)

    {

        int index = Math.round(Math.random() * yList.length)

        System.out.println(yList[index]);

        System.out.println("");

    }

    else

    {

        int index = Math.round(Math.random() * nList.length)

        System.out.println(nList[index] + answer);

        System.out.println("");

    }

Open in new window

0
 
LVL 31

Expert Comment

by:Frosty555
ID: 21781726
and I forgot my semicolons of course :)
0
 
LVL 1

Author Comment

by:Arka3L
ID: 21781788
No problem, i've added them in but now it's telling me that "int index" has possible loss of precision, found: long, expected: int. I don't know what that means. I've tried creating int index just for the heck of it lol. Sorry for this, i'm sure it's a straight forward answer, but I'm still learning.
0
 
LVL 1

Author Comment

by:Arka3L
ID: 21781823
Well, the whole line is red so it could be anything, not the int index alone, sorry for not clearing that up.
0
 
LVL 1

Accepted Solution

by:
Arka3L earned 0 total points
ID: 21781875
Don't you just love it when you figure out the code yourself after hours of pulling your hair out in 5 minutes? -.-


 while(true){

   

    String[] yList = { "Congratulations!", "Very well done", "Well done!"};

    String[] nList = { "I'm sorry, the correct answer is: ", "No that's not correct. The answer is: ", "Wrong. The answer is: "};

    int max = 12;

    int max2 = 12;

    int yesIndex;

    int noIndex;

    

    yesIndex = RanGen(4)-1;

    noIndex = RanGen(4)-1;

    

    r1 = RanGen(max);

    r2 = RanGen(max2);
 

    System.out.println("What is " + r1 + " x " + r2 + "? : ");

    

    

    strNumber = jin.readLine();  //wait for user input

    Integer.parseInt(strNumber);

    int userInput = Integer.parseInt(strNumber);

  

    answer = r1*r2;

    

    if(answer == userInput)

    {

        System.out.println(yList[RanGen(3)-1]);

        System.out.println("");
 

    }

    else

    {

       System.out.println(nList[RanGen(3)-1] + answer);

        System.out.println("");

    }

    if(userInput == 0)

    {

        System.exit(0);

  }

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now