Solved

Random output from an array

Posted on 2008-06-13
5
418 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make AngularJS IntelliSense working in VS 2012 4 174
Notepad++ Remote File Management 3 86
selectallvieditor 6 112
egit plugin on eclipse 8 62
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes 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.

813 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