English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A simple question:

How do i put these statements together into one if?
if (Toss1.equals(h))
numberHead1 = 1;

and

if (Toss1.equals(t))
numberTail1 = 1;

Help please!

2006-09-15 05:28:15 · 3 answers · asked by ? 1 in Computers & Internet Programming & Design

3 answers

Let's assume that the only possible outcomes of the coin toss is either h or t. This means that if Toss1 does not equal h, then it must equal t. So, you only need to examine Toss1 once. Use an else with your if, as other posters suggested, but don't test Toss1 again.

I'm not quite giving you the code, because I think you should think it through yourself. But hopefully this will be helpful.

2006-09-15 05:49:44 · answer #1 · answered by arbeit 4 · 0 0

It can be like this:
if (Toss1.equals(h)) {
numberHead1 = 1;
} else if (Toss1.equals(t)) {
numberTail1 = 1;
}

As for Elmer Fudd's code:
if (Toss1.equals(h) AND (Toss1.equals(t)) {
numberHead1=1;
numberTail1=1;
}

I don't think that it'll work because h and t will have to be equal for
numberHead1=1;
numberTail1=1;
to be carried out, which in this situation is not the case.

But a word of advice: If the two statements can function well, you do not need to combine them.

2006-09-15 12:43:13 · answer #2 · answered by papyrus 4 · 0 0

if (Toss1.equals(h) AND (Toss1.equals(t)) {
numberHead1=1;
numberTail1=1;
}

2006-09-15 12:37:36 · answer #3 · answered by Elmer Fudd 6 · 0 0

fedest.com, questions and answers