So I've been reading and teaching myself java with a bunch of books, and tried to make a few programs based more on being interactive than functional. Anyways there are these two files I've been running in eclipse and neither of them give me error messages for syntax or during operation. At the same time, neither of these work properly. This has to do with the Scanner utility (java.util.Scanner). I'm pretty sure it has to do with the way my blocks are arranged. What happens in SpaceCats.java is you enter your guess and nothing happens. And in JerryStore.java when you make your selection it gives a correct result, but won't let you continue entering numbers :sad.gif:
CODE:
SpaceCats.java
package misc;
import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
public class SpaceCats
{
public static void main(String args[])
{
out.println("WELCOME TO JERRY'S SPACE-CAT HEAD GUESSING GAME!");
out.println("");
out.println("Hey it's Jerry again! I've been out hunting Space-Cats and I was wondering if you wanted to play a little game.");
out.println("So here's how it works, If you guess exactly how many Space-Cat heads I got (between 0 and 20), I'll give you $10.00!");
out.println("If you guess with-in 3 of the amount of heads I got I'll give you a $2.50 prize. And if you're any further off");
out.println(" that's a big whopping zero for you! One more thing. You only get 20 guesses!");
out.println("");
out.println("YOUR GUESS: ");
Scanner inputReader = new Scanner(System.in);
int guesses = 0;
int guess = inputReader.nextInt();
float cash = 0.0F;
int spaceCatHeads = new Random().nextInt(20) + 1;
int rndmOffLess = (spaceCatHeads - 4);
int rndmOffMore = (spaceCatHeads + 4);
boolean good;
boolean bad;
if (spaceCatHeads < 10){
good = false;
bad = true;
}else{
good = true;
bad = false;
}
if (guess == spaceCatHeads){
out.println("Wow! What a good guess! Now you've got $" + cash + "!");
guesses = +1;
cash = +10.00F;
if (guess > rndmOffLess && guess < spaceCatHeads){
out.println("That was a close guess... But NOT CLOSE ENOUGH! Now you've got $" + cash + ".");
out.println("Hey, you've only got " + guesses + " left.");
cash = +2.5F;
guesses = +1;
if (good = true){
out.println("I am a miraculous Space-Cat hunter!");
out.println("Hey, you've only got " + guesses + " left.");
}
if (bad = true){
out.println("I ain't so goo at hunting those silly Space-Cats am I?");
out.println("Hey, you've only got " + guesses + " left.");
}
}
if (guess < rndmOffMore && guess > spaceCatHeads){
out.println("That was a close guess... But NOT CLOSE ENOUGH! Now you've got $" + cash + ".");
out.println("Hey, you've only got " + guesses + " left.");
cash = +2.5F;
guesses = +1;
}
}
}
}
JerryStore.java
package misc;
import static java.lang.System.out;
import java.util.Scanner;
public class JerryStore
{
double cash = 0.0D;
static boolean hasItemOne = false;//My possessive booleans
static boolean hasItemTwo = false;
static boolean hasItemThree = false;
static boolean hasItemFour = false;
static boolean hasItemFive = false;
public static void main(String args[])
{
double cash = 900.00D;//money
out.println("Hey, welcome to my store. I can sell you all kinds of useless stuff here! Why don't you brows a little?");
out.println("");
out.println("Your monies: $" + cash + "");
out.println("");
int itemOne = 1;//item selection numbers
int itemTwo = 2;
int itemThree = 3;
int itemFour = 4;
int itemFive = 5;
int leave = 0;
int viewStash = 9;
double itemOneCost = 23.5D;//item prices
double itemTwoCost = 54.4D;
double itemThreeCost = 107.6D;
double itemFourCost = 138.2D;
double itemFiveCost = 216.9D;
out.println("~Item One~ price: $" + itemOneCost + "");
out.println("~Item Two~ price: $" + itemTwoCost + "");
out.println("~Item Three~ price: $" + itemThreeCost + "");
out.println("~Item Four~ price: $" + itemFourCost + "");
out.println("~Item Five~ price: $" + itemFiveCost + "");
out.println("");
out.println("Leave: 0");
out.println("View Stash: 9");
Scanner inputReader = new Scanner(System.in); //My scanner
int input = inputReader.nextInt();
if (input == itemOne){//basically, if you try to buy this you either can and do, or can't and don't
if (cash >= itemOneCost){
out.println("Well well... It looks like you bought the *Commemerative Jerry Bobblehead*! Have fun with it!");
cash = (cash - itemOneCost);
hasItemOne = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemTwo){
if (cash >= itemTwoCost){
out.println("I think you'll enjoy this *Golden Model of a Lama*. It's Quite useless.");
cash = (cash - itemTwoCost);
hasItemTwo = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemThree){
if (cash >= itemThreeCost){
out.println("It's going to be hard, but for a brother like you I guess I'll give away my *Pixie Power Teddy Bear*. Enjoy!");
cash = (cash - itemThreeCost);
hasItemThree = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemFour){
if (cash >= itemFourCost){
out.println("Well looky here. Looks like you'v finally come to terms with the *Uber Jerry Marble*. Well, TAKE IT!");
cash = (cash - itemFourCost);
hasItemFour = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemFive){
if (cash >= itemFiveCost){
out.println("This is my final product: the *Shiny Flashing Button*. And it looks like it's yours!");
cash = (cash - itemFiveCost);
hasItemFive = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == leave){
out.println("K, thanks for buying my stuff! Come back again if you need to waste money!");
}
if (input == viewStash){//This displays the current inventory of the user
if (hasItemOne == true){
out.println("You already have the *Commemerative Jerry Bobblehead*.");
}else{
out.println("You don't own item one.");
}
if (hasItemTwo == true){
out.println("You already have the *Golden Model of a Lama*.");
}else{
out.println("You don't own item two.");
}
if (hasItemThree == true){
out.println("You already have the *Pixie Power Teddy Bear*.");
}else{
out.println("You don't own item three.");
}
if (hasItemFour == true){
out.println("You already have the *Uber Jerry Marble*.");
}else{
out.println("You don't own item four.");
}
if (hasItemFive == true){
out.println("You already have the *Shiny Flashing Button*.");
}else{
out.println("You don't own item five.");
}
}
}
}
Any help would be appreciated. I'm an eager mind and am not bored by long explanations :smile.gif:
The reason it only works once (or will only work once) is because you dont have you main logic in a loop. In other words, its going to execute the code only once then end the program.
Secondly, this part:
if (guess == spaceCatHeads){
out.println("Wow! What a good guess! Now you've got $" + cash + "!");
guesses = +1;
cash = +10.00F;
is going to print out the cash BEFORE you add the 10.00 to it. A quick fix is just putting the output at the end. I see you've done this in all the cases.
For the SpaceCats file it looks like this entire block of code:
if (guess == spaceCatHeads){
out.println("Wow! What a good guess! Now you've got $" + cash + "!");
guesses = +1;
cash = +10.00F;
if (guess > rndmOffLess && guess < spaceCatHeads){
out.println("That was a close guess... But NOT CLOSE ENOUGH! Now you've got $" + cash + ".");
out.println("Hey, you've only got " + guesses + " left.");
cash = +2.5F;
guesses = +1;
if (good = true){
out.println("I am a miraculous Space-Cat hunter!");
out.println("Hey, you've only got " + guesses + " left.");
}
if (bad = true){
out.println("I ain't so goo at hunting those silly Space-Cats am I?");
out.println("Hey, you've only got " + guesses + " left.");
}
}
if (guess < rndmOffMore && guess > spaceCatHeads){
out.println("That was a close guess... But NOT CLOSE ENOUGH! Now you've got $" + cash + ".");
out.println("Hey, you've only got " + guesses + " left.");
cash = +2.5F;
guesses = +1;
}
}
You have wrapped an entire if statement around all of the other if statements. That first if statement relies on the answer being the exact amount of heads collected so if you type anything else it will not work.
You also put all the game logic inside of a loop and have a boolean controlling if it is running or not so for example:
The reason it only works once (or will only work once) is because you dont have you main logic in a loop. In other words, its going to execute the code only once then end the program.
Secondly, this part:
if (guess == spaceCatHeads){
out.println("Wow! What a good guess! Now you've got $" + cash + "!");
guesses = +1;
cash = +10.00F;
is going to print out the cash BEFORE you add the 10.00 to it. A quick fix is just putting the output at the end. I see you've done this in all the cases.
Change those and it should work.
Thanks! I now understand. It's taking a while for me to realize that though java is OO, it still does have a step by step flow :tongue.gif:
You have wrapped an entire if statement around all of the other if statements. That first if statement relies on the answer being the exact amount of heads collected so if you type anything else it will not work.
I knew it was syntax! I just couldn't find it. Thanks for the help! I'll go try that right now :biggrin.gif:
if (input == itemOne){//basically, if you try to buy this you either can and do, or can't and don't
if (cash >= itemOneCost){
out.println("Well well... It looks like you bought the *Commemerative Jerry Bobblehead*! Have fun with it!");
cash = (cash - itemOneCost);
hasItemOne = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemTwo){
if (cash >= itemTwoCost){
out.println("I think you'll enjoy this *Golden Model of a Lama*. It's Quite useless.");
cash = (cash - itemTwoCost);
hasItemTwo = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemThree){
if (cash >= itemThreeCost){
out.println("It's going to be hard, but for a brother like you I guess I'll give away my *Pixie Power Teddy Bear*. Enjoy!");
cash = (cash - itemThreeCost);
hasItemThree = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemFour){
if (cash >= itemFourCost){
out.println("Well looky here. Looks like you'v finally come to terms with the *Uber Jerry Marble*. Well, TAKE IT!");
cash = (cash - itemFourCost);
hasItemFour = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == itemFive){
if (cash >= itemFiveCost){
out.println("This is my final product: the *Shiny Flashing Button*. And it looks like it's yours!");
cash = (cash - itemFiveCost);
hasItemFive = true;
out.println("Looks like now you have about $" + cash + " on you...");
}else{
out.println("HEY! YOU DON'T HAVE ENOUGH MONEY TO BUY THAT! DON'T GO AROUND TRYING TO MOOCH PEOPLE!");
}
}
if (input == leave){
out.println("K, thanks for buying my stuff! Come back again if you need to waste money!");
}
if (input == viewStash){//This displays the current inventory of the user
if (hasItemOne == true){
out.println("You already have the *Commemerative Jerry Bobblehead*.");
}else{
out.println("You don't own item one.");
}
if (hasItemTwo == true){
out.println("You already have the *Golden Model of a Lama*.");
}else{
out.println("You don't own item two.");
}
if (hasItemThree == true){
out.println("You already have the *Pixie Power Teddy Bear*.");
}else{
out.println("You don't own item three.");
}
if (hasItemFour == true){
out.println("You already have the *Uber Jerry Marble*.");
}else{
out.println("You don't own item four.");
}
if (hasItemFive == true){
out.println("You already have the *Shiny Flashing Button*.");
}else{
out.println("You don't own item five.");
}
}
?
(and I don't know why it rearranged the lines, getting the difference for cash is before println in eclipse :S)
CODE:
SpaceCats.java
JerryStore.java
Any help would be appreciated. I'm an eager mind and am not bored by long explanations :smile.gif:
The reason it only works once (or will only work once) is because you dont have you main logic in a loop. In other words, its going to execute the code only once then end the program.
Secondly, this part:
is going to print out the cash BEFORE you add the 10.00 to it. A quick fix is just putting the output at the end. I see you've done this in all the cases.
Change those and it should work.
You have wrapped an entire if statement around all of the other if statements. That first if statement relies on the answer being the exact amount of heads collected so if you type anything else it will not work.
You also put all the game logic inside of a loop and have a boolean controlling if it is running or not so for example:
Want a place to advertise your Minecraft server? try MyMCStatus.net now!
Thanks! I now understand. It's taking a while for me to realize that though java is OO, it still does have a step by step flow :tongue.gif:
I knew it was syntax! I just couldn't find it. Thanks for the help! I'll go try that right now :biggrin.gif:
?
(and I don't know why it rearranged the lines, getting the difference for cash is before println in eclipse :S)