tags:

views:

83

answers:

3

I know I was just asking a question earlier facepalm

This is in Java coding by the way.

Well after everyones VERY VERY helpful advice (thank you guys alot)

I managed to get over half of the program running how I wanted.

Everything is pointing in the arrays where I want them to go. Now I just need to access the arrays so that It prints the correct information randomly.

This is the current code that im using:

http://pastebin.org/301483

The specific code giving me problems is this:

long aa; int abc;

for (int i = 0; i < x; i++)
{
 aa = Math.round(Math.random()*10);

 String str = Long.toString(aa);
 abc = Integer.parseInt(str);

 String[] userAnswer = new String[x];

 if(abc > x)
 {
  JOptionPane.showMessageDialog(null,"Number is too high. \nNumber Generator will reset.");
  break;
 }

 userAnswer[i] = JOptionPane.showInputDialog(null,"Question "+quesNum+"\n"+questions[abc]+"\n\nA: "+a[abc]+"\nB: "+b[abc]+"\nC: "+c[abc]+"\nD: "+d[abc]);

 answer = userAnswer[i].compareTo(answers[i]);

 if(answer == 0)
 {
  JOptionPane.showMessageDialog(null,"Correct. \nThe Correct Answer is "+answers[abc]+""+i);
 }
 else
 {
  JOptionPane.showMessageDialog(null,"Wrong. \n The Correct Answer is "+answers[abc]+""+i);
 }//else
+2  A: 

I'm not sure what your question is, but I noticed this line:

aa = Math.round(Math.random()*10);

If you need a random int between 0 and 10 inclusive, it's much better to use:

java.util.Random.nextInt(int n): Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

You'd first create an instance elsewhere (you should only need to do this once in your application):

Random r = new Random();

Then, whenever you need a random integer between 0 and 10 inclusive, you do:

aa = r.nextInt(10+1);

You can just write 11, but writing 10+1 perhaps have the added benefit of reminding readers that it's a half-open range.


Okay, I just noticed the following:

 aa = Math.round(Math.random()*10);
 String str = Long.toString(aa);
 abc = Integer.parseInt(str);

This makes very little sense. You should be able to just do:

 aa = r.nextInt(10+1);
 abc = (int) aa;

Though frankly I'm not sure if you really need these many variables in the first place. Perhaps you can just write:

 int aa = r.nextInt(10+1);

You also ought to consider using String.format instead of doing all these concatenations.

    System.out.println(
        String.format("%d + %d = %s", 3, 4, 7)
    ); // prints "3 + 4 = 7"

API links

polygenelubricants
you dont even realize the help you have just been. I have been looking for a way to randomize numbers for a LONG time. I never knew how. What more does that random do?
Nick Gibson
@Nick: see full API http://java.sun.com/javase/6/docs/api/java/util/Random.html ; it has Gaussian distribution, random `boolean`, random bits, random `byte[]`, random `long`, and of course, `double` between 0 and 1.
polygenelubricants
what all my books have ever told me was Math.random() which creates a random number 0.0 through 1.0 which is crazily inconvienent being how 1) its in data type long and 2) i have to multiply it and manipulate it in several ways to ev en get it to where I need it to be.
Nick Gibson
@Nick: listen to the other answers too about variable names.
polygenelubricants
@Nick: I think you can just write `if (userAnswer[i].equals(answers[i])) { ... } else { ... }`
polygenelubricants
Thanks...that shortened it and helped. Also, if you have time is there any easier place to talk. The time frame here is too wide
Nick Gibson
+1  A: 

In the future, use real variable names. aa and abc mean nothing and don't help convey what date the variable actually holds. Unless you are are dealing with coordinates, x is also a bad variable name.

unholysampler
+1  A: 

Hi Nick,

Some general comments on your code:

  • Try to use descriptive variable names. A variable named "x" doesn't mean much to the programmer reading your code. For all he knows, it could be used to store the population of France. What do you think a better name might be?

  • Keep in mind the scope of your variables (where they are being used). For example, if a variable is only being used in a for loop, then it should be declared in the for loop. Where do you think the "qq" variable declaration should be moved to?

Michael Angstadt