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))
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 4 months ago.
Improve this question
For an assignment, I had to make a program to see if the user input contained any of the three letters: r, c or p. If it was wrong, I had to keep asking the user to retype the input until the user typed the correct answer.[]
I tried using a do while loop to do the validation and I tested it myself, but instead of the invalid error message being returned only as long as the conditions were met, it returns it no matter what the user input was. The do statement ignored even the "valid inputs" and prints an invalid message regardless.[]
The main problem in your program is the condition you specified for the loop. If the input is not "c" or not "r" or not "p" the loop will continue. But the input will always be not "c" or not "r", if your input is for example "p". You should seperate the conditions with an and not an or.
while (!customerCode.contentEquals("r") && !customerCode.contentEquals("c") && !customerCode.contentEquals("p"));
You need to use a while loop here, not a do..while.
A do..while loop runs its body at least once, and then checks the condition - so it will show the error message at least once no matter what.
Instead, use a while loop, which checks the condition first:
while (input does not contain r, c or p) {
// Show error, ask for new input
}
// Input is now valid
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 2 years ago.
Improve this question
Why does this return a false?
if (!member.getPermissions().contains(Permission.ADMINISTRATOR) || !member.getId().equals(UniversalVariables.DJZK)) {
eb = EmbedMaker.embedBuilderDescription(MessageSender.noPermission);
channel.sendMessage(eb.build()).queue();
return;
}
You need to be a bit more specifi CH as far as i can tell you say in the if-Statement:
if you have NOT permission ADMINISTRATOR OR Are NOT user A then Do smoething
This is logically equivalent to if you are both ADMINISTRATOR AND user A then DON‘T run the if statement.
So you probably are the said user A and have the said permission.
your return; statement has no argument so it returns "nothing"! This may be 0 which is interpreted as False.
So the logic or is irrelevant to the return value. So return(true); would return logic true when the if condition is true.
Nathan's Answer was right. Usage of Morgan's Rule.
if(!(member.getPermissions().contains(Permission.ADMINISTRATOR) || member.getId().equals(UniversalVariables.DJZK))){
eb = EmbedMaker.embedBuilderDescription(MessageSender.noPermission);
channel.sendMessage(eb.build()).queue();
return;
}
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
}
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
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.