Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
enter image description here
I need some help for an if condition. I know how the basic one works: if x == 1, return something. But how am I supposed to do it, when I need to define certain cases for different operators. Like if the operators is + then the result denoted the sum.
So basically I need to translate the condition in the link into base cases for a recrsive method. We learned that we always use if for basecases. I know how to that with smaller or bigger then, but with operators I don't know.
FYI: if() is not a loop while() is. if() statement works as true or false, if the statement is true then execute a certain code other wise some other code.
For example:
if(1==1){// yourcode } // Always as true
// or
String hello="hi there";
if(hello.contains("hi there")){ // Your code which if the statement happen to be true }
else { // Not true}
int x=3, s=1, i=2;
if(x==(s+i)){ // Your code which if the statement happen to be true }
else { // Not true}
also you can find a lot of tutorials online to help you better understand all the operators!
Are you trying to state if its positive or negative? If so you would do the following...
if(x >= 0){ //this operator is saying if x is greater than or equal to 0
// you can remove the equal sign to have it just greater or
// switch it to less than.
//if positive
}else{
//all other numbers, which would just be negative numbers
}
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 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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how does increment the integer in (all) loops work ?
Like in the first time it initialize the integer then it checks the condition then increments the value and after that it does what i told it to do or it initialize the integer then it checks the condition and it does what i told it to do then increment the value ?
In a for loop,
for (a;b;c){
// code d
}
1) It runs statement a first.
2) Then checks statement b.
3a) If b fails it exits the loop.
3b) If it passes, it executes d ONCE. If at any point in d does it exit (eg. break), it will not do anything else in the loop. If it does not break and completes all the code in d, then it runs statement c once.
3) repeat steps 2, 3 until 3a is true.
Assuming you mean a for loop:
for (<initialization>; <condition>; <increment>) {
// loop body
}
The initialization is executed first. Then the condition is evaluated, and if it evaluates to true, the loop's body is executed. Once that's done, the increment is executed. Then, the condition is evaluated again, and the whole process repeats until the condition no longer evaluates to true.
for-loop:
initialize->condition->code->increment->condition──false──>end
└───────true───────┘
while-loop:
condition->code->condition──false──>end
└─true─┘
do-while-loop:
code->condition──false──>end
└─true─┘
PS: If you downvote, do it at least for an objective reason. If I made a mistake please tell me.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For the condition
while (!(cChoice=='Q'||cChoice=='q')), an input of Q or q will cause the program to exit. Does the position of the ! cause this to happen? If so, can you explain why? I am just learning Java, so please go easy on me.
Well since the negation applies to the whole expression,
(!(cChoice=='Q'||cChoice=='q'))
evaluates to
!cChoice=='Q' && !cChoice=='q'
using boolean algebra:
(A+B)' = A' . B'
So if input choice is either of those the program will quit. It's not specific to java or any language.
To help understand why this conditional behaves as it does, we can break it down.
First, the inner block, cChoice == 'Q' || cChoice == 'q' will give a result of True if input is 'Q' or 'q'.
Next, adding an ! outside of this block will negate the result. So, in english, we can read this as:
while NOT(input is 'Q' or 'q'), execute
or... while we don't receive 'Q' or 'q' as input, execute.
For the statement :
while(cChoice=='Q'||cChoice=='q')
an input of Q or q will set this the condition as true. Hence the code inside while loop will run. But when you add the "!"(i.e., not) before the condition, the condition is now !(true), which means "not" true and hence false. So the program exits.
The easy way to understand this, is just to replace ! with ==false.
(!(cChoice=='Q'||cChoice=='q'))
is the same as
((cChoice=='Q'||cChoice=='q')==false)
Clearly true:
not male(Jeff) or not female(Jeff)
Clearly false:
not (male(Jeff) or female(Jeff))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.
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 7 years ago.
Improve this question
Here's a snippet from my code...
Boolean Add;
Double Answer;
Add = false;
if (Add == true);
{
Answer = (3 + 6);
System.out.print(Answer);
}
What baffles me is that at the end of my code it keeps evaluating and printing 3 + 6 even though the code is specifically geared to make it NOT show up. Any help would be appreciated.
EDIT: Thanks everyone
You have a semicolon at the end of your if condition. Java will treat that as the body for your if condition. The block with braces below it is detached from the if condition and will always run.
Remove the semicolon, and Java will interpret the block with braces as the body for your if statement.
Change
if (Add == true);
to
if (Add == true) // no semicolon
Additionally, Add is already a boolean value (a Boolean unboxed to a boolean). You can just say:
if (Add)
Remove ; after if condition.
if (Add == true);//<-here
If you format this code you will get
if (Add == true)
;
which is the same as placing empty block after if
if (Add == true)
{}//empty block
so this block will always execute because it is not actually no longer related to if condition
{
Answer = (3 + 6);
System.out.print(Answer);
}
BTW == true part is redundant in condition. You can just write it as if (Add)
You're mixing primitive types with reference types (not a problem really, but a bad practice nevertheless), and more importantly, there's an extra semicolon after the condition!. Try this:
boolean add;
double answer;
add = false;
if (add == true) // remove the ; from here
{
answer = 3 + 6;
System.out.print(answer);
}
That seemingly inoffensive ; was the cause of the problem. If you add a ; after the condition, you're stating: here's the end of the condition, there won't be any instructions inside. The rest of the code, the part that's inside {} runs inside a block, but will always be executed - it's outside the if statement.
You have a semicolon immediately after if so it is not executed. Change:
if (Add == true);
to:
if (Add == true)
Also you should consider using boolean primitive instead of Boolean class.