[英]Stuck on Java program (java.io.*;)?
我程序的目的是询问温度(F)是多少,以及外面的天气情况如何。
天气条件可以是晴天(1),下雨(2),多云(3)或下雪(4.)数字1-4将用于阐明天气条件(我不确定该怎么做还有其他方法...)
然后,根据temp
和weatherCondition
的组合,我希望能够根据temp
和weatherCondition
的组合显示10种选择中的3件衣服。
我仍在学习,因此如果我的问题似乎很平凡,我深表歉意...
当用户输入temp
和weatherCondition
,将根据两个输入的组合(例如,热晴天, weatherCondition
)给出响应。
相反,我想创建一个或多个txt文件,并让每个文件都命名为hotSunny.txt 。 在这些txt文件中,我列出了10种服装。 我最终希望程序识别出哪个组合匹配其适当的txt文件,然后随机显示10个中的3个。
我到目前为止所拥有的...
public static void main(String[] args)
{
double temperature;
int weatherCondition;
String input;
input = JOptionPane.showInputDialog("What is " +
"the current temperature?");
temperature = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Sweet I now know the temperature! " +
"Now please take a look out the nearest window is it Sunny , Rainy ," +
" Cloudy or Snowy? " +
"(1 = Sunny) (2 = Raining) " +
"(3 = Cloudy) (4 = Snowing)");
weatherCondition = Integer.parseInt(input);
if (temperature <= 32){
if (weatherCondition == 4){
freezingSnowing();
} else if (weatherCondition == 3){
freezingCloudy();
} else if (weatherCondition == 2){
freezingRain();
} else {
freezingSunny();
}
}..........
else if ((temperature >= 33) && (temperature <= 50)) {
else if ((temperature >= 51) && (temperature <= 75)) {
else if ((temperature >= 76) && (temperature <= 140)) {
public static void freezingSnowing()
{
JOptionPane.showMessageDialog(null, "It's is snowing! I recommend that you dress very warm" +
"and wear a large coat that is preferably water proof.");
}
您的freezingSnowing
方法应如下所示:
public static void freezingSnowing() {
file = new File(MyWeatherApp.class.getResource
(path + "freezingSnowing.txt"));
// path to the txt file
// where path is the local path to the file
scanner = new Scanner(file);
ArrayList<String> garments = new ArrayList<>(10);
while(scanner.hasNextLine()) {
garments.add(scanner.nextLine());
}
ArrayList<Integer> indices = new ArrayList<>(3);
for(int i = 0; i < 3; i++) {
while(true) { // watch out for duplicates
int rand = (int)(Math.random() * 9);
if(!indices.contains(rand))
break;
}
indices.add(rand);
JOptionPane.showMessageDialog(null, "It's is snowing! " +
"I recommend that you dress very warm " +
"and wear " + garments.get(indices.get(1)) +
", " garments.get(indices.get(2)) +
" and " + garments.get(indices.get(3)) +
".");
}
这是我的随机选择项目版本。
public static void main(String[] args) {
String[] weatherCond = new String[] {"cold", "hot"};
ArrayList<String> garmets = new ArrayList<String>();
garmets.add("clothes");
garmets.add("hat");
garmets.add("gloves");
garmets.add("coat");
ArrayList<String> pick;
int ITEM = 3;
int temperature = 29;
if (temperature >= 30) { // hot condition
System.out.println("weather condition " + weatherCond[0]);
pick = garmets;
for (int i = 0; i < ITEM; i++) {
int idx = (int) (Math.round(Math.random() * pick.size()) % pick.size());
System.out.print(pick.get(idx) + " " );
pick.remove(idx);
}
} else {
System.out.println("weather condition " + weatherCond[1]);
pick = garmets;
for (int i = 0; i < ITEM; i++) {
int idx = (int) (Math.round(Math.random() * pick.size()) % pick.size());
System.out.print(pick.get(idx) + " " );
pick.remove(idx);
}
}
}
另外,如果你想使用一个固定组garmets的特定气候条件下,你可以使用它使用的天气状况作为重点和garmet组作为值HashMap中 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.