我已经制作了自动取款机(ATM)程序,但我无法弄清楚出了什么问题。除了交易是一行零之外,一切正常。我一定搞砸了......我认为用“showTransactions”方法是这样的。谢谢你的帮助!
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];
}
}
答案 0 :(得分:1)
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;
在上面的代码中,当您调用makeTransactions
方法时,您将amount
作为参数传递,您永远不会更改,即它始终为0.我认为您应该传入sum
因为那是你存储用户输入值的地方。
答案 1 :(得分:1)
你的逻辑是说 IF trans [i] EQUALS 0然后打印为什么?也许改变
if(trans[i] == 0)
到
if(trans[i] != 0)
或者为什么不打印它们,无论如何它们都是交易的全部内容。