提问人:flowoverstack 提问时间:11/21/2021 最后编辑:archflowoverstack 更新时间:11/30/2021 访问量:89
我需要检查一个数字是否相等,2x,3x,4x,5x。我该怎么做?
I need to check if a number is equal,2x,3x,4x,5x greater than the other. How can i do this?
问:
基本上,我只需要这个程序来告诉我,如果 a>b 以及多少。所以我需要检查的条件是 a==b、a>b、b<a 以及我还需要知道大多少倍,范围从 2x、3x、4x、5x、6x。例如,如果数字大于 6 倍,则只需打印“大得多”。 如果有任何其他有效的方法可以做到这一点,请给我建议。但是这段代码的问题在于它运行速度不快,第一个条件是 (a>b||一/二<1||A/B>0)
import java.util.Scanner;
public class 主 {
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");
}
}}
答:
1赞
Viktor
11/21/2021
#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);
}
}
评论
0赞
flowoverstack
11/21/2021
哇,我完全看过去了,我的意思是使用 and 运算符,谢谢你,你现在是个天才,它应该可以正常工作
0赞
Viktor
11/21/2021
我已经编辑了我的帖子,为您提供了代码的替代方案,这可能更容易阅读(如果具有非常相似条件的语句,则不会那么多)。@flowoverstack
评论