Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am unsure why my code is not working:
public int caughtSpeeding(int speed, boolean isBirthday) {
if(isBirthday=true){
speed = speed - 5;
}
if(speed<=60){
return 0;
}
if(speed>=81){
return 2;
}
return 1;
}
The question is:
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
You are using an assignment operator here =, where you should be using an == operator for comparison. So it should be
if(isBirthday==true){
speed = speed - 5;
}
Here's the problem:
if (isBirthday = true) {
It should be:
if (isBirthday == true) {
We use two equal signs for comparison, otherwise you're just assigning a true value to the variable, making it always true. We can go even further and simplify the expression like this:
if (isBirthday) {
Your code is not working because you have a typo here:
if(isBirthday=true){
this is setting the variable to true instead of checking its value
if(isBirthday==true){ is what you are looking for
and in almost all languages is better when you write
if(isBirthday){ for checking if true
if(!isBirthday){ for checking if false
instead
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is a segment of the code for my program. What I'm trying to do is match the health of the enemy with the random enemy that get selected. My plan is to make a condition for when the randomInt selects a number so I can print out the correct enemyHealth value from my array. Is there any way I can set the a condition for when rand.nextInt is equal to some number?
if(rand.nextInt(enemy.enemyType) = 0)
System.out.println(enemy.enemyHealth[0]);
if(rand.nextInt(enemy.enemyType) == 0)
System.out.println(enemy.enemyHealth[0]);
= is an assigning operator. It assigns a variable a value and that is not a variable. == is a comparing operator for primitive type variables.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The value of sum is not changing even if I am adding it inside the for loop. Value of diff is 3
The value which is getting printed is like 3 3 3 in initial iteration which should be 3 6 9.
Can someone please help?
loop start
sum = sum + diff<(26-diff)?diff:(26-diff);
loop end
Order of precedence in how your numeric expression is being calculated is what's causing you to get the wrong result. Your current statement is equivalent to this:
sum = (sum + diff)<(26-diff)?diff:(26-diff);
and so equates to the value of diff, which is always 3. Change your statement to this:
sum = sum + (diff<(26-diff)?diff:(26-diff));
and you'll get the behavior you're expecting. you can also use the += operator to fix this by changing your statement to:
sum += diff<(26-diff)?diff:(26-diff);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
public int pray(int secondsPrayed){
int randomRecoveryValue = secondsPrayed + new Random().nextInt(3);
int actuallyRecoveredValue = Math.min(this.MAX_MP -this.mp,randomRecoveryValue);
this.mp += actuallyRecoveredValue;
return actuallyRecoveredValue;
}
This is from a book I'm currently using to learn. The objective is to create a pray method for a Priest object that restores its MP by the amount of seconds prayed plus 0 to 2 at random. I don't quite understand two things:
3 after nextInt rather than 2 (the book's answer) - why is this when we're trying to get 0 to 2 at random?
actuallyRecoveredValue returns its value to secondsPrayed; however, to me it looks like that to get actuallyRecoveredValue in the first place we need RandomRecoveryValue, which in turn needs secondsPrayed, which at that point doesn't seem to exist? I'm new to Java so I don't have much experience with returning values and such.
nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this
random number generator's sequence.
You pass secondsPrayed as variable into the method, so it does exist inside the method (scope). From the code you posted, we can't tell if actuallyRecoveredValue returns its value to secondsPrayed is true.
to me it looks like that to get actuallyRecoveredValue in the first
place we need RandomRecoveryValue, which in turn needs secondsPrayed
it's true, but as stated above, secondsPrayed does exist
1) The nextInt() method returns an integer value between zero and the parameter you used (3 in your case). While zero is included in the range, the last element of the range is not.
You can think that nextInt(n) returns n possible values, meaning the range between "0" and "n-1"
nextInt(5) // can return 0,1,2,3,4
2) The code is a little bit messy, I'll give you that, and as you said to get you have to determine the value of "randomRecoveryValue" in order to determine actuallyRecoveredValue. The method pray() does three things:
Calculates the recovered value, based on the input param and some randomness
Adjusts this value, so that the priest cannot heal over its full health. That happens at the third line, basically the priest heals for a number of points equal to "randomRecoveryValue" only if its current MP is not too high, otherwise he heals for the value that will get him to full health.
Update the value of his MP
Returns how much the he actually healed, not necessary, but may be useful to the caller of this method
Cheers
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
How do I check in Java if a string is not equal to "ABC"?
I know that to check if it is equal we can type string.equals("ABC") but how do I do it when I want to check if it is not equal?
Simply negate the result of equals:
!string.equals("ABC")
String.equals returns a boolean value, to get the inverse of any boolean value, use the ! operator:
boolean t = true; // t will be true
boolean f = !t; // f will be false
Negate the returning value from string.equals(TestString):
!string.equals("ABC")
You better have a look at Logical Operators in Java
You can simply write it like this.
!string.equals("ABC")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
If I have an if statement that includes a string like this:
double GUInumber1 = ((GUInumber1 >= 0 || <= 0)? Double.parseDouble(GUIfirstNumber) : 0);
('GUIfirstNumber' is the string)
Why does it come up as a error? Do I need brackets somewhere, do I need to use 'If/else' instead?
Supposedly my compiler says it does not recognize or (||), is this supposed to be something else or does this work in a completely different way.
Any help on this situation would be appreciated, also, if you need to know or are just wondering why I want to make a if statement for a string its because whenever I try to put in a letter instead of a number Java crashes, I'm hoping I could get this to alternatively solve the situation instead.
EDIT:
Solved, had to remove 'or' statements and alternatively make more else statements instead.
This expression (GUInumber1 >= 0 || <= 0) is not correct.
You will need something on both sides of the <= operator.