0

学习互联网的人。我目前正在为一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,对于这个小问题的任何帮助将不胜感激。

该程序本质上是一个数字猜谜游戏,它将猜测存储和排序到一个数组列表中,一旦你猜对了,就会显示平均值、最低猜测、正确猜测的索引数组、平均值和转换后的华氏温度. 它在 9 到 40 之间,因为作业指定了 40 和我学生证上的最高数字。当我可以简单地将它们归为一个时,不必要的类数量的原因也是分配规范错误。

我遇到的麻烦是将温度猜测(无论正确与否)存储在数组列表中。我相信我已经在我的构造函数中正确初始化了数组列表,但我不确定如何或在哪里捕获来自 jTextField 的猜测。一旦我弄清楚这一点,我就可以合并我为显示结果编写的其他方法并完成这项任务。

再次,我为我的一般新手道歉,任何帮助将不胜感激。

package TemperatureGuessApp;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;

public class TemperatureFrame extends JFrame {

public JFrame mainFrame;
public JLabel prompt1, prompt2;
public JTextField userInput;
public JLabel comment;
public JButton restart;
public int randomTemperature;
public int min = 9;
public int max = 40;
public int guessTemperature;
public int sumTemperature;
public double avgTemperature;
public int lowestTemperature;
public int convertedTemperature;
public int indexTemperature;
public Color background;

public TemperatureFrame() {

    super("Temperature Guess/Conversion Application");
    prompt1 = new JLabel("Randomly generated temperature is between 9 and 40.");
    prompt2 = new JLabel("Write temperature (your guess) or -1 (for exit) and press enter key:");
    userInput = new JTextField(5);
    userInput.addActionListener(new GuessHandler());
    ArrayList<Integer> guesses = new ArrayList<>();
    comment = new JLabel("The results will be shown here.");
    restart = new JButton("Start Again - Generate A New Temperature");
    restart.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    userInput.setText("");
                    comment.setText("The results will be shown here.");
                    RandomTemperature();
                    userInput.setEditable(true);
                }
            });

    setLayout(new FlowLayout());
    background = Color.LIGHT_GRAY;

    add(prompt1);
    add(prompt2);
    add(userInput);
    add(comment);
    add(restart);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 150);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    RandomTemperature();
    ConvertTemperature();
}

public void RandomTemperature() {
    Random random = new Random();
    randomTemperature = random.nextInt(max - min) + min;
}
public void SortTemperature() {   
}
public void FindLowestTemperature() {    
}
public void FindAverageTemperature() {    
}
public void FindIndexTemperature() {    
}
public void ConvertTemperature() {
    convertedTemperature = randomTemperature * 9 / 5 + 32;
}

class GuessHandler implements ActionListener {

    @ Override
    public void actionPerformed(ActionEvent e) {
        {

            guessTemperature = Integer.parseInt(userInput.getText());
            {   

                if (guessTemperature > randomTemperature) {
                    comment.setText("Temperature guessed is higher than the random temperature.");
                    userInput.setText("");
                    userInput.setEditable(true);
                }

                if (guessTemperature < randomTemperature) {
                    comment.setText("Temperature guessed is lower than the random temperature.");
                    userInput.setText("");
                    userInput.setEditable(true);
                }

                if (guessTemperature == randomTemperature) {
                    comment.setText("Temperature guessed is equal to the random temperature.");
                    JOptionPane.showMessageDialog(null, "Temperature guessed is equal to the random temperature.\n\n1. Lowest temperature is:" + lowestTemperature + "\n2. Average temperature is:" + avgTemperature + "\n3. Array index of correctly guessed temperture is:" + indexTemperature + "\n4. Temperature in Farenheit is:" + convertedTemperature + "\n\nThank you for playing!");
                } 
                else if (guessTemperature == -1) {
                    System.exit(0);
                }
            }
        }
    }
}    
}
4

1 回答 1

0

1.首先创建一个Pojo来存储玩家的所有数据...

例如

public class Player{

    int prompt1;
    int prompt2;
    int userInput;
    String comment;
    int restart;

  }

2.然后将它们存储在ArrayList......

//////////////////////已编辑//////////////////// ////////

public class Player{

        int prompt1;
        int prompt2;
        int userInput;
        String comment;
        int restart;

    public Player(int p1, int p2, int usInput, String c, int res){

       this.prompt1 = p1;
       this.prompt2 = p2;
       this.userInput = usInput;
       this.comment = c;
       this.restart = res ;


    }

}

 public class Test{

   ArrayList<Player> arList = new ArrayList<Player>();

   arList(new Player(p1, p2, usInput, c,  res));  // These Arguments u must declare and initialize....


  }

///////////////////已编辑////////////////////// /

好的,可以在不使用 POJO的情况下将int guessTemperature添加到ArrayListfrom中,可以通过这种方式完成。JTextField

ArrayList<Integer> arList = new ArrayList<Integer>();

String str = jText.getText().toString();

int gTemp = Integer.parseInt(str);

arList.add(gTemp);

现在假设你有这个 Player 有 4 个属性,比如 TemperatureInput、Name、Age、Score 等等......并且有 N 个玩家正在玩这个游戏......那么你应该使用 Pojo 和 Collection(即......列表,设置,地图)

public class Player{

    int guessTemp;
    String name;
    int age;
    long score;


    public Player(int gTemp, int name, int age, int score){

      this.guessTemp = gTemp;
      this.name = name;
      this.age = age;
      this.score = score;
    }

   public int getGuessTemp() {
        return guessTemp;
    }
    public void setGuessTemp(int guessTemp) {
        this.guessTemp = guessTemp;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public long getScore() {
        return score;
    }
    public void setScore(long score) {
        this.score = score;
    }


}

现在......从另一个班级应该是这样的......

public class Test{


public static void main(String[] args){

        ArrayList<Player> pList = new ArrayList<Player>();

       String name = jName.getText().toString();
       long score = Long.parseLong(jScore.getText().toString());
       int age =  Integer.parseInt(jAge.getText().toString());
       int gTemp = Integer.parseInt(jTemp.getText().toString());


       pList.add(new Player(gTemp, name, age, score));


   }


}
于 2012-09-05T11:02:08.263 回答