首页 > 解决方案 > Java 支票簿平衡器

问题描述

我无法更正以下代码。它没有给我任何语法错误,但我知道它背后有一些逻辑代码,我无法指出并纠正它。

我还是编码新手,所以我必须从你的帮助中学习。谢谢!

这是我应该编写代码的问题:

Make a simple checkbook balancer that would input a sequence of 
deposits (positive floating-point values) and 
issuances (negative floating-point values) terminated by a 0. 
It would output the transaction type, the amount and the running balance for each entry. 
Assume the initial balance of 0. Ensure that the issued check won’t bounce!
import java.util.*;
import java.lang.*;

public class Checkbook{
    public static void main (String[]args){
        Scanner input = new Scanner(System.in);
        float bal=0, amt=0;
        char reply;

        do{
            System.out.print("Input Amount: ");
            amt=input.nextInt();
            if(amt>0){

                bal+=amt;

                System.out.print("\nAmount: " +amt);
                System.out.print("\nTransaction Line: Deposit");
                System.out.print("\nRunning Balance: " +bal);
                ;
                }


            else if(amt<0 ){

                bal-=amt;

                System.out.print("\nAmount: " +Math.abs(amt));
                System.out.print("\nTransaction Line: Issuance");
                System.out.print("\nRunning Balance: " +bal);
                ;
                }
            else

                do{
                            System.out.println("Hello world!");
                            System.out.println("Again? [y/n]");
                            reply=input.next().charAt(0);   
                }while(reply!='n');



        }while(bal!=0);

    }
}

这就是它目前的样子。 在此处输入图像描述

标签: javaiteration

解决方案


推荐阅读