我已經制作了自動提款機(ATM)程序,但我無法弄清楚什麼是錯的。除了交易是零線之外,一切都在起作用。我一定弄錯了一些東西..我認爲這是「showTransactions」方法的一些東西。感謝幫助!簡單的ATM。在ATM程序中取代零交易而不是ATM交易
import java.util.Scanner;
public class ATM {
private static int position = - 1;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Set a value to the various
int amount = 0;
int choice = 0;
int sum;
int balance = 0;
int[] trans = new int[10];
while (choice != 4) {
choice = menu();
switch (choice) {
case 1:
System.out.print("How much would you like to deposit?: ");
System.out.println();
sum = input.nextInt();
if (sum == 0) {
System.out.print("Unable to deposit");
System.out.println();
System.out.println();
} else {
balance += sum;
makeTransactions(trans, amount);
}
break;
case 2:
System.out.print("How much would you like to withdraw?");
System.out.println();
sum = input.nextInt();
if (sum == 0 || sum > balance) {
System.out.print("Unable to withdraw");
System.out.println();
System.out.println();
} else {
balance += - sum;
makeTransactions(trans, amount);
}
break;
case 3:
showTransactions(trans, balance);
break;
case 4:
System.out.println("You chosed to end ");
break;
}
}
}
public static int menu() {
Scanner input = new Scanner(System.in);
int choice = 0;
System.out.println("Cash machine");
System.out.println();
System.out.println("------------------------");
System.out.println("1 - Deposit *");
System.out.println("2 - Withdraw *");
System.out.println("3 - View transactions *");
System.out.println("4 - End cash machine *");
System.out.println("------------------------");
System.out.println();
System.out.println("Enter your choice: ");
choice = input.nextInt();
return choice;
}
public static void showTransactions(int[] trans, int balance) {
System.out.println();
System.out.println("Previous 10 transactions");
System.out.println();
for (int i = 0; i < trans.length; i++) {
if(trans[i] != 0)
{
System.out.print(trans[i] + "\n");
}
else
{
System.out.print(trans[i] + "\n");
balance = balance + trans[i];
}
}
System.out.println();
System.out.println("Balance: " + balance + " kr" + "\n");
System.out.println();
}
public static void makeTransactions(int[] trans, int amount) {
int position = findNr(trans);
if(position == -1)
{
moveTrans(trans);
position = findNr(trans);
trans[position] = amount;
}
else {
trans[position] = amount;
}
}
public static int findNr(int[] trans) {
int position = -1;
for (int i = 0; i < trans.length - 1; i++) {
if (trans[i] == 0) {
position = i;
break;
}
}
return position;
}
public static void moveTrans(int[] trans) {
for (int i = 0; i < trans.length; i++)
trans[i - 1] = trans[i];
}
}
重現此問題的最簡單輸入是什麼?當你在調試器中遍歷代碼時,你看到了什麼? –