Java 控制语句:为什么我的';新余额';当I';记入贷方的费用';然后';支付信用卡';两次?
我从一开始就试着阅读这本Java书,并在暑假做一些练习。我提出了这个问题: “(信用额度计算器)开发一个Java应用程序,确定 百货公司客户已超过收费帐户的信用限额……对于那些信用限额被超过的客户,程序应显示 “超出信用额度”消息 我的代码只有在我使用一次费用贷记时才有效。如果我再次使用Charge to Credit,我的新余额值将发生变化。例如: 这是我现在的问题。我的新余额应该仍然是22000,因为信用额度已经超过了。但现在是18000。我不知道哪里出了问题,所以我需要帮助 这是我的代码: Customer.javaJava 控制语句:为什么我的';新余额';当I';记入贷方的费用';然后';支付信用卡';两次?,java,while-loop,switch-statement,calculator,credits,Java,While Loop,Switch Statement,Calculator,Credits,我从一开始就试着阅读这本Java书,并在暑假做一些练习。我提出了这个问题: “(信用额度计算器)开发一个Java应用程序,确定 百货公司客户已超过收费帐户的信用限额……对于那些信用限额被超过的客户,程序应显示 “超出信用额度”消息 我的代码只有在我使用一次费用贷记时才有效。如果我再次使用Charge to Credit,我的新余额值将发生变化。例如: 这是我现在的问题。我的新余额应该仍然是22000,因为信用额度已经超过了。但现在是18000。我不知道哪里出了问题,所以我需要帮助 这是我的代码:
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
public class CreditLimitCal {
public static void main (String[] args){
Customer cus = new Customer();
cus.start();
}
}
CreditLimitCal.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
public class CreditLimitCal {
public static void main (String[] args){
Customer cus = new Customer();
cus.start();
}
}
这一行:
newBalance = beginBal + itemsCharged - creditsPaid;
不需要减去贷方余额
,以前当用户付清部分余额时,您已经这样做了
newBalance每次只能修改一件事,因此对于案例1:
newBalance = newBalance + itemsCharged;
案例2:
newBalance = newBalance - creditsPaid;
但情况1也是如此。因此,如果我删除了它,那么我的新余额将越来越大,丢弃已支付的积分-/成功了,谢谢!但当我继续问“你有其他交易吗?”并输入另一个帐户编号时,开始余额为10000,信用额度为15000。然后我第一次在那个账户上记入贷方,上面写着“超出了贷方限额”。然后余额将是acc num 123中的上一个余额。@javanerd在start()中您应该将newBal设置为beginBal,并重置其他值。现在一切都好了!非常感谢你!我现在可以安息了。英雄联盟