Results 1 to 3 of 3
  1. #1
    V3ndetta is offline Member
    Join Date
    Apr 2016
    Posts
    2
    Rep Power
    0

    Unhappy 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();
             }
             }
        }
    winval represents the value substracted from the other in case of a win.
    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.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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) {
    1. To test equality for non-objects, use == not =.
    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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    V3ndetta is offline Member
    Join Date
    Apr 2016
    Posts
    2
    Rep Power
    0

    Smile Re: Dice game money count problem

    Quote Originally Posted by jim829 View Post
    First, the following snippet is incorrect.
    Java Code:
    boolean repeatgame = false;
    System.out.println("Dice game V0.1 by V3ndetta");
    while (repeatgame = true) {
    1. To test equality for non-objects, use == not =.
    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,
    Jim
    Thanks,the main problem I could not solve was that I had my variables inside the while loop,which was not even working well.

Similar Threads

  1. Pig dice game
    By dangyounoobs in forum New To Java
    Replies: 2
    Last Post: 12-09-2013, 05:21 AM
  2. Dice Game
    By DD13 in forum New To Java
    Replies: 3
    Last Post: 03-29-2013, 05:12 AM
  3. Help With Dice Game Please
    By Visions in forum New To Java
    Replies: 2
    Last Post: 10-07-2012, 09:16 PM
  4. Help with a Dice Game.
    By HalfAZN in forum New To Java
    Replies: 30
    Last Post: 04-30-2012, 07:36 PM
  5. Dice Game
    By Rachel1991 in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 12:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •