gpt4 book ai didi

java - 控制语句: Why is my 'New Balance' change when I 'charge to credit' then 'pay credit' twice?

转载 作者:行者123 更新时间:2023-12-01 14:37:33 26 4
gpt4 key购买 nike

我尝试从头开始阅读这本 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 18000

This 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com