简体   繁体   中英

how do I fix my method to output one answer?

This is the method I have that shows if two numbers are equal to each other. The last if statement i have is if all the numbers are the same but when I run this, it prints "two tied for second" and "all tied for first". how do i make it so if all of the numbers are the same, it will only output "all tied for first"?

public static void overlap(double a, double b, double c){ 
  if (a==b) {
       System.out.println("Two tied for second");
       }
  if (c==b) {
           System.out.println("Two tied for second");
       }
  if (c==a) {
           System.out.println("Two tied for second");
       }
  if(a==b && b==c && a==c) { 
       System.out.println("All tied for first");
       }
 }

This would be way cleaner.

public static void overlap(double a, double b, double c) {

    if (a == b && b == c && a == c) {
        System.out.println("All tied for first");
    } 
    else if (a == b || c == b || c == a) {
        System.out.println("Two tied for second");
    }
}

Try using else if .

Additionally put the last condition, which will otherwise be true much earlier, first:

if(a==b && b==c && a==c) { 
     System.out.println("All tied for first");
}
else if (a==b) {
     System.out.println("Two tied for second");
}
else if (c==b) {
     System.out.println("Two tied for second");
}
else if (c==a) {
     System.out.println("Two tied for second");
}

Put that statement in the beginning before you go onto other conditional statements. And make sure to use if and if else statements.

like other posts said, using else if is also a good idea, but the thing to remember is when it meets a condition on one else if, it won't go any further. so if it finds that a==b, it won't go any further to check c==b or c==a

How about?

public static void overlap(double a, double b, double c) { 
    if(a==b && b==c && a==c) { 
        System.out.println("a==b==c");
    }
    else {
        if (a==b) {
            System.out.println("a==b");
        }
        if (c==b) {
            System.out.println("c==b");
        }
        if (c==a) {
            System.out.println("c==a");
        }
    }
}

You have two issues.

The first is the order of evaluation of the 'if' statements. It sounds like you would like to evaluate the last one first.

The second problem is that you will want to print the result of the first 'if' statement that matches, and skip the rest.

There are a number of ways to do this. A popular way is to have a chain of if/else if statements, but I find that to be less readable than I like. So I use the following, somewhat nonstandard method:

do {
  if(a==b && b==c && a==c) { 
    System.out.println("All tied for first");
    break;
  }

  if (a==b) {
    System.out.println("Two tied for second");
    break;
  }

  if (c==b) {
    System.out.println("Two tied for second");
    break;
  }

  if (c==a) {
    System.out.println("Two tied for second");
    break;
  }

} while(false);

The issue with your code is that it evaluates each if statement independently. Thus, if you fail to specify an else statement, each if statement will execute if evaluated as TRUE , regardless of the order of statements.

Along the same lines as Moishe (and perhaps more intuitively for your purposes), you could also structure your code as so:

public static void overlap(double a, double b, double c){ 
    if(a==b && b==c && a==c) { 
       System.out.println("All tied for first");
       }
    }
    else {
          if (a==b) {
               System.out.println("Two tied for second");
          }
          if (c==b) {
               System.out.println("Two tied for second");
          }
          if (c==a) {
               System.out.println("Two tied for second");
          }
    }
}

However, if you're looking to streamline things a bit, you may consider restructuring things to prevent excessive comparisons:

public static void overlap(double a, double b, double c){ 
    if (a==b) {
       if(b==c && a==c) {
           System.out.println("All tied for first");
           }
       else {
           System.out.println("Two tied for second");
           }
       }
    else if (c==b) {
           System.out.println("Two tied for second");
       }
    else if (c==a) {
           System.out.println("Two tied for second");
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM