我目前正在为一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,对于这个小问题的任何帮助将不胜感激。我很抱歉因为我的新手而堵塞了网站。
该程序本质上是一个使用温度的猜数游戏,它将JTextField中的猜测(intguessTemperature)存储并排序到一个数组列表中,并通过JOptionMessage显示,一旦你猜对了,最低猜测,正确猜测的索引数组,所有猜测的平均值和以华氏度为单位的转换温度。它在 9 到 40 之间,因为作业指定了 40 和我学生证上的最高数字。我没有包含 avg 或 index 的方法,因为不起作用的 arraylist 使它变得多余。
我遇到的麻烦是将温度猜测(无论正确还是不正确)存储在数组列表中。我手动测试程序,从 9 开始,一直工作到 40。因此,如果我的数组列表运行正常,那么在对猜测的最低温度进行排序后的输出应该始终为 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);
}
}
}
}
}
}