无法将来自jtextfield的输入存储在arraylist中用于java GUI温度猜测/转换游戏

时间:2012-09-05 10:52:04

标签: java input arraylist jtextfield

了解互联网的人。我正在为第一年的编程课程完成一项任务,老实说,编程并不是我最好的技能。所以对这个小问题的任何帮助都会非常感激。

该程序本质上是一个数字猜谜游戏,它存储并将猜测分类到一个arraylist并显示,一旦你猜对了,平均,最低猜测,正确猜测的索引数组,平均值和转换华氏温度。它在9到40之间,因为作业指定了40,而且我的学生ID上的编号最高。当我可以简单地将它们放入一个类中时,不必要的类数量的原因也是赋值规范错误。

我遇到的麻烦是在arraylist中存储温度猜测,无论是正确的还是不正确的。我相信我已经在我的构造函数中正确初始化了arraylist但我不确定如何或从何处捕获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);
                }
            }
        }
    }
}    
}

1 个答案:

答案 0 :(得分: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....


  }

<强> ///////////////////////被修改//////////////////// ///////

好的......可以将int guessTemperature 添加到来自ArrayList JTextField而不使用POJO 就这样。

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

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

int gTemp = Integer.parseInt(str);

arList.add(gTemp);

现在假设你有这个播放器有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));


   }


}