Near-Reality RSPS - Runescape Private Server
Go Back   Near-Reality RSPS - Runescape Private Server > Media > Off-Topic Discussion > Programming
Reload this Page java
Sign in to follow this  
dcomputer

java

8 posts in this topic

Unread
  (#0)
Default java - Sunday 17th March, 2019, 23:25

I just started learning java and I have this dice game. it works but u bet on a number. after that it ask how much r u going to bet. when u put a number in there it tells u that u don't have enuf money while I know u do. (u start with 1000) after u get that msg u just type ur amount u want to bet again and it will work.
y does it tell me I don't have enuf money when I do? here part of da code if u need da hole code let me know.
also any one willing to teach me some stuff I would love. I do concert const. im rdy to start getting back in to coding (been out of it for going on 7 yrs now)

 

public static void rollDice()
 {
  System.out.println("you have: $" + money);
  System.out.println("what number would u like to bet on? (1-6)?");
  int betRoll = in.nextInt();
  System.out.println("how much would u like to bet?");
  int betMoney = in.nextInt();
  while (betMoney > money);
  {
   System.out.println("you dont have that much money! you have: $" + money +
     ". please enter your bet...");
   betMoney=in.nextInt();
  }
  int dice;
  dice = random.nextInt(6)+1;
  if (betRoll==dice)

Edited by dcomputer

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Sunday 17th March, 2019, 23:46

I'll get back to you soon 

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Sunday 17th March, 2019, 23:55

You could maybe try passing the money int as a parameter in the method, like

public static void rollDice(int money)

and when you use the method you put how much you have in there, rollDice(1000)

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Monday 18th March, 2019, 00:12
17 minutes ago, Divinate said:

You could maybe try passing the money int as a parameter in the method, like

public static void rollDice(int money)

and when you use the method you put how much you have in there, rollDice(1000)

I wrote it in there but it give me same msg and it wont - from 1000 if I lose roll now. I put code back to what it was. here is da hole thing. mayb I just miss put something in there

package test2;
import java.util.*;
public class Testcode
{
 static int money;
 static String userName;
 static Scanner in = new Scanner(System.in);
 static Random random = new Random();
 public static void main(String[] args)
 {
  System.out.println("please enter yout username...");
  userName=in.nextLine();
  System.out.println("welcome" + userName + "!");
  money=10000;
  rollDice();
 }
  public static void rollDice()
 {
  System.out.println("you have: $" + money);
  System.out.println("what number would u like to bet on? (1-6)?");
  int betRoll = in.nextInt();
  System.out.println("how much would u like to bet?");
  int betMoney = in.nextInt();
  while (betMoney > money);
  {
   System.out.println("you dont have that much money! you have: $" + money +
     ". please enter your bet...");
   betMoney=in.nextInt();
  }
  int dice;
  dice = random.nextInt(6)+1;
  if (betRoll==dice)
  {
   System.out.println("you have won! you win" + betMoney * 6);
   money+=betRoll*6;
  }
  else
  {
   System.out.println("you lost!");
   money-=betMoney;
  }
  if (money <= 0)
  {
   System.out.println("you are broke!");
  }
  else
  {
   rollDice();
  }
 }
}
Edited by dcomputer

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Monday 18th March, 2019, 00:33

Maybe it has to do something with the variables at the top being declared static? I'm not too useful at debugging and things like this, but the rest of the code looks like it should work to me.

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Monday 18th March, 2019, 07:06
while (betMoney > money);

 

When it reaches this, you're not actually doing anything, as the semicolon terminates the statement. You're then creating a scoped block with your braces rather than defining it as the loop body, and so the block always gets executed. Swap the loop to look like this (note how I removed the semicolon at the loop declaration):

 

	while (betMoney > money)
	{
	    System.out.println("you dont have that much money! you have: $" + money + ". please enter your bet...");
	    betMoney = in.nextInt();
	}
	

 

Edited by Cups

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Tuesday 19th March, 2019, 00:53
On ‎3‎/‎17‎/‎2019 at 7:33 PM, Divinate said:

Maybe it has to do something with the variables at the top being declared static? I'm not too useful at debugging and things like this, but the rest of the code looks like it should work to me.

I had to put that in there or it wouldn't work with out it. I didn't have it in there at 1st :P . but thank u for trying :) 

 

17 hours ago, Cups said:

while (betMoney > money);

 

When it reaches this, you're not actually doing anything, as the semicolon terminates the statement. You're then creating a scoped block with your braces rather than defining it as the loop body, and so the block always gets executed. Swap the loop to look like this (note how I removed the semicolon at the loop declaration):

it worked! thank u very much and that makes sense . much love :)

Share this post


Link to post
Share on other sites
  • Report
  • Quote
Unread
  (#0)
Default Tuesday 19th March, 2019, 01:12
18 hours ago, Cups said:

while (betMoney > money);

 

When it reaches this, you're not actually doing anything, as the semicolon terminates the statement. You're then creating a scoped block with your braces rather than defining it as the loop body, and so the block always gets executed. Swap the loop to look like this (note how I removed the semicolon at the loop declaration):

 

 


	while (betMoney > money)
	{
	    System.out.println("you dont have that much money! you have: $" + money + ". please enter your bet...");
	    betMoney = in.nextInt();
	}
	

 

 

Wow I really missed that lmao told you I was no good at debugging

Share this post


Link to post
Share on other sites
  • Report
  • Quote

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

Contains New Posts   Forum Contains New Posts
Contains No New Posts   Forum Contains No New Posts