【问题标题】:I need to check if a number is equal,2x,3x,4x,5x greater than the other. How can i do this?我需要检查一个数字是否等于、2x、3x、4x、5x 大于另一个。我怎样才能做到这一点?
【发布时间】:2021-12-31 00:39:21
【问题描述】:

基本上我只需要这个程序告诉我如果 a>b 和多少。所以我需要检查的条件是 if a==b, a>b,bb||a/b 的第一个条件下运行不快0)

import java.util.Scanner;

公共类主{

public static void main(String[] args) {



    Scanner input =new Scanner(System.in);

    System.out.println("what is the value for a");
    double a =input.nextDouble();
    System.out.println("what is the value for b");
    double b =input.nextDouble();


    if (a==b){
        System.out.println("a is equal to b");
    }
    else if (a>b||a/b<1||a/b>0){
        System.out.println("a is slightly bigger than b");

    }
    else if (a>b||a/b<3||a/b>=2) {
        System.out.println("a is 2x bigger than b");
    }
    else if (a>b||a/b<4||a/b>=3) {
        System.out.println("a is 3x bigger than b");
    }
    else if (a>b||a/b<5||a/b>=4) {
        System.out.println("a is 4x bigger than b");
    }

    else if (a>b||a/b<6||a/b>=5) {
        System.out.println("a is 5x bigger than b");
    }

    else if (a>b||a/b<100||a/b>6){
        System.out.println("a is alot bigger than b");
    }
    // to check if b is bigger than a
    else if (a<b||b/a<1||b/a>0){
        System.out.println("b is slightly bigger than a");

    }
    else if (a<b||b/a<3||b/a>=2) {
        System.out.println("b is 2x bigger than a");
    }
    else if (a<b||b/a<4||b/a>=3) {
        System.out.println("b is 3x bigger than a");
    }
    else if (a<b||b/a<5||b/a>=4) {
        System.out.println("b is 4x bigger than a");
    }

    else if (a<b||b/a<6||b/a>=5) {
        System.out.println("b is 5x bigger than a");
    }

// 检查它是否更大

    else if (a>b||a/b<100||a/b>6){
        System.out.println("a is alot bigger than b");



    }



}}

【问题讨论】:

    标签: java if-statement comparison operators


    【解决方案1】:

    如果 a 大于 b,则第二个条件始终为真,即使 a “比 b 大很多”。你确定你不想要 && 而不是 ||到处都是?

    另外,考虑添加另一个类似这样的 if 语句(以减少语句中的规则数量并提高可读性):

    if (a == b) {
      // Code
    } else if (a > b) {
      // If statements when a > b
    } else {
      // If statements when b > a
    }
    

    这是一个可以作为替代方案的解决方案:

    public class Test {
      public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("a: ");
        double a = input.nextDouble();
        System.out.print("b: ");
        double b = input.nextDouble();
    
        double greaterInt = (a > b) ? a : b;
        double smallerInt = (a > b) ? b : a;
        char greaterChar = (a > b) ? 'a' : 'b';
        char smallerChar = (a > b) ? 'b' : 'a';
    
        double frac = greaterInt / smallerInt;
        boolean noRemainder = 0 == greaterInt % smallerInt;
    
        String msg = "";
    
        if (noRemainder && frac <= 5) {
          if (frac == 1)
            msg = greaterChar + " and " + smallerChar + " are equal";
          else
            msg = greaterChar + " is " + (int) frac + "x bigger than " + smallerChar;
        } else if (frac < 2) {
          msg = greaterChar + " is slightly bigger than " + smallerChar;
        } else if (frac > 5) {
          msg = greaterChar + " is a lot bigger than " + smallerChar;
        } else {
          msg = "Nothing special to say about these numbers";
        }
    
        System.out.println(msg);
      }
    }
    

    【讨论】:

    • 哇,我完全看错了,我打算使用 and 运算符谢谢你现在是个天才它应该可以正常工作
    • 我已经编辑了我的帖子,为您提供代码的替代方案,这可能更容易阅读(没有那么多条件非常相似的 if 语句)。 @flowoverstack
    猜你喜欢
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多