开发者

Arrays not counting correctly

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


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

  • java.util.Formatter


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜