+ 1

whats wrong with my text based mini-game? (java)

so i tried to make a text based mini - game but it not works like i think, i tried solve it many times but i failed probably the problem is on loop statement EDIT (FIXED): i've fixed loop problem while writing this post xd, anyway the problem is chopping tree's not giving any amount to purse variable, whats the problem with currency? EDIT 2: thx jonas i learned something new and issue get fixed, but problem is rn with the multiplier i tried change position of if but its not fixed woodPrice must 2x when axe is bought but for some reason this not works package basics; import java.util.Scanner; public class basc1 { public static void main(String[] args) { Scanner terminal = new Scanner(System.in); System.out.println("WELCOME TO TREE CHOPPING GAME! \nstart the game by typing chop tree!"); int purse = 5; int multiplier = 1; double woodPrice = 10; boolean axeEquipped = false; final int axePrice = 20; String movement = terminal.nextLine(); while (!movement.equals("quit game")) { movement = terminal.nextLine(); if (movement.equals("buy axe")) { if (purse > axePrice) { if (axeEquipped == true) multiplier += 2; System.out.println("now you earn 2x wood!"); purse -= axePrice; axeEquipped = true; System.out.println("AXE BOUGHT!"); } else if (purse < axePrice) { System.out.println((purse - axePrice) + "missing cash"); } } switch(movement) { case "bank": System.out.println("bank: " + purse); break; case "chop tree": System.out.println("tree chopped"); purse += woodPrice * multiplier; break; case "/help": System.out.println("Commands listed ---> 'chop tree', 'buy axe', 'bank', 'quit game'"); break; } } } }

23rd Jan 2022, 8:20 AM
berkay er
berkay er - avatar
3 Antworten
+ 3
You have all these inside the loop: double purse = 0; double woodPrice = 1.5; boolean sword = false; final int swordPrice = 20; That means, with every cycle of the game, purse is reset to zero, sword possession is reset to false ... you get the idea :) Have all the game state initialisation OUTSIDE of the loop. The loop only modifies them.
23rd Jan 2022, 8:39 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
hey jonas i have one more question, can you take a look code to find out why multiplier not works?
23rd Jan 2022, 10:32 AM
berkay er
berkay er - avatar
+ 1
What seems to be the problem? Things that I can make out: 1) axeEquipped is a boolean. It is already a truth value, so you don't need to check if(axeEquipped == true), it is enough to test if(axeEquipped). 2) There are missing curly braces after the if(axeEquipped == true), so, only the next statement belongs to that if. "Now you earn 2x wood!" will be printed if purse>axePrice, regardless of axeEquipped.
23rd Jan 2022, 11:42 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar