Results 1 to 3 of 3
Thread: Dice game money count problem
- 04-09-2016, 10:10 PM #1
Member
- Join Date
- Apr 2016
- Posts
- 2
- Rep Power
- 0
Dice game money count problem
Hello,I just started out with java a day ago.I am mostly familiar of how to code in other languages,and I have made a dice based game.You and the opponent both throw 2 dices,and the one that wins takes from the other 10$.The problem is that when I tried to do something like a=a-b,I got some errors,so I used another variable,c=a-b,a=a.Now I have no errors,but the money is not substracted or added at all.This is the code I have:
Java Code:package javaapplication1; import java.util.Scanner; public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { boolean repeatgame=false; System.out.println("Dice game V0.1 by V3ndetta"); while(repeatgame=true){ // TODO code application logic here int mydice1=(int)(Math.random()*6+1); int mydice2=(int)(Math.random()*6+1); int mysum=mydice1+mydice2; int enemydice1=(int) (Math.random()*6+1); int enemydice2=(int) (Math.random()*6+1); int enemysum=enemydice1+enemydice2; int mymoney=50; int enemymoney=75; int loseval=10; int winval=10; int myph=0; int enph=0; Scanner repeat=new Scanner(System.in); System.out.println("You rolled "+mydice1+" and "+mydice2+" total "+mysum+" you have "+ mymoney+"$ "+"left"); System.out.println("Opponent rolled "+enemydice1+" and "+enemydice2+" total "+enemysum+" opponent has "+enemymoney+"$ "+ " left"); if(mysum>enemysum){ System.out.println("You won"); myph=mymoney+winval; mymoney=myph; System.out.println("Press enter to continue"); }else if(mysum<enemysum){ System.out.println("You lost"); enph=enemymoney+winval; enemymoney=enph; System.out.println("Press enter to continue"); }else if(mysum==enemysum){ System.out.println("Draw"); System.out.println("Press enter to continue"); } String ron=repeat.nextLine(); } } }
emph is the placeholder for money of the opponent,which will equal his money.
myph is the placeholder for my money.
I dont know if I am doing something wrong,or is my code placed badly.
- 04-09-2016, 10:28 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Dice game money count problem
First, the following snippet is incorrect.
Java Code:boolean repeatgame = false; System.out.println("Dice game V0.1 by V3ndetta"); while (repeatgame = true) {
2. For testing booleans, just use the boolean itself (it is already true or false which is all you need).
3. If you set it to false initially, then the loop will not be entered.
4. You never reset repeatgame base on some user input.
Now, the big problem I see is that you re-initialize your variables inside the loop. So even if they are updated,
they will be re-initialized.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-09-2016, 11:10 PM #3
Member
- Join Date
- Apr 2016
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Pig dice game
By dangyounoobs in forum New To JavaReplies: 2Last Post: 12-09-2013, 05:21 AM -
Dice Game
By DD13 in forum New To JavaReplies: 3Last Post: 03-29-2013, 05:12 AM -
Help With Dice Game Please
By Visions in forum New To JavaReplies: 2Last Post: 10-07-2012, 09:16 PM -
Help with a Dice Game.
By HalfAZN in forum New To JavaReplies: 30Last Post: 04-30-2012, 07:36 PM -
Dice Game
By Rachel1991 in forum New To JavaReplies: 1Last Post: 11-25-2011, 12:10 AM
Bookmarks