- Joined
- May 11, 2024
- Messages
- 16
- Reaction score
- 0
- Points
- 6
this is same problem of c# lab yesterday but this use parsing or string to double
Java:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.Scanner;
class Main {
public static void main(String[] args) {
String Balance = "1000.0";
double nb =Double.valueOf(Balance);
Scanner s = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = s.nextLine();
int i =0;
for(i = 0; i <= 5; i++){
System.out.println("press 1 to check balance");
System.out.println("press 2 to deposit");
System.out.println("press 3 to withdraw");
System.out.println("press 4 to exit");
int n = s.nextInt();
if(n == 1){
System.out.printf("Account name: %s\nyour balance is: %.2f \n",name,nb);
}else if(n==2){
System.out.print("Enter deposit amount: ");
double n1 = s.nextDouble();
nb+=n1;
System.out.printf("your new balance is: %.2f\n", nb);
}else if(n==3){
System.out.print("Enter withdraw amount: ");
double n2 = s.nextDouble();
nb-=n2;
System.out.printf("your new balance is: %.2f \n", nb);
}else if(n==4){
break;
}
else{
System.out.print("Invalid number!"+"\n");
}
}
}
}