aaronkupen
Fully Optimized
- Messages
- 1,848
- Location
- Pittsburgh, PA
Code:
/*
Date: 11/17/05
Filename: GuessingGame.java
Purpose: a game to guess a number between 1-15
*/
import javax.swing.*;
public class GuessingGame
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"Welcome to the guessing game, guess a number between 1 and 15","Intro",JOptionPane.PLAIN_MESSAGE);
int nNumber = (int)(Math.random() * 15 +1);
int nGuess, nCounter = 0;
int[] nPrevious;
nPrevious = new int[15];
String strNumbers = "\n";
do
{
String strGuess = JOptionPane.showInputDialog(null,"Enter a number" + strNumbers,"Number",JOptionPane.PLAIN_MESSAGE);
nGuess = Integer.parseInt(strGuess);
if (nGuess < 1)
{
JOptionPane.showMessageDialog(null,"Number out of range","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
for (int i = 0; i++ <= nCounter;)
{
if (nGuess == nPrevious[i]
{
JOptionPane.showMessageDialog(null,"You entered the same number","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
nPrevious[nCounter] = nGuess;
strNumbers += strGuess + "\n";
++nCounter;
}
}
}
}
while (nGuess != nNumber);
JOptionPane.showMessageDialog(null,"You are correct. It took you " + nCounter + "time(s) to get it","Answer",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}