我编写了一个程序,通过调用方法来打印“根据可能的温度” A,B,C或D季节。
代码:
import java.util.Scanner;
import javax.swing.*;
public class Seasons {
public static void main(String[] args) {
int inputTemp;
String response = JOptionPane.showInputDialog(null,
"Enter the temperature");
inputTemp = Integer.parseInt(response);
String message = "Based on the temperature of " + inputTemp
+ " it is most likely " + determineSeason(inputTemp);
JOptionPane.showMessageDialog(null, message);
}
public static String determineSeason(int inputTemp) {
String season = null;
if (inputTemp > 130 || inputTemp < -20) {
JOptionPane.showMessageDialog(null,"Invalid");
}
else if (inputTemp >= 90) {
JOptionPane.showMessageDialog(null,"summer"); }
else if (inputTemp >= 70 && inputTemp < 90) {
JOptionPane.showMessageDialog(null,"spring"); }
else {
JOptionPane.showMessageDialog(null,"winter"); }
return season;
}
}
程序从我的方法返回的季节是JOptionPane,然后在主方法中显示JOptionPane,我需要说“基于季节是季节A,B,C或D的温度。
对于我所缺少的任何建议,将不胜感激!
答案 0 :(得分:1)
您没有将结果分配给season
,而只返回了null
。
将if语句中的每个条件更改为与此类似的内容。
String season = null;
if (inputTemp > 130 || inputTemp < -20) {
season = "invalid";
} else if(inputTemp >= 90) {
season = "summer";
}
答案 1 :(得分:1)
determineSeason
应该是不处理UI部件(不与Swing组件交互)的私有方法。
private String determineSeason(int inputTemp) {
if (inputTemp > 130 || inputTemp < -20) {
throw new IllegalArgumentException("Invalid");
}
if (inputTemp >= 90) {
return "summer";
} else if (inputTemp >= 70 && inputTemp < 90) {
return "spring";
} else {
return "winter";
}
}
season
不能为"Invalid"
,您不会显示"it is most likely Invalid"
,因此最好从方法中抛出异常,在调用方捕获并显示错误消息窗口:
class Seasons {
public static void main(String[] args) {
String response = JOptionPane.showInputDialog(null, "Enter the temperature");
int inputTemp = Integer.parseInt(response);
try {
String message = String.format("Based on the temperature of %d, it is most likely %s", inputTemp, determineSeason(inputTemp));
JOptionPane.showMessageDialog(null, message);
} catch (IllegalTemperatureValueException | NumberFormatException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private static String determineSeason(int inputTemp) throws IllegalTemperatureValueException {
if (inputTemp > 130 || inputTemp < -20) {
throw new IllegalTemperatureValueException("incorrect temperature value");
}
return inputTemp >= 90 ? "summer" : (inputTemp >= 70 ? "spring" : "winter");
}
}
class IllegalTemperatureValueException extends Exception {
public IllegalTemperatureValueException(String message) {
super(message);
}
}
答案 2 :(得分:0)
我建议您使用接口和Java 8 Streams。这样一来,您就避免了很多if,这使代码不太冗长,并且引入新类变得很简单。
即使在小型或简单的项目中,也要考虑面向接口的编程,这使您在声明新方法之前要多加考虑。
public abstract class Season {
public <T extends Season> Stream season(Integer temperature) {
if (temperature <= max() && temperature >= min()) {
return Stream.of(this);
}
return Stream.empty();
}
public abstract Integer max();
public abstract Integer min();
@Override
public abstract String toString();
}
public class Summer extends Season {
@Override
public String toString() {
return "Summer";
}
@Override
public Integer max() {
return 90;
}
@Override
public Integer min() {
return 70;
}
}
public class Spring extends Season {
@Override
public String toString() {
return "Spring";
}
@Override
public Integer max() {
return 69;
}
@Override
public Integer min() {
return 40;
}
}
public class Main {
public static void main(String[] args) {
Collection<Season> seasons = new ArrayList();
seasons.add(new Summer());
seasons.add(new Winter());
seasons.add(new Season() {
@Override
public Integer max() {
return 39;
}
@Override
public Integer min() {
return 10;
}
@Override
public String toString() {
return "Winter";
}
});
String response = JOptionPane.showInputDialog(null, "Enter the temperature");
int temperature = Integer.parseInt(response);
String msg = "Based on the temperature of %d it is most likely %s%n";
seasons.stream().forEach(s -> {
s.season(temperature).findAny().ifPresent(t -> {
String message = String.format(msg, temperature, t);
JOptionPane.showMessageDialog(null, message);
});
});
}
}