This is what I wrote:
if ((Toss3.equals(h));
numberHead3 = 1;
if (Toss3.equals(t))
numberTail3 = 1
else
numberHead3 = 0;
numberTail3 = 0;
invalidInput3 = 1;
System.out.println("Invalid input, please enter 'h', 'H', 't', or 'T'");
I want to assign the value 1 to numberHead3 if the value of Toss3 is equal to h and assign the value 1 to numberTail3 if the value of Toss3 is equal to t. If not then assign the value of 0 to numberHead3 and numberTail3. How do I write that? Is mine correct?
2006-09-15
05:12:15
·
5 answers
·
asked by
?
1
in
Computers & Internet
➔ Programming & Design
what i want to have here is actually putting two if statements and getting the same else output of both og that if statement. how do I do that?
2006-09-15
05:24:06 ·
update #1
All you are missing is the curly braces around your else blocks. Even though, in the snippet of the code you have provided, not having the braces don't matter, but it may effect the logic in your program if you don't have the braces.
Also, since we know that the result of toss could either be H OR a T, we can put else if(Toss3.equals(t)), because we know, if toss result was equal to H it would never be equal to T. This doesn't make a huge difference but, it prevents an extra evaluation of a condition.
Let's say the result of toss3 is h but you're not using else, your program will first go into the first if block, then it would evaluate the second if block, however, if you use else if in the second block (leveraging our knowledge of tosses - that if toss result is h it cant be t as well), your program will not need to evaluate the else block.
if(Toss3.equals(h)) {
numberHead3 = 1;
}
else if (Toss3.equals(t)) {
numberTail3 = 1;
} else {
numberHead3 = 0;
numberTail3 = 0;
invalidInput3 = 1;
System.out.println("Invalid input. Please enter h, H, t or T" );
}
2006-09-15 05:54:30
·
answer #1
·
answered by SmartSpider 4
·
0⤊
0⤋
I almost got fired when I was a Java tutor at my college for writing students' code, but nevertheless...
I'm gonna try to teach, not solve. You need to use a boolean here. Booleans are true/false.
Also, you need to learn to make your own method, and have it return a value. Here I created a checkValue method to see if the input matches the coin toss. It returns a 0 if it was a matching head, 1 if it was a matching tail, 2 if it was not a match, and 3 if the input was not an 'h' or 't'. Sound logical?
public void main(String[] args) {
int returnValue;
boolean did_i_find_a_match = false; //default this to false
boolean was_it_a_head = false; //default this to false
boolean did_user_enter_bad_input = true; //default this to true
String strToss; //Find a way to assign 'h' 50% of the time, 't' 50% of the time. Hint: MATH class
Do {
System.out.println("Please enter 'h' or 't'. Case Insensitive.");
String input = Console.ReadLine(); //This may depend upon your I/O classes
returnValue = checkValue(input, strToss); //Runs the method many lines below called 'checkValue'
if (returnValue == 0) {
did_i_find_a_match = true;
was_it_a_head = true;
did_user_enter_bad_input = false;
}
if (returnValue == 1) {
did_i_find_a_match = true;
was_it_a_head = false;
did_user_enter_bad_input = false;
}
if (returnValue == 2) {
did_i_find_a_match = false;
did_user_enter_bad_input = false;
}
/*
You could see if checkValue returned 3 here, but it's unnecessary. It would look like this:
if (returnValue == 3) {
did_i_find_a_match = false;
did_user_enter_bad_input = true;
}
*/
} while (did_user_enter_bad_input == true);
public int checkValue(String strInput, String strToss) {
if (strToss.equalsIgnoreCase (strInput)) {
if (strInput.equalsIgnoreCase ('h') {
return 0; //toss was an h, guess was an h!
} else {
return 1; //toss was a t, guess was a t!
}
} else if (strInput.equalsIgnoreCase ('h') || strInput.equalsIgnoreCase('t')) {
return 2; //right input, non-match!
} else {
return 3; // bad input!
}
}
The above code is verbose, and meant to help you learn programming concepts.
2006-09-15 05:58:17
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
Beware of the separators (or punctuation) and some other mistakes shown below.
if ((Toss3.equals(h)); << Remove this semicolon.
numberHead3 = 1;
if (Toss3.equals(t)) << Add an "else" in front of the "if".
numberTail3 = 1 << Add a semicolon.
else
numberHead3 = 0;
numberTail3 = 0;
invalidInput3 = 1;
System.out.println("Invalid input, please enter 'h', 'H', 't', or 'T'");
Also try the code provided by SmartSpider.
2006-09-15 05:57:30
·
answer #3
·
answered by papyrus 4
·
0⤊
0⤋
I think if you want the same else statement to two separate if statements, you would just have to repeat it:
if (Toss3.equals(h)) {
numberHead3 = 1;
}
else {
numberHead3 = 0;
numberTail3 = 0;
invalidInput3 = 1;
System.out.println("Invalid input, please enter 'h', 'H', 't', or 'T'");
}
if (Toss3.equals(t)) {
numberTail3 = 1;
}
else {
numberHead3 = 0;
numberTail3 = 0;
invalidInput3 = 1;
System.out.println("Invalid input, please enter 'h', 'H', 't', or 'T'");
}
An if statement can only have 1 else. It can have multiple else-if's though.
2006-09-15 05:58:31
·
answer #4
·
answered by clievers 4
·
0⤊
0⤋
Try these,
http://java.sun.com/docs/books/tutorial/
http://www.codetoad.com/java/applets/
Have fun but be safe!
2006-09-15 05:16:37
·
answer #5
·
answered by Anonymous
·
0⤊
0⤋