0

我的程序的目的是询问温度(F)是多少以及外面的天气状况如何。

天气状况可以是晴天(1),下雨(2),多云(3)或下雪(4)。数字1-4将用于说明天气状况(我不知道该怎么做任何其他方式...)

然后,根据 和 的组合,tempweatherCondition希望能够根据 和 的组合显示 10 种选择中的 3 件temp服装weatherCondition

我还在学习,所以如果我的问题或问题看起来很平凡,我深表歉意......

在用户输入temp和的那一刻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.");
} 
4

2 回答 2

0

您的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)) +
                ".");
}
于 2013-03-14T02:15:46.500 回答
0

这是我随机挑选物品的版本。

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

此外,如果您想针对特定天气条件使用一组固定的服装,您可以使用哈希图,该哈希图使用天气条件作为键,服装组作为值。

于 2013-03-14T02:32:00.963 回答