| | |
Guess Number Game (Problem)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
HI, i'm new in JAVA
i creating a "GUESS NUMBER GAME"..
which allow user 3 changes to guess..
i'm new to java so i need a simple coding...
i has done the part but i dun wan to combine all function in main...
this is my problem, i hope u can solve to me...
this is my coding... pls look forward it..
import javax.swing.JOptionPane;
public class game1
{
public static void main (String args[])
{
int rollOfDice, num, x;
String output,userNum;
//generate a random number betwwen 1 and 6 inclusive
rollOfDice = (int)(Math.random()*6)+1;
output = "You rolled a "+rollOfDice;
//print msg
JOptionPane.showMessageDialog(null,output,"Random Number Demo",JOptionPane.INFORMATION_MESSAGE);
do
{
userNum = JOptionPane.showInputDialog ("Please Guess the number");
num = Integer.parseInt(userNum);
if (num == rollOfDice)
{
output = "Congratulation";
JOptionPane.showMessageDialog(null,output,"Win",JOptionPane.INFORMATION_MESSAGE);
}
else
{
for (x=1;x>3;x++)
{
output = "Try again";
JOptionPane.showMessageDialog(null,output,"Lose",JOptionPane.INFORMATION_MESSAGE);
//output = "You are lose";
//JOptionPane.showMessageDialog(null,output,"Lose",JOptionPane.INFORMATION_MESSAGE);
}
}
}while(num != rollOfDice);
System.exit(0);
}
}
i creating a "GUESS NUMBER GAME"..
which allow user 3 changes to guess..
i'm new to java so i need a simple coding...
i has done the part but i dun wan to combine all function in main...
this is my problem, i hope u can solve to me...
this is my coding... pls look forward it..
import javax.swing.JOptionPane;
public class game1
{
public static void main (String args[])
{
int rollOfDice, num, x;
String output,userNum;
//generate a random number betwwen 1 and 6 inclusive
rollOfDice = (int)(Math.random()*6)+1;
output = "You rolled a "+rollOfDice;
//print msg
JOptionPane.showMessageDialog(null,output,"Random Number Demo",JOptionPane.INFORMATION_MESSAGE);
do
{
userNum = JOptionPane.showInputDialog ("Please Guess the number");
num = Integer.parseInt(userNum);
if (num == rollOfDice)
{
output = "Congratulation";
JOptionPane.showMessageDialog(null,output,"Win",JOptionPane.INFORMATION_MESSAGE);
}
else
{
for (x=1;x>3;x++)
{
output = "Try again";
JOptionPane.showMessageDialog(null,output,"Lose",JOptionPane.INFORMATION_MESSAGE);
//output = "You are lose";
//JOptionPane.showMessageDialog(null,output,"Lose",JOptionPane.INFORMATION_MESSAGE);
}
}
}while(num != rollOfDice);
System.exit(0);
}
}
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Number game in C++ (C++)
- Invisible entry widget. (Python)
- prime number generator does nothing. (C++)
- Something similar to def in Tkinter (Python)
- Including other python programs (Python)
- Number input problem. (C++)
- The Number Game (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: Jave newbie
- Next Thread: Dasher in java
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






