Little help with this project please
Hi everyone and thank you in advance for helping me with this issue. What im trying to do is, in my ending message, im trying to make it say thanks for playing... "you have won + wins" but i cant figure out how to add the +wins in there (ending message is just a copy of opening message that im changing). im rather new to java. also any suggestions on changing the code a bit to make it better is greatly appreciated.
Code java:
import javax.swing.*;
import java.awt.Font; // need for Font and Point Size Changes
import java.awt.Color; // need to change Color
public class Lab2
{
static int wins = 0;
static int losses = 0;
static int totalPlays = 0;
public static void main(String[] args)
{
runThis();
System.exit(0);
}//main
public static void runThis()
{
int userNum;
int compNum;
int value;
openingMessage();
getInt();
do
{
userNum = getInt();
compNum = rollTheDice();
compareNum (userNum, compNum);
value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
}
while (value == JOptionPane.YES_OPTION); // do while
endingMessage();
}
public static void openingMessage()
{
String output = "";
Font big = new Font ( "Serif", Font.BOLD, 22 ); // set up how the letters will look
JTextArea outputArea = new JTextArea (); // create a new JTextArea named outputArea
outputArea.setFont(big); // use the font stuff from above
outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) ); // Navy 000080
outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) ); // Corn Silk FFF8DC
output = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n"
+ "The computer will generate a random number from 1 to 6. \n"
+ " Your task is to see if you can guess the number. \n\n"
+ "The game will keep track of how many games you played \n"
+ "and it will tell you how many wins you had \n \n"
+ "\t Click OK to Begin.\n\n\t Good Luck \n";
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
return;
}// opening message
public static void compareNum (int user, int comp)
{
if(user == comp)
{
JOptionPane.showMessageDialog (null, "You Win");
wins++;
totalPlays++;
System.out.println ("Wins:" + wins);
}
else
{
JOptionPane.showMessageDialog (null, "You Lost");
losses++;
totalPlays++;
System.out.println ("Losses:" + losses);
}
}
public static int getInt()
{
int val;
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s;
s = JOptionPane.showInputDialog ( "Enter a number from 1 - 6:" );
if (s == null)
{
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit (0);
} // cancel if
else
if (s.equals(""))
{
JOptionPane.showMessageDialog
(null, "You must make an entry in the InputBox");
} // entered nothing
try
{
val = Integer.parseInt(s);
if (val >= 1 && val <= 6)
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a number from 1 - 6");
}// catch
}//while
return val; //return the number to the calling method
} //end getInt
public static void endingMessage()
{
String output = "";
Font big = new Font ( "Serif", Font.BOLD, 22 ); // set up how the letters will look
JTextArea outputArea = new JTextArea (); // create a new JTextArea named outputArea
outputArea.setFont(big); // use the font stuff from above
outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) ); // Navy 000080
outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) ); // Corn Silk FFF8DC
output = "Thanks for playing the Guess the Numbers from 1 to 6 Program \n\n"
+ "You have won. \n"
+ " Your task is to see if you can guess the number. \n\n"
+ "The game will keep track of how many games you played \n"
+ "and it will tell you how many wins you had \n \n"
+ "\t Click OK to Begin.\n\n\t Good Luck \n";
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
return;
}// opening message
public static void dice()
{
int Total;
String output = "";
//collect from user guess of
//die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
Total = guessTheDice();
int rolledTotal;
//roll the dice in method
rolledTotal = rollTheDice();
if(Total == rolledTotal)
output = "You guessed right!" +
" The dice total is " + Total;
else
output = "Sorry. The dice total is: "
+rolledTotal;
JOptionPane.showMessageDialog(
null, output);
System.exit(0);
}//end main
public static int guessTheDice ()
{
int dice1; //first value of die1
int total;
String firstDice;
firstDice=JOptionPane.showInputDialog
("Guess die 1 (1-6): ");
dice1=Integer.parseInt(firstDice);
total=dice1;
return total;
}//end guessTheDice method
public static int rollTheDice()
{
int x;
//generating random numbers
//between 1-6 with
//Math.random
x=1 + (int) (Math.random( ) *6);
return x;
}//end rollTheDice method
}// try val = integer parseInt
The program is a game that randomly generates a number from 1 - 6 and tracks your wins, losses, and times played. Another problem with it is that the firs time you guess a number, the program kinda disregards the first guess and starts tracking from the second guess onwards. Thanks again
Re: Little help with this project please
Look at how you print out Total (which should be total according to standard naming conventions) and just do something similar with your wins.
Re: Little help with this project please
Sorry i dont understand.. im trying to get it to look like this..
Code java:
public static void endingMessage()
{
String output = "";
Font big = new Font ( "Serif", Font.BOLD, 22 ); // set up how the letters will look
JTextArea outputArea = new JTextArea (); // create a new JTextArea named outputArea
outputArea.setFont(big); // use the font stuff from above
outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) ); // Navy 000080
outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) ); // Corn Silk FFF8DC
output = "Thanks for playing the Guess the Numbers from 1 to 6 Program \n\n"
+ "You have won +wins \n"
+ "You have lost +losses \n\n"
+ "You have played +totalPlays \n";
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"Thanks for playing", JOptionPane.INFORMATION_MESSAGE);
return;
}// ending message
But that doesn't work.. so it would say you have won 5 times etc. i just cant figure it out =(
Re: Little help with this project please
That's not how you print the total, is it? Pay close attention to where the quotation marks are.