the amount of balance is fix to 10 and when the number 10 add 0.70 it will be 10.70 and carry forward the sum and add the next number of 10.70 + 2.4 become 13.1. instead it renew to the value 10 and add 2.4 become 12.4. I don't want it to renew to 10 whenever it adds new number , because I want to have the final balance of the adding.
input ;
3
=
+ 0.70
+ 2.40
+ 1.20
expected output
10.0
10.7
13.1
14.3
my output;
10.0
10.7
12.4
11.2
My code;
import java.util.Scanner;
public class TestMyPrintCard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=1;
int N = sc.nextInt();
MyPrintCard c1 = new MyPrintCard(); //declare obj
for(int i=0; i<=N; i++) {
char character = sc.next().charAt(0); // for the character input, =,+,-
if (character=='=') {
System.out.println(c1.getBalance());
} else if (character=='+') {
double topup=sc.nextDouble();
c1.topupCard(topup);
}//end for
}
}
the class ;
public class MyPrintCard {
double balance; //declare
MyPrintCard(){ //constructor initialised
balance = 10.00;
}
//getter
public double getBalance() {
return balance;
}
public void topupCard(double amt) {
double addBalance = balance + amt;
System.out.println(addBalance);
}
Aucun commentaire:
Enregistrer un commentaire