Welcome to LudiGames Forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Help with Java

2 posters

Go down

Help with Java Empty Help with Java

Post  J_Mart29 Sat Sep 28, 2013 6:18 pm

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.
J_Mart29
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  LudicrousYoshi Sun Sep 29, 2013 4:37 pm

Do you understand how to use while loops and one-way flags?
LudicrousYoshi
LudicrousYoshi
Admin

Posts : 1337
Join date : 2010-12-05
Location : Virginer

https://ludigames.forumotion.com

Back to top Go down

Help with Java Empty Re: Help with Java

Post  J_Mart29 Mon Sep 30, 2013 1:06 pm

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
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  LudicrousYoshi Mon Sep 30, 2013 6:18 pm

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:

Code:
boolean error;
do{
    error = false;
    ...your code...
    catch(Exception e)
        error = true;
} while(error);
LudicrousYoshi
LudicrousYoshi
Admin

Posts : 1337
Join date : 2010-12-05
Location : Virginer

https://ludigames.forumotion.com

Back to top Go down

Help with Java Empty Re: Help with Java

Post  J_Mart29 Tue Oct 01, 2013 11:23 pm

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);
        }
also should I include sheepPlus<0 as an error = true?
J_Mart29
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  LudicrousYoshi Wed Oct 02, 2013 1:04 am

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
LudicrousYoshi
LudicrousYoshi
Admin

Posts : 1337
Join date : 2010-12-05
Location : Virginer

https://ludigames.forumotion.com

Back to top Go down

Help with Java Empty Re: Help with Java

Post  J_Mart29 Wed Oct 02, 2013 4:35 pm

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
(Cool  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
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  J_Mart29 Wed Oct 02, 2013 4:39 pm

*except instead of Cool its an 8 parentheses
J_Mart29
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  LudicrousYoshi Wed Oct 02, 2013 5:40 pm

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.
LudicrousYoshi
LudicrousYoshi
Admin

Posts : 1337
Join date : 2010-12-05
Location : Virginer

https://ludigames.forumotion.com

Back to top Go down

Help with Java Empty Re: Help with Java

Post  J_Mart29 Wed Oct 02, 2013 10:10 pm

HOORAY IT WORKS!!! Thanks Ludi, I owe you one!
J_Mart29
J_Mart29

Posts : 32
Join date : 2012-06-06
Location : Virginia

Back to top Go down

Help with Java Empty Re: Help with Java

Post  LudicrousYoshi Thu Oct 03, 2013 12:13 am

Glad I could help Very Happy
LudicrousYoshi
LudicrousYoshi
Admin

Posts : 1337
Join date : 2010-12-05
Location : Virginer

https://ludigames.forumotion.com

Back to top Go down

Help with Java Empty Re: Help with Java

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum