有没有不同的写法,如果..否则..在这种情况下陈述句子?


public class Sales {
public static void main(String[] args) {
int ursales = 6000;
int target = 3000;
if (ursales >= 2 * target) {
System.out.println("performance Excellent");
System.out.println("bouns 1000");
} else if (ursales >= 1.5 * target) {
System.out.println("performance Fine");
System.out.println("bouns 500");
} else if (ursales >= target) {
System.out.println("performance Satisfactory");
System.out.println("bouns 100");
} else {
System.out.println("You Are Fired!");
}
}
}

我试着把每个语句写成:

performance = "...";
bonus = "...";

但它没有用。

有人可以告诉我是否有其他可能的方法可以在没有System.out.println的情况下编写此语句?

你准备好啰嗦了吗?我们定义了一个奖金接口,用于确定销售是否达到实施定义的目标。然后,我们创建一个按顺序排序的流,然后在过滤器满足的流中返回第一个结果。这种方法的好处是,如果您想在未来添加更多奖金,那么这样做非常容易。

interface Bonus {
boolean hit(int sales, int target);
}
static class ExcellentBonus implements Bonus {
@Override
public boolean hit(int sales, int target) {
return sales >= target * 2;
}
}
static class FineBonus implements Bonus {
@Override
public boolean hit(int sales, int target) {
return sales >= target * 1.5;
}
}
static class SatisfactoryBonus implements Bonus {

@Override
public boolean hit(int sales, int target) {
return sales >= target;
}
}
static class FiredBonus implements Bonus {
@Override
public boolean hit(int sales, int target) {
return sales < target;
}
}
public static void main(String[] args) {
int sales = 100;
int target = 50;
Bonus bonus = Stream.of(new ExcellentBonus(), new FineBonus(), new SatisfactoryBonus())
.filter(b -> b.hit(sales, target)).findFirst().orElse(new FiredBonus());
}

这样吗?

String performance;
int bonus;
if (ursales >= 2 * target) {
performance = "performance Excellent";
bonus = 1000;
} else if (ursales >= 1.5 * target) {
performance = "fine";
bonus = 500;
} else ... etc..
System.out.println("performance " + performance );
System.out.printkn("bouns " +  bonus );

您可以使用System.out.print()代替使用System.out.println()。打印到控制台时,这不会创建任何新行。

您可以使用 StringBuilder 连接您需要的所有字符串,并在最后一次打印它们。

此解决方案类似于@Jason的答案,但使用的协定略有不同。它定义类来表示性能评估结果的方式类似。它的不同之处在于它允许定义绩效评估的因素映射。

Ideone demo

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Main {
public static void main(final String... args) {
final String FORMAT = "Performance %snbonus %d";
final Map<Double, PerformanceResult> performanceResultMap = new HashMap<>();
performanceResultMap.put(2.0, new PerformanceResult("Excellent", 1000));
performanceResultMap.put(1.5, new PerformanceResult("Fine", 500));
performanceResultMap.put(1.0, new PerformanceResult("Satisfactory", 100));
final PerformanceResult excellent =
calculatePerformance(200.0, 100.0, performanceResultMap);
System.out.println(String.format(FORMAT, excellent.getMessage(), excellent.getBonus()));
final PerformanceResult fine = calculatePerformance(150.0, 100.0, performanceResultMap);
System.out.println(String.format(FORMAT, fine.getMessage(), fine.getBonus()));
final PerformanceResult satisfactory =
calculatePerformance(100.0, 100.0, performanceResultMap);
System.out.println(String.format(FORMAT, satisfactory.getMessage(), satisfactory.getBonus()));
try {
calculatePerformance(0, 100, performanceResultMap);
throw new IllegalStateException("Exception should have been thrown");
} catch (final NoFittingPerformanceResultFoundException e) {
System.out.println("Expected exception thrown");
}
}
public static PerformanceResult calculatePerformance(
final double actual,
final double target,
final Map<Double, PerformanceResult> performanceResultMap) {
return performanceResultMap.keySet().stream()
.sorted(Collections.reverseOrder())
.filter(factor -> actual >= target * factor)
.findFirst()
.map(performanceResultMap::get)
.orElseThrow(NoFittingPerformanceResultFoundException::new);
}
}
class PerformanceResult {
private final String message;
private final int bonus;
PerformanceResult(final String message, final int bonus) {
this.message = Objects.requireNonNull(message);
this.bonus = bonus;
}
public String getMessage() {
return message;
}
public int getBonus() {
return bonus;
}
}
class NoFittingPerformanceResultFoundException extends IllegalStateException {}