学习互联网的人。我目前正在为一年级的编程课做作业,老实说,编程并不是我最大的技能。因此,对于这个小问题的任何帮助将不胜感激。
该程序本质上是一个数字猜谜游戏,它将猜测存储和排序到一个数组列表中,一旦你猜对了,就会显示平均值、最低猜测、正确猜测的索引数组、平均值和转换后的华氏温度. 它在 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);
}
}
}
}
}
}