Help with Java
2 posters
Page 1 of 1
Help with Java
So at Wisconsin I'm taking Intro to Programming and we have to make a program called "SheepMaster" which is pretty much a text based game where you have a flock of sheep and some guard dogs and you need to buy lots of sheep and guard dogs so you can eventually make 1500 doubloons (game currency) without your flock getting wiped out by wolves.
Part of the program I'm struggling with, is when the user selects buy sheep from the main menu it takes the user to a buy sheep menu which asks "How Many Sheep would you like to purchase?" which then allows the user to input an integer in a scanner and it then determines whether the user has enough doubloons to purchase the sheep and if they don't, allows the user to retry the input. My problem is that this action also needs to loop back to the beginning of the sheep menu when an exception occurs, such as the user purchasing negative sheep or typing a string instead of an integer.
Here is the Program from when the user selects "purchase sheep from the menu
else if (pick.equals("3"))
{
//Determines number of sheep User would like to purchase
System.out.println("How Many Sheep Would You Like to Purchase?");
try
{
int sheepPlus = scan.nextInt();
int cost = sheepPlus * SHEEPCOST;
if (cost>doubloons)
{
//cancels if user does not have enough money
System.out.println("Insufficient Funds!");
}
if (sheepPlus < 0 )
{
System.out.println("You Can't Buy Negative Sheep!");
}
else
{
//increases number of sheep based on user selection and decreases doubloons
doubloons = doubloons - cost;
sheep = sheep + sheepPlus;
sheepSurvival = sheepSurvival + sheepPlus;
sheepTot = sheepTot + sheepPlus;
System.out.print("You've Purchased ");
System.out.print(sheepPlus);
System.out.println(" Sheep!");
System.out.print("You Now have ");
System.out.print(sheep);
System.out.println("!");
System.out.print("You Have ");
System.out.print(doubloons);
System.out.println(" Doubloons Remaining!");
}
}
catch (Exception e)
{
System.out.println("Please Select a Positive Integer!");
}
note: after the action is executed it returns the user back to the main menu currently. you should also be able to find more specific details of the whole program if you google CS302 and click the first link, go to "programs", and go to "SheepMaster"
Please Help... no one in my dorm knows how to program and I'm still kind of a noob at programming... I have everything else done except for exception-proofing it.
Part of the program I'm struggling with, is when the user selects buy sheep from the main menu it takes the user to a buy sheep menu which asks "How Many Sheep would you like to purchase?" which then allows the user to input an integer in a scanner and it then determines whether the user has enough doubloons to purchase the sheep and if they don't, allows the user to retry the input. My problem is that this action also needs to loop back to the beginning of the sheep menu when an exception occurs, such as the user purchasing negative sheep or typing a string instead of an integer.
Here is the Program from when the user selects "purchase sheep from the menu
else if (pick.equals("3"))
{
//Determines number of sheep User would like to purchase
System.out.println("How Many Sheep Would You Like to Purchase?");
try
{
int sheepPlus = scan.nextInt();
int cost = sheepPlus * SHEEPCOST;
if (cost>doubloons)
{
//cancels if user does not have enough money
System.out.println("Insufficient Funds!");
}
if (sheepPlus < 0 )
{
System.out.println("You Can't Buy Negative Sheep!");
}
else
{
//increases number of sheep based on user selection and decreases doubloons
doubloons = doubloons - cost;
sheep = sheep + sheepPlus;
sheepSurvival = sheepSurvival + sheepPlus;
sheepTot = sheepTot + sheepPlus;
System.out.print("You've Purchased ");
System.out.print(sheepPlus);
System.out.println(" Sheep!");
System.out.print("You Now have ");
System.out.print(sheep);
System.out.println("!");
System.out.print("You Have ");
System.out.print(doubloons);
System.out.println(" Doubloons Remaining!");
}
}
catch (Exception e)
{
System.out.println("Please Select a Positive Integer!");
}
note: after the action is executed it returns the user back to the main menu currently. you should also be able to find more specific details of the whole program if you google CS302 and click the first link, go to "programs", and go to "SheepMaster"
Please Help... no one in my dorm knows how to program and I'm still kind of a noob at programming... I have everything else done except for exception-proofing it.
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Re: Help with Java
While loops complete the action if the boolean is not satisfied as opposed to do-while loops that complete the action until the booleanis satisfied. I don't know what one-way flags are though.
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Re: Help with Java
Your idea of while loops isn't really correct. A while loop evaluates if the condition is true and repeats itself until it tries to start again while the condition is false, in which case the while loop ends. A do while is a chunk of code that gets evaluated at least once then checks to see if it should repeat itself at the end according to the condition, otherwise the loop ends. Dont worry about the term "one way flags", its just one way of strategically using a booleans that only change one way and not back.
I would stick the whole part of code that needs to be repeated inside a do-while loop. When an exception occurs, make a boolean that changes to true so something like this:
I would stick the whole part of code that needs to be repeated inside a do-while loop. When an exception occurs, make a boolean that changes to true so something like this:
- Code:
boolean error;
do{
error = false;
...your code...
catch(Exception e)
error = true;
} while(error);
Re: Help with Java
I may have done this wrong but when I typed it out it created an infinitely repeating loop of "Please Select a Positive Integer!" and "How Many Sheep Would you like to purchase?"
- Code:
else if (pick.equals("3"))
{
boolean error;
do{
error = false;
//Determines number of sheep User would like to purchase
System.out.println("How Many Sheep Would You Like to Purchase?");
try
{
int sheepPlus = scan.nextInt();
int cost = sheepPlus * SHEEPCOST;
if (cost>doubloons)
{
//cancels if user does not have enough money
System.out.println("Insufficient Funds!");
}
if (sheepPlus < 0 )
{
System.out.println("You Can't Buy Negative Sheep!");
}
else
{
//increases number of sheep based on user selection and decreases doubloons
doubloons = doubloons - cost;
sheep = sheep + sheepPlus;
sheepSurvival = sheepSurvival + sheepPlus;
sheepTot = sheepTot + sheepPlus;
System.out.print("You've Purchased ");
System.out.print(sheepPlus);
System.out.println(" Sheep!");
System.out.print("You Now have ");
System.out.print(sheep);
System.out.println("!");
System.out.print("You Have ");
System.out.print(doubloons);
System.out.println(" Doubloons Remaining!");
}
}
catch (Exception e)
{
error = true;
System.out.println("Please Select a Positive Integer!");
}
} while (error);
}
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Re: Help with Java
Note please format your code with [code_][_/code] without the "_" so it is easier to read in the future.
Now are you getting the infinite loop before or after entering a value? Or does it even let you enter a value?
Also i would put the sheep<0 check before the cost>dubloons check, otherwise you will always have sufficient funds if you buy negative sheep, and yes that should also set error = true
Now are you getting the infinite loop before or after entering a value? Or does it even let you enter a value?
Also i would put the sheep<0 check before the cost>dubloons check, otherwise you will always have sufficient funds if you buy negative sheep, and yes that should also set error = true
Re: Help with Java
So Here's what happens.
I click 'run program'
What is Your Name? bob 'enter'
Welcome to bob's Meadow!
The World is Your Oyster!
Current Status
Doubloons: 500
Number of Sheep: 1
Number of Guard Dogs: 0
Please Select an Action from the Menu Below!
(1) Print Market Prices
(2) Print Detailed Statistics
(3) Buy a Sheep
(4) Buy a Guard Dog
(5) Sell a Sheep
(6) Sell a Guard Dog
(7) Begin Night Phase
( Quit Game
3 'enter'
How Many Sheep Would You Like to Purchase?
goop 'enter'
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
.............(continues indefinitely until I terminate)
I click 'run program'
What is Your Name? bob 'enter'
Welcome to bob's Meadow!
The World is Your Oyster!
Current Status
Doubloons: 500
Number of Sheep: 1
Number of Guard Dogs: 0
Please Select an Action from the Menu Below!
(1) Print Market Prices
(2) Print Detailed Statistics
(3) Buy a Sheep
(4) Buy a Guard Dog
(5) Sell a Sheep
(6) Sell a Guard Dog
(7) Begin Night Phase
( Quit Game
3 'enter'
How Many Sheep Would You Like to Purchase?
goop 'enter'
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
How Many Sheep Would You Like to Purchase?
Please Select a Positive Integer!
.............(continues indefinitely until I terminate)
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Re: Help with Java
*except instead of its an 8 parentheses
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Re: Help with Java
Pretty subtle error, but I figured it out!
For this line:
int sheepPlus = scan.nextInt();
If it errors out, then the scanner does not advance to the next line, so it continually tries to read the same thing over and over. Instead use this:
int sheepPlus = Integer.parseInt(scan.next());
What this does is successfully scans a string in, scan.next(), and moves the scanner to the next line, then checks to see if the string you scanned can be turned into an int, Integer.parseInt(String). When this errors out, the scanner will try to read the next line rather than the same line since it already successfully read the previous one.
For this line:
int sheepPlus = scan.nextInt();
If it errors out, then the scanner does not advance to the next line, so it continually tries to read the same thing over and over. Instead use this:
int sheepPlus = Integer.parseInt(scan.next());
What this does is successfully scans a string in, scan.next(), and moves the scanner to the next line, then checks to see if the string you scanned can be turned into an int, Integer.parseInt(String). When this errors out, the scanner will try to read the next line rather than the same line since it already successfully read the previous one.
Re: Help with Java
HOORAY IT WORKS!!! Thanks Ludi, I owe you one!
J_Mart29- Posts : 32
Join date : 2012-06-06
Location : Virginia
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|