Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

Random output from an array

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
Arka3L
Asked:
Arka3L
  • 3
  • 2
2 Solutions
 
Frosty555Commented:
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
 
Frosty555Commented:
and I forgot my semicolons of course :)
0
 
Arka3LAuthor Commented:
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
 
Arka3LAuthor Commented:
Well, the whole line is red so it could be anything, not the int index alone, sorry for not clearing that up.
0
 
Arka3LAuthor Commented:
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

Independent Software Vendors: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now