Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arraylist未在Java GUI温度猜测游戏中存储用户输入_Java_Arrays_Arraylist_Store_User Input - Fatal编程技术网

Arraylist未在Java GUI温度猜测游戏中存储用户输入

Arraylist未在Java GUI温度猜测游戏中存储用户输入,java,arrays,arraylist,store,user-input,Java,Arrays,Arraylist,Store,User Input,我目前正在为一个一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,如果您能帮我解决这个小问题,我将不胜感激。我很抱歉因为我的新手身份而堵塞了网站 该程序本质上是一个使用温度的数字猜测游戏,它将来自JTextField的猜测(int guessTemperature)存储并排序到arraylist中,并通过JOptionMessage显示,一旦您正确猜测,最低猜测,正确猜测的索引数组,所有猜测的平均值和转换温度(以华氏度为单位)。它介于9和40之间,因为作业指定了40和我的学生ID上的最

我目前正在为一个一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,如果您能帮我解决这个小问题,我将不胜感激。我很抱歉因为我的新手身份而堵塞了网站

该程序本质上是一个使用温度的数字猜测游戏,它将来自JTextField的猜测(int guessTemperature)存储并排序到arraylist中,并通过JOptionMessage显示,一旦您正确猜测,最低猜测,正确猜测的索引数组,所有猜测的平均值和转换温度(以华氏度为单位)。它介于9和40之间,因为作业指定了40和我的学生ID上的最大数字。我没有包括avg或索引的方法,因为不起作用的arraylist使其多余

我遇到的麻烦是在arraylist中存储温度猜测,无论正确与否。我手动测试程序,从9点开始,一直到40点。因此,如果我的arraylist运行正常,在对最低猜测温度进行排序后的输出应始终为9,因为这始终是我开始的位置,但当前最低温度的输出是随机计算的数字和正确猜测。除非我的排序方法和查找最小值方法不正确,否则arraylist中除了最后一个之外并没有任何猜测。因此,如果有人能向我建议我的arraylist有什么错(或者如果一切都错了),说明它没有捕获猜测的每个实例,我将不胜感激

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 ArrayList<Integer> arList;

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());       
    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;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 150);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);        

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

    RandomTemperature();
    ConvertTemperature();   
}

public void RandomTemperature() {
    Random random = new Random();
    randomTemperature = random.nextInt(max - min) + min;
}    

public void ConvertTemperature() {
    convertedTemperature = randomTemperature * 9 / 5 + 32;        
}

class GuessHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        {
            arList = new ArrayList<>();
            String str;
            str = userInput.getText().toString();
            guessTemperature = Integer.parseInt(str);
            arList.add(guessTemperature);
            {   
                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) {
                    Collections.sort(arList);
                    lowestTemperature = Collections.min(arList);
                    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);
                }
            }
        }
    }
}      
import 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转换温度;
公共温度指数;
公共色彩背景;
公共ArrayList arList;
公共温度名称(){
超级(“温度猜测/转换应用”);
prompt1=新的JLabel(“随机生成的温度介于9和40之间”);
prompt2=新的JLabel(“写入温度(您的猜测)或-1(用于退出)并按回车键:”;
用户输入=新的JTextField(5);
addActionListener(新的GuessHandler());
comment=newjlabel(“结果将显示在此处”);
重新启动=新JButton(“重新启动-生成新温度”);
restart.addActionListener(
新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
userInput.setText(“”);
setText(“结果将显示在这里。”);
随机温度();
userInput.setEditable(true);
}
});
setLayout(新的FlowLayout());
背景=颜色。浅灰色;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(500150);
setLocationRelativeTo(空);
setVisible(真);
可设置大小(假);
增加(提示1);
增加(提示2);
添加(用户输入);
添加(评论);
添加(重新启动);
随机温度();
转换温度();
}
公众温度(){
随机=新随机();
随机温度=随机。下一个(最大-最小)+最小;
}    
公众温度(){
转换温度=随机温度*9/5+32;
}
类GuessHandler实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
{
arList=新的ArrayList();
字符串str;
str=userInput.getText().toString();
猜测温度=整数.parseInt(str);
arList.add(猜测温度);
{   
如果(猜测温度>随机温度){
setText(“猜测的温度高于随机温度”);
userInput.setText(“”);
userInput.setEditable(true);
}
if(猜测温度<随机温度){
setText(“猜测的温度低于随机温度”);
userInput.setText(“”);
userInput.setEditable(true);
}
如果(猜测温度==随机温度){
Collections.sort(arList);
最低温度=收集量.min(arList);
setText(“猜测的温度等于随机温度”);
JOptionPane.showMessageDialog(null,“猜测的温度等于随机温度”。\n\n1.最低温度为:“+LowerstTemperature+”\n2.平均温度为:“+AvgtTemperature+”\n3.正确猜测的温度的数组索引为:“+IndextTemperature+”\n4.法伦赫温度为:“+convertedTemperature+”“\n\n感谢您的参与!”;
} 
否则如果(猜测温度==-1){
系统出口(0);
}
}
}
}
}      

}

您可能希望将
arList=new ArrayList();
作为处理程序类的实例变量移动。现在,由于ArrayList在actionPerformed方法内初始化,因此每次用户单击按钮时,它都会重新初始化


(只是粗略地看了一下程序。我可能错了)

只是简单地看了一下,但我认为问题是你创建了每个