#How would i be able to make the total balance stack ?
18 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @sweet needle! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Please format your code to make it more readable.
For java, it should look like this:
```โโโ`โjava
public void foo() {
}
โโโ`โ```
''' import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("Welcome to your Bank Account");
System.out.println("Type in one of the options");
System.out.println("1: Deposit ");
System.out.println("2: Withdraw ");
System.out.println("3: Transaction History ");
System.out.println("4: Total Balance");
int option = scanner.nextInt();
int deposit = 1;
int withdraw = 2;
int transactionHistory = 3;
int totalBalance = 4;
int total = 0;
if(option == deposit){
System.out.println("You have chosen to Deposit money. How much would you like to deposit?");
int x = scanner.nextInt();
System.out.println("Total balance:" + total + x);
}
if (option == withdraw) {
System.out.println("You have chosen to Withdraw money. How much would you like to withdraw? ");
int y = scanner.nextInt();
System.out.println("Total balance:"+ (total - y ));
}
}
}
}''' ```
This message has been formatted automatically. You can disable this using /preferences.
Give the total variable a longer lifetime. As you create it in the loop, it loses all memory from one iteration of the loop to another.
If instead you created it before the loop, then it would persist throughout the entire loop, always keeping the value you put to it so far.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
'''import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int total = 0;
while(true){
System.out.println("Welcome to your Bank Account");
System.out.println("Type in one of the options");
System.out.println("1: Deposit ");
System.out.println("2: Withdraw ");
System.out.println("3: Total Balance");
int option = scanner.nextInt();
int deposit = 1;
int withdraw = 2;
int transactionHistory = 3;
int totalBalance = 4;
if(option == deposit){
System.out.println("You have chosen to Deposit money. How much would you like to deposit?");
int x = scanner.nextInt();
System.out.println("Total balance:" + total + x);
total += x;
}
if (option == withdraw) {
System.out.println("You have chosen to Withdraw money. How much would you like to withdraw? ");
int y = scanner.nextInt();
System.out.println("Total balance:"+ (total - y ));
total -= y;
}
if (option == transactionHistory) {
System.out.println("Your total balance is" + total );
}
}
}
}''' ```
This message has been formatted automatically. You can disable this using /preferences.
Just like one of them needed parentheses, the other needs parentheses too
And, again, it would be preferable to update the total first, and to display what's the new total then
Talk to us about this line:
System.out.println("Total balance:"+ (total - y ));
Prefer an int that represents cents instead of representing dollars
Is there another line where you display the total on which you make another operation?
Just have the two lines look the same with similar parentheses
It's weird that you haven't tried
Or just make the program better and don't compute operations in lines that display things
๐ค Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
'''import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
while(true){
System.out.println("Welcome to your Bank Account");
System.out.println("Type in one of the options");
System.out.println("1: Deposit ");
System.out.println("2: Withdraw ");
System.out.println("3: Total Balance");
int option = scanner.nextInt();
int deposit = 1;
int withdraw = 2;
int transactionHistory = 3;
int totalBalance = 4;
if(option == deposit){
System.out.println("You have chosen to Deposit money. How much would you like to deposit?");
double x = scanner.nextDouble();
total += x;
System.out.println("Total balance:" + total + " dollars");
}
if (option == withdraw) {
System.out.println("You have chosen to Withdraw money. How much would you like to withdraw? ");
double y = scanner.nextDouble();
total -= y;
System.out.println("Total balance:"+ total + " dollars");
}
if (option == transactionHistory) {
System.out.println("Your total balance is" + total + "dollars" );
}
if (option == )
}
}
}''' ```
This message has been formatted automatically. You can disable this using /preferences.
Use switch or an if/else if ladder. Why do you need to ask about that kind of things, that's precisely what these syntaxes exist for