コードはすべてのものを動作していない理由はわからない細かいD思わ:JavaのGUI推測ゲーム
だから、このコードは、あなたがそれを見ることができるように推測ゲームでスイングを実装し、何らかの理由でそれだけでコンパイルされません日食の
public class GuessingGame {
public static void main(String[] args)
{
int randomNumber;
randomNumber = (int) (10 * Math.random()) + 1;
Scanner keyboard = new Scanner(System.in);
int guess=0;
int attempts = 0;
GuessingGameGUI game = new GuessingGameGUI();
game.setVisible(true);
while (guess != randomNumber)
{
try
{
System.out.print("Guess the number from 1-10: ");
attempts++;
guess = keyboard.nextInt();
if (guess < 1 || guess > 10)
throw new BadGuessException();
}
catch (BadGuessException e)
{
System.out.println(e.getMessage());
}
catch (InputMismatchException e)
{
keyboard.next();
System.out.println("Sorry, you entered an invalid number format. ");
}
}
System.out.println("\nYOU GOT IT! (It took " + attempts + " attempt(s))");
}
}
について
public class GuessingGameGUI extends JFrame
{
public static final int WIDTH = 500;
public static final int HEIGHT = 250;
public GuessingGameGUI()
{
super();
setTitle("Guessing Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel north= new JPanel();
add(north, BorderLayout.NORTH);
JLabel lab1 = new JLabel("Guess a number between 1 and 10?");
JPanel center = new JPanel();
add(center, BorderLayout.CENTER);
JTextField userInput = new JTextField();
JButton Guess = new JButton("Guess");
Guess.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel south = new JPanel();
add(south, BorderLayout.SOUTH);
JLabel lab2 = new JLabel();
south.add(lab2);
int guess = 0;
int randomNumber=(int) (10 * Math.random()) + 1;
int attempts = 0;
attempts++;
if (e.getActionCommand().equals("Guess"))
{
if (guess < 1 || guess > 10)
{
lab2.setText("Invalid number");
getContentPane().setBackground(Color.red);
}
else if (guess == randomNumber)
{
lab2.setText("\nYOU GOT IT! (It took " + attempts + "
attempt(s))");
}
else
{
getContentPane().setBackground(Color.GRAY);
lab2.setText("Sorry, try again");
}
}
}
});
//NORTH PANEL
north.add(lab1);
//CENTER PANEL
userInput.setPreferredSize(new Dimension(200, 25));
setLayout(new FlowLayout());
center.add(userInput);
center.add(Guess);
Container pane = this.getContentPane();
}
}
「と、何らかの理由でそれがコンパイルされません」' - 「いくつかの」理由でそれはです**ません**実際、コンパイラは理由と場所を正確に伝えています。この重要な情報をあなたの質問と共有してください。それはちょっとした鍵です。 –
また、書式の整ったコードだけを投稿してください。あなたがそれを許可すれば、Eclipseはあなたのためにこれを行います。 –
それは「解析中にファイルの終わりに達しました」と表示されています。 – jaegers1234