【问题标题】:Trouble storing input from jtextfield in arraylist for java GUI temperature guessing/conversion game在 java GUI 温度猜测/转换游戏的数组列表中存储来自 jtextfield 的输入时出现问题
【发布时间】:2012-09-05 10:52:03
【问题描述】:

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

该程序本质上是一个数字猜谜游戏,它将猜测存储和排序到一个数组列表中,一旦你猜对了,就会显示平均值、最低猜测、正确猜测的索引数组、平均值和转换后的值华氏温度。它在 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);
                }
            }
        }
    }
}    
}

【问题讨论】:

    标签: java input arraylist jtextfield


    【解决方案1】:

    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....
    
    
      }
    

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

    ok 好的...将int guessTemperature 添加到ArrayList from JTextField WITHOUT USING POJO ,可以通过这种方式完成。

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

    【讨论】:

    • No....我的意思是...您要存储的所有玩家数据...必须封装在一个名为 Player..的 Pojo 中,然后将每个 Player 对象存储在一个 ArrayList 中.....
    • 抱歉,我还是没听懂。我需要将输入到 jTextField userInput 中的 intguessTemperature 存储在数组列表中。我只是不明白 pojo 的创建是如何做到的。希望我在这里不会太健忘,我非常感谢您的帮助。
    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多