Results 1 to 12 of 12
Thread: Help with code.
- 09-16-2012, 11:55 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Help with code.
I'm creating a simple and basic slot machine for a presentation. I'm trying to get it to work in Eclipse. I'm running to an issue with "String". Is there something else I can use instead to fix the issue.
[highlight=Java]
import java.util.Random;
import java.util.Scanner;
public class SM {
public static void main (String[]args)
{
//Instance Data//
int slot1;
int slot2;
int slot3;
String start = "1";
String end = "0";
int play;
//New Scanner and Random Generator//
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Enter 1 to start or 0 to stop playing.");
play = scan.nextInt();
while (play == 1)
{
//Number Generators//
slot1 = generator.nextInt(10);
slot2 = generator.nextInt(10);
slot3 = generator.nextInt(10);
//Determines if the numbers match and prints the appropriate response//
if (slot1 == slot2 && slot2 == slot3)
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Congratulations, You have won!");
}
if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
}
else
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " None of the numbers match");
}
//Asks if player would like to play again//
System.out.println("If you want to play again, Enter 1 for yes or 0 for no.");
{
play = scan.nextInt();
}
}
if (play == 0)
{
System.out.println("Thank you for playing");
}
}
}
[/highlight]
- 09-17-2012, 12:54 AM #2
Re: Help with code.
Can you explain the "issue"? Please post the full text of any error messages.an issue with "String". Is there something else I can use instead to fix the issue.
Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming ForumIf you don't understand my response, don't ignore it, ask a question.
- 09-17-2012, 06:30 AM #3
Re: Help with code.
And go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-17-2012, 08:10 AM #4
Member
- Join Date
- Sep 2012
- Location
- Navi Mumbai
- Posts
- 30
- Rep Power
- 0
Re: Help with code.
Not understood your issue'
'
But this bug was noticed....'
'
In ur if else block...............'
'if (slot1 == slot2 && slot2 == slot3)
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Congratulations, You have won!");
}
if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
}
else
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " None of the numbers match");
}
Even if you win, all same numbers,
Both the statements are executed.
'
for 2nd 'if' block, write
else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
}
- 09-17-2012, 08:18 AM #5
Member
- Join Date
- Sep 2012
- Location
- Navi Mumbai
- Posts
- 30
- Rep Power
- 0
Re: Help with code.
--------------------------------------------------------------------------------
Your issue with String...'
--------------------------------------------------------------------------------
In your program, you have just declared the variables 'start' and 'end'
But not used anywhere'
Just try commenting them.....'int slot1;
int slot2;
int slot3;
int play;String start = "1";
String end = "0";
The program runs as desired correctly......................'int slot1;
int slot2;
int slot3;
//String start = "1";
//String end = "0";
int play;
'
- 09-30-2012, 01:07 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Help with code.
I apologize for the late response I figured out the problem and solved any issue I had.
- 09-30-2012, 01:35 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Help with code.
Can a Moderator please delete this post.
-
Re: Help with code.
Why would you want it deleted? Probably best to leave it open and for you to post your solution in case future programming students run into a similar problem and find this site when searching for a solution.
Did you look at the answers and did they help you solve your problem? Consider thanking the folks who put in the effort to help you since they're all volunteers and helped on their free time. I'm sorry about being a nudge, but this is how we're paid, and it really isn't asking a lot, I don't think.I apologize for the late response I figured out the problem and solved any issue I had.Last edited by Fubarable; 09-30-2012 at 02:05 AM.
- 09-30-2012, 02:29 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Help with code.
Yes, I looked at the answers to resolve my problem. I would like to have it closed that's all. I do not even know how to thank them and have no problem thanking them at all.
-
- 09-30-2012, 02:36 AM #11
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Re: Help with code.
I will like to thank suyog53 and mention that it's solved.
- 09-30-2012, 02:38 AM #12
Member
- Join Date
- Sep 2012
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks