import java.util.Scanner;
public class bankcounter {
public static void main(String []args) {
int numchecks;
double totalfeeforchecks;
double totalfee;
double totalbasecharge;
double bankfee;
Scanner in = new Scanner(System.in);
System.out.printf("Enter in the amount of checks you wish to use: ");
numchecks = in.nextInt();
totalbasecharge = 20.00;
bankfee = 10.00;
totalfeeforchecks = 0;
if(numchecks > 0 && numchecks < 20)
{
totalfeeforchecks += (numchecks * .10);
}
else
{
totalfeeforchecks += (19 * .10);
numchecks -= 19;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (numchecks * .08);
}
else
{
totalfeeforchecks += (20 * .08);
numchecks -= 20;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (numchecks * .06);
}
else
{
totalfeeforchecks += (20 * .06);
numchecks -= 20;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (20 * .04);
numchecks -= 20;
}
else
{
totalfeeforchecks += (numchecks * .04);
}
totalfee = totalfeeforchecks + totalbasecharge + bankfee;
System.out.printf("Based on our check fee schedule: .10 cents for less than 20 checks :: .08 cents for 20-39 checks :: .06 cents for 40 to 59 checks :: .04 cents each for 60 or more checks\n\nYour charges are as follows: \nTotal fee = %.2f \n\tTotal base charge = \t%.2f\n\tTotal bank fee = \t%.2f\n\tTotal fee for checks = \t%.2f", totalfee, totalbasecharge, bankfee, totalfeeforchecks);
因此,如果我为numchecks输入9,则支票的总费用应该只有.90,而不是2.96。我的if语句不起作用吗?我想如果我把9放在numchecks上,它应该只运行超过第一个条件,因为我嵌套了其余的......帮助?
答案 0 :(得分:1)
我建议您使用IDE中的工具,例如格式化程序和调试程序。使用这些工具,您会看到此行有四次
if(numchecks > 0 && numchecks <= 20)
这是正确的四次,所以代替if
不工作,事实上工作比你想象的更多。
这意味着您正在运行
totalfeeforchecks += (numchecks * .10); // 0.9
totalfeeforchecks += (numchecks * .08); // 0.72
totalfeeforchecks += (numchecks * .06); // 0.54
totalfeeforchecks += (20 * .04); // 0.8
// total is 2.96
答案 1 :(得分:1)
让我为您修复缩进,而不实际更改代码:
import java.util.Scanner;
public class bankcounter {
public static void main(String []args) {
int numchecks;
double totalfeeforchecks;
double totalfee;
double totalbasecharge;
double bankfee;
Scanner in = new Scanner(System.in);
System.out.printf("Enter in the amount of checks you wish to use: ");
numchecks = in.nextInt();
totalbasecharge = 20.00;
bankfee = 10.00;
totalfeeforchecks = 0;
if(numchecks > 0 && numchecks < 20)
{
totalfeeforchecks += (numchecks * .10);
}
else
{
totalfeeforchecks += (19 * .10);
numchecks -= 19;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (numchecks * .08);
}
else
{
totalfeeforchecks += (20 * .08);
numchecks -= 20;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (numchecks * .06);
}
else
{
totalfeeforchecks += (20 * .06);
numchecks -= 20;
}
if(numchecks > 0 && numchecks <= 20)
{
totalfeeforchecks += (20 * .04);
numchecks -= 20;
}
else
{
totalfeeforchecks += (numchecks * .04);
}
totalfee = totalfeeforchecks + totalbasecharge + bankfee;
原因现在应该是显而易见的。