作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试从头开始阅读这本 Java 书,并在暑假期间做了一些练习。我想出了这个问题:“(信用限额计算器)开发一个 Java 应用程序,用于确定多个信用限额中的任何一个百货商店客户已超出赊账账户的信用限额......对于超出信用限额的客户,程序应显示消息“超出信用限额”。”
仅当我使用 Charge to Credit ONCE 时,我的代码才有效。如果我再次使用Charge to Credit,我的新余额值将会改变。示例:
* Acc # = 123
* Beginning Balance = 20000
* Credit Limit = 25000
* Select Transaction = 1 (Charge to Credit)
* Amount to charge = 4000
* Select Transaction = 2 (Pay credit)
* Amount to pay = 2000 [My balance should now be 22000)
* Select Transaction = 1 (Charge to Credit)
* Amount to charge = 10000 [This shud exceed credit limit]
* Select Transaction = 3 (Balance Inquiry)
* My new balance is now 18000This is my problem now. My new balance should still be 22000 since the credit limit has exceeded. But now its 18000. I don't know where it went wrong so I need help, please.
这是我的代码:
Customer.java
import java.util.Scanner;
public class Customer {
Scanner input = new Scanner (System.in);
int accNum,
beginBal,
creditLimit;
int itemsCharged,
creditsPaid,
newBalance;
int transaction;
String action;
public void start (){
System.out.print("\nAccount Number: ");
accNum = input.nextInt();
System.out.print("Beginning Balance: ");
beginBal = input.nextInt();
System.out.print("Credit Limit: ");
creditLimit = input.nextInt();
transaction();
}
public void transaction (){
boolean loop = true;
while (loop){
System.out.println("\n[1] Charge to Credit \n[2] Pay Credit \n[3] Balance Inquiry/Exit");
System.out.print("Select Transaction #: ");
transaction = input.nextInt();
switch (transaction){
case 1:
System.out.print("\n--CHARGE TO CREDIT-- \nEnter amount: ");
itemsCharged = input.nextInt();
newBalance = beginBal + itemsCharged - creditsPaid;
if (newBalance > creditLimit){
System.err.println("Credit Limit Exceeded!");
newBalance -= itemsCharged;
} else {
System.out.println("Amount charged.");
}
break;
case 2:
System.out.print("\n--PAY CREDIT-- \nEnter amount: ");
creditsPaid = input.nextInt();
newBalance = beginBal + itemsCharged - creditsPaid;
if (creditsPaid > newBalance){
System.err.println("Invalid Amount!");
newBalance -= creditsPaid;
} else {
System.out.println("Payment posted!");
newBalance = beginBal + itemsCharged - creditsPaid;
}
break;
case 3:
System.out.println("\n--BALANCE INQUIRY-- \nNew Balance: " + newBalance);
restart();
break;
default:
System.err.println("Invalid number!");
transaction();
break;
}
}
}
public void restart (){
System.out.println("\nDo you have another transaction? [Y/N]");
System.out.print("Select Action: ");
boolean loop = true;
while (loop){
action = input.nextLine();
switch (action){
case "Y":
start();
break;
case "N":
System.err.println("Terminated.");
System.exit(0);
break;
}
}
}
} // end class
CreditLimitCal.java
public class CreditLimitCal {
public static void main (String[] args){
Customer cus = new Customer();
cus.start();
}
}
最佳答案
这一行:
newBalance = beginBal + itemsCharged - creditsPaid;
不需要减去creditsPayed
,之前当用户还清部分余额时您已经完成了该操作。
newBalance 每次只能修改一项,因此对于情况 1:
newBalance = newBalance + itemsCharged;
案例2:
newBalance = newBalance - creditsPaid;
关于java - 控制语句: Why is my 'New Balance' change when I 'charge to credit' then 'pay credit' twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16343008/
我是一名优秀的程序员,十分优秀!