在java GUI温度猜测/转换游戏的arraylist中存储来自jtextfield的输入时出现问题
互联网上有学问的人。我目前正在为一个一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,任何对这个小问题的帮助都将不胜感激在java GUI温度猜测/转换游戏的arraylist中存储来自jtextfield的输入时出现问题,java,input,arraylist,jtextfield,Java,Input,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; impo
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);
}
}
}
}
}
}
该程序本质上是一个数字猜测游戏,它将猜测存储并排序到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);
}
}
}
}
}
}
包装温度UESSApp;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Random;
导入javax.swing.*;
公共类TemperatureName扩展JFrame{
公共JFrame主机;
公共标签提示1、提示2;
公共JTextField用户输入;
公众评论;
公共按钮重启;
公众温度;
公共int最小值=9;
公共int最大值=40;
公共温度;
公共温度;
公共双平均温度;
公共室内最低温度;
公共int转换温度;
公共温度指数;
公共色彩背景;
公共温度名称(){
超级(“温度猜测/转换应用”);
prompt1=新的JLabel(“随机生成的温度介于9和40之间”);
prompt2=新的JLabel(“写入温度(您的猜测)或-1(用于退出)并按回车键:”;
用户输入=新的JTextField(5);
addActionListener(新的GuessHandler());
ArrayList guesses=新的ArrayList();
comment=newjlabel(“结果将显示在此处”);
重新启动=新JButton(“重新启动-生成新温度”);
restart.addActionListener(
新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
userInput.setText(“”);
setText(“结果将显示在这里。”);
随机温度();
userInput.setEditable(true);
}
});
setLayout(新的FlowLayout());
背景=颜色。浅灰色;
增加(提示1);
增加(提示2);
添加(用户输入);
添加(评论);
添加(重新启动);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(500150);
setLocationRelativeTo(空);
setVisible(真);
可设置大小(假);
随机温度();
转换温度();
}
公众温度(){
随机=新随机();
随机温度=随机。下一个(最大-最小)+最小;
}
公共无效SortTemperature(){
}
public void FindLowestTemperature(){
}
public void FindAverageTemperature(){
}
public void FindIndexTemperature(){
}
公众温度(){
转换温度=随机温度*9/5+32;
}
类GuessHandler实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
{
guessTemperature=Integer.parseInt(userInput.getText());
{
如果(猜测温度>随机温度){
setText(“猜测的温度高于随机温度”);
userInput.setText(“”);
userInput.setEditable(true);
}
if(猜测温度<随机温度){
setText(“猜测的温度低于随机温度”);
userInput.setText(“”);
userInput.setEditable(true);
}
如果(猜测温度==随机温度){
setText(“猜测的温度等于随机温度”);
JOptionPane.showMessageDialog(null,“猜测的温度等于随机温度”。\n\n1.最低温度为:“+LowerstTemperature+”\n2.平均温度为:“+AvgtTemperature+”\n3.正确猜测的温度的数组索引为:“+IndextTemperature+”\n4.法伦赫温度为:“+convertedTemperature+”“\n\n感谢您的参与!”;
}
否则如果(猜测温度==-1){
系统出口(0);
}
}
}
}
}
}
1.首先创建一个Pojo
来存储玩家的所有数据
例如
public class Player{
int prompt1;
int prompt2;
int userInput;
String comment;
int restart;
}
2.然后将它们存储在ArrayList
///////////Edited///////////////strong>
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 ;
}
}
现在假设这个玩家有4个属性,比如温度输入、姓名、年龄、分数……等等,有N个玩家在玩这个游戏。那么你应该使用Pojo和集合(即列表、集合、地图)
现在…从另一节课上应该是这样的
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));
}
}
公共类测试{
公共静态void main(字符串[]args){
ArrayList pList=新的ArrayList();
String name=jName.getText().toString();
long score=long.parseLong(jScore.getText().toString());
int age=Integer.parseInt(j
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));
}
}