Java 需要您的建议来降低给定场景的复杂性吗
在我的情况下,是否有可能减少if条件 下面的程序已经得到了买家的数量 我正在检查与价格有关的买方数量Java 需要您的建议来降低给定场景的复杂性吗,java,Java,在我的情况下,是否有可能减少if条件 下面的程序已经得到了买家的数量 我正在检查与价格有关的买方数量 package com; import java.util.ArrayList; public class Test extends Thread { public static void main(String[] args) { double startPrice = 235.80; int marketQuantity = 0;
package com;
import java.util.ArrayList;
public class Test extends Thread {
public static void main(String[] args) {
double startPrice = 235.80;
int marketQuantity = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
double buyer1 = 234.50; int quantity1 = 100;
double buyer2 = 235.55; int quantity2 = 200;
double buyer3 = 232.55; int quantity3 = 300;
// Positve Conditions
if (buyer1 >= startPrice) {
marketQuantity = quantity1;
}
if (buyer2 >= startPrice) {
marketQuantity = marketQuantity + quantity2;
}
if (buyer3 >= startPrice) {
marketQuantity = marketQuantity + quantity3;
}
// negative conditions
// for condition less than 1
if(buyer1-startPrice<=-1)
{
lessthan1 = quantity1;
}
if(buyer2-startPrice<=-1)
{
lessthan1 = lessthan1+quantity2;
}
if(buyer3-startPrice<=-1)
{
lessthan1 = lessthan1+quantity3;
}
// similarly i need to implement lessthan2 and lessthan3 conditions also
// can this be improved
}
}
这可能是一个更适合该网站的问题。如果你有静态值,你不需要任何If,因为你知道结果。你可以通过简单地遍历买家和数量列表来简化这个问题。因此,将这两个列表添加到Arraylist中,并使用for loop对每个列表进行迭代。我昨天看到了一个类似的问题: