Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###Not so random Random bug

Not so random Random bug

int randNumber = randGenerator.nextInt((endRange - startRange + 1)
 + startRange);

Let's say that startRange = 10, and endRange = 25, then this will evaluate to:

int randNumber = randGenerator.nextInt((25 - 10 + 1) + 10);
int randNumber = randGenerator.nextInt((16) + 10);
int randNumber = randGenerator.nextInt(26);

Which will produce numbers in the range 0 -- 25. But... startRange was set to 10 !?

That's because your parenthesis is incorrect. Correct would be:

int randNumber = randGenerator.nextInt(endRange - startRange + 1) + startRange;

Which evaluates, with the same example numbers, to:

int randNumber = randGenerator.nextInt(25 - 10 + 1) + 10;
int randNumber = randGenerator.nextInt(16) + 10;

So the lowest possible number is 0 + 10 and the highest 15 + 10, that looks about right!


###A variable name

A variable name

int maxAttemp;

I'm not quite sure why you left that variable name unfini

I'd recommend naming it maxAttempts. Cutting the last two characters helps no one.


###If-else-if-else-if...else?

If-else-if-else-if...else?

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
}

I can assure you, if a number is not equal to, and not more than another number, it will be less than.

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else {
 ...
}

Or, as I would prefer to write it: If it's not more, and not less, then it is equal.

if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
} else {
 ...
}

###Not so random Random bug

int randNumber = randGenerator.nextInt((endRange - startRange + 1)
 + startRange);

Let's say that startRange = 10, and endRange = 25, then this will evaluate to:

int randNumber = randGenerator.nextInt((25 - 10 + 1) + 10);
int randNumber = randGenerator.nextInt((16) + 10);
int randNumber = randGenerator.nextInt(26);

Which will produce numbers in the range 0 -- 25. But... startRange was set to 10 !?

That's because your parenthesis is incorrect. Correct would be:

int randNumber = randGenerator.nextInt(endRange - startRange + 1) + startRange;

Which evaluates, with the same example numbers, to:

int randNumber = randGenerator.nextInt(25 - 10 + 1) + 10;
int randNumber = randGenerator.nextInt(16) + 10;

So the lowest possible number is 0 + 10 and the highest 15 + 10, that looks about right!


###A variable name

int maxAttemp;

I'm not quite sure why you left that variable name unfini

I'd recommend naming it maxAttempts. Cutting the last two characters helps no one.


###If-else-if-else-if...else?

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
}

I can assure you, if a number is not equal to, and not more than another number, it will be less than.

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else {
 ...
}

Or, as I would prefer to write it: If it's not more, and not less, then it is equal.

if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
} else {
 ...
}

Not so random Random bug

int randNumber = randGenerator.nextInt((endRange - startRange + 1)
 + startRange);

Let's say that startRange = 10, and endRange = 25, then this will evaluate to:

int randNumber = randGenerator.nextInt((25 - 10 + 1) + 10);
int randNumber = randGenerator.nextInt((16) + 10);
int randNumber = randGenerator.nextInt(26);

Which will produce numbers in the range 0 -- 25. But... startRange was set to 10 !?

That's because your parenthesis is incorrect. Correct would be:

int randNumber = randGenerator.nextInt(endRange - startRange + 1) + startRange;

Which evaluates, with the same example numbers, to:

int randNumber = randGenerator.nextInt(25 - 10 + 1) + 10;
int randNumber = randGenerator.nextInt(16) + 10;

So the lowest possible number is 0 + 10 and the highest 15 + 10, that looks about right!


A variable name

int maxAttemp;

I'm not quite sure why you left that variable name unfini

I'd recommend naming it maxAttempts. Cutting the last two characters helps no one.


If-else-if-else-if...else?

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
}

I can assure you, if a number is not equal to, and not more than another number, it will be less than.

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else {
 ...
}

Or, as I would prefer to write it: If it's not more, and not less, then it is equal.

if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
} else {
 ...
}
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

###Not so random Random bug

int randNumber = randGenerator.nextInt((endRange - startRange + 1)
 + startRange);

Let's say that startRange = 10, and endRange = 25, then this will evaluate to:

int randNumber = randGenerator.nextInt((25 - 10 + 1) + 10);
int randNumber = randGenerator.nextInt((16) + 10);
int randNumber = randGenerator.nextInt(26);

Which will produce numbers in the range 0 -- 25. But... startRange was set to 10 !?

That's because your parenthesis is incorrect. Correct would be:

int randNumber = randGenerator.nextInt(endRange - startRange + 1) + startRange;

Which evaluates, with the same example numbers, to:

int randNumber = randGenerator.nextInt(25 - 10 + 1) + 10;
int randNumber = randGenerator.nextInt(16) + 10;

So the lowest possible number is 0 + 10 and the highest 15 + 10, that looks about right!


###A variable name

int maxAttemp;

I'm not quite sure why you left that variable name unfini

I'd recommend naming it maxAttempts. Cutting the last two characters helps no one.


###If-else-if-else-if...else?

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
}

I can assure you, if a number is not equal to, and not more than another number, it will be less than.

if (guess == randNumber) {
 ...
} else if (guess > randNumber) {
 ...
} else {
 ...
}

Or, as I would prefer to write it: If it's not more, and not less, then it is equal.

if (guess > randNumber) {
 ...
} else if (guess < randNumber) {
 ...
} else {
 ...
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /