初学者Java程序员算术错误 [英] Beginner Java programmer arithmetic error

查看:102
本文介绍了初学者Java程序员算术错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始从事Java编程,并认为自己是编程新手.似乎我的源代码算法有问题.我已经验证了所有嵌套的if-else语句,它们都可以达到最终else语句的算法.它计算不正确,就像上面的if-else语句一样,我已经设置了算法.

I've recently embarked into Java programming and consider myself a programming novice. It seem that I'm having an issue with my source code arithmetic. I have verified all the nested if-else statements and they all work up to the final else statement's arithmetic. It doesn't calculating correctly I have set the arithmetic up just as the above if-else statements.

else语句假定从金额中减去40,然后加上1%的费用.我已经尝试过else语句fee = ((checkAmount - 40) * .01)fee = ((checkAmount * .01) - 40)

the else statement is suppose to subtract 40 from the amount and then apply 1 percent charge. I have tried for the else statement fee = ((checkAmount - 40) * .01) and fee = ((checkAmount * .01) - 40)

这只是本书的练习

  import java.util.Scanner;
public class ServiceCharge {
public static void main(String[] args)
{
    double checkAmount;
    double fee;
    Scanner kb = new Scanner(System.in);
    System.out.println("I will calulate the service charge to cash your check");
    System.out.print("Enter amount of check: $");
    checkAmount = kb.nextDouble();

    if (checkAmount > 0)
    {
        if (checkAmount <= 10)
        {
            fee = -1;
            System.out.println("$1 service charge");
            checkAmount = checkAmount + fee;
            System.err.println("You have " + checkAmount + " left after service charge.");
        }
        else if ((checkAmount > 10) && (checkAmount <= 100))
        {
            System.out.println("There will be a 10 percent charge.");
            fee = (checkAmount * .10);
            checkAmount = checkAmount - fee;
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n" , checkAmount);
        }
        else if ((checkAmount > 100) && (checkAmount <= 1000))
        {
            System.out.println("There will be a $5 charge plus 5 percent");
            fee = ((checkAmount - 5) * .05);
            checkAmount = (checkAmount - fee);
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n", checkAmount);
        }
        else
        {
            System.out.println("$40 processing fee plus 1 percent");
            fee = ((checkAmount - 40) * .01);
            checkAmount = (checkAmount - fee);
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n" , checkAmount);
        }
        System.out.println("Thanks for using Service Charger." + "\nGood bye");
    }
}
}

推荐答案

            System.out.println("$40 processing fee plus 1 percent");
            fee = ((checkAmount - 40) * .01);

那不是40美元的费用+ 1%.那是略低于1%的费用;好像您是免费兑现了前40美元,然后对剩余的部分收取1%的费用.

That's not a 40 dollar fee + 1 percent. That's a fee of slightly less than 1 percent; it's as if you cash out the first 40 dollars for free, and then apply a 1 percent charge to the rest.

假设整张支票都收取1%的费用,而不是减去40美元后剩下的钱,那笔费用的正确表达是

Assuming the 1% charge applies to the whole check, rather than what's left after subtracting 40 dollars, the correct expression for the fee is

            fee = 40 + 0.01*checkAmount;

这篇关于初学者Java程序员算术错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆