d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Question
12Next
Add Reply New Topic New Poll
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 06:54pm
double userBet = input.nextDouble();
if ( userBet > userCash) {
System.out.println("You dont have enough money. Place another bet.");


My question, is how do I loop the next user input to this same if statement, so that if they bet above their limit, it will repeat the same println.

/e After thinking about it a little, is it possible to make this a while loop..
-- While userBet > userCash, print that line.. And once they bet below it registers and moves forward?

This post was edited by Malexir on Feb 20 2013 06:55pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 06:59pm
double userBet = input.nextDouble();
while ( userBet > userCash) {
System.out.println("You dont have enough money. Place another bet.");
userBet = input.nextDouble();
}
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 07:01pm
Thanks so much. Did not know once I named the double I could just reuse.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 07:04pm
Quote (Malexir @ Feb 20 2013 08:01pm)
Thanks so much. Did not know once I named the double I could just reuse.


by default, variables can change. if you prefix it with "final" then it cannot change. eg:
final double userBet = input.nextDouble();
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 07:12pm
Thanks for that info as well. :)
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 07:23pm
ANOTHER QUESTION!

for (userCash = 10.00; userCash > 0.00;) {

I need to ALSO add the possibility for the user to type "done" and the game will end.
I can't figure out where to place it within the for loop. I have a string that I use for many of the users choices, but I can't figure out how to attached something like. uc.equals"done" --- the string is 'uc' without the '
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 08:35pm
not sure what you're asking. is this what you want?

if ("done".equalsIgnoreCase(uc))
break;
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 08:38pm
I have a for loop.
The loop is for(userCash = 10.00; userCash > 0.00;) {
That is saying that the game will run as long as the user has more than 0 dollars.. I need to also add that the "for loop" will end if the user tpyes "done"
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 08:56pm
i know that part. i'm not sure what part you need help with. i assume you're asking the user for input again inside the for loop. you can either add what i suggested or modify the condition of your for loop

for (userCash = 10.00; userCash > 0.00 && !"done".equalsIgnoreCase(uc);) {
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 09:11pm
Tried it, and it didn't work.

double userCash;
System.out.println("Please type your first name to being");
String user = input.next();
System.out.println("Hello " + user + " and welcome to the game!");

for (userCash = 10.00; userCash > 0.00 && "done".equalsIgnoreCase(uc);) {


String uc = input.next().toLowerCase();
switch(uc){

The string is inside the for loop, but the equalsIgnoreCase gives an error. I changed it to a constant and the error doesn't exist, but tpying done does not end the loop
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll