Solved

Random output from an array

Posted on 2008-06-13
5
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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 use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

615 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