简体   繁体   English

坚持使用Java程序(java.io. *;)?

[英]Stuck on Java program (java.io.*;)?

The purpose of my program is to ask what the temperature(F) is and what the weather condition outside is like. 我程序的目的是询问温度(F)是多少,以及外面的天气情况如何。

The weather condition can be either sunny(1), raining(2), cloudy(3) or snowing(4.) The numbers 1-4 will be used to clarify what the weather condition is (I'm not sure how to do it any other way...) 天气条件可以是晴天(1),下雨(2),多云(3)或下雪(4.)数字1-4将用于阐明天气条件(我不确定该怎么做还有其他方法...)

Then, depending on the combination of temp and weatherCondition I want to be able to display 3 garments out of 10 choices, based on the combo of temp and weatherCondition . 然后,根据tempweatherCondition的组合,我希望能够根据tempweatherCondition的组合显示10种选择中的3件衣服。

I'm still learning so I apologize if my question or problem seems mundane... 我仍在学习,因此如果我的问题似乎很平凡,我深表歉意...

At the moment when a user enters the temp and weatherCondition , a response is given depending on the combo of the two inputs (ex. hot-sunny, freezing-snowing). 当用户输入tempweatherCondition ,将根据两个输入的组合(例如,热晴天, weatherCondition )给出响应。

Instead, I would like to create one or more txt files and have each one named something like hotSunny.txt for example. 相反,我想创建一个或多个txt文件,并让每个文件都命名为hotSunny.txt Inside these txt files I've listed 10 types of garments. 在这些txt文件中,我列出了10种服装。 I ultimately want the program to recognize which combo matches its appropriate txt file and then randomly display 3 of the 10. 我最终希望程序识别出哪个组合匹配其适当的txt文件,然后随机显示10个中的3个。

What I've got so far... 我到目前为止所拥有的...

   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.");
} 

Your freezingSnowing method should look like this: 您的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)) +
                ".");
}

This is my version of randomly picking item. 这是我的随机选择项目版本。

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);
        }
    }
}

Also, if you want to use a fix set of garmets for a specific weather condition, you could use a hashmap which uses weather condition as a key and garmet groups as a value. 另外,如果你想使用一个固定组garmets的特定气候条件下,你可以使用它使用的天气状况作为重点和garmet组作为值HashMap中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM