0

私のプログラムの目的は、気温(F)とは何か、外の気象条件はどのようなものかを尋ねることです。

気象条件は、晴れ(1)、雨(2)、曇り(3)、雪(4)のいずれかになります。1〜4の数字は、気象条件を明確にするために使用されます(方法がわかりません)。それ以外の方法で...)

次に、との組み合わせに応じて、との組み合わせに基づいて、10の選択肢から3つの衣服を表示できるようにしたいと思いtempます。weatherConditiontempweatherCondition

私はまだ学んでいるので、私の質問や問題が平凡に思える場合はお詫びします...

tempユーザーがとを入力した瞬間にweatherCondition、2つの入力の組み合わせ(例:暑い晴れ、凍るような雪)に応じて応答が返されます。

代わりに、1つ以上の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 に答える