Я пытаюсь создать банкомат, способный внести депозит, вывод и показать баланс, но моя проблема возникает, когда я пытаюсь сделать свою 11-ю транзакцию (размер моих транзакционных записей равен 10).Ошибка Java ATM arrays
Вот как программа должна работать:
Earlier transactions:
=====================
1
2
3
4
5
6
7
8
9
10
=======================
Balance: 55 KR
Earlier transactions:
=====================
2
3
4
5
6
7
8
9
10
11
=======================
Balance: 65 KR
я должен использовать эти методы и не полностью переведены на программу на английском языке.
import java.util.Scanner;
public class Bankomat
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Declarer variables
int[] trans = new int[10];
int amount = 0;
int balance = 0;
int sum = 0;
int theChoice = 1;
while(theChoice != 4)
{
theChoice= menu();
switch(theChoice)
{
case 1:
System.out.println("\nDu valde \"deposit\"");
System.out.print("\nState the value you want to take in: ");
sum = in.nextInt();
if(sum == 0)
{
System.out.print("\nYou have given are wrong value.\n");
}
else
{
amount = (int) + sum;
makeTransactions(trans,amount);
}
break;
case 2:
System.out.println("\nDu valde \"withdrawal\"");
System.out.print("\nState the value you want to take in: ");
sum = in.nextInt();
if(sum == 0)
{
System.out.print("\nDu har angett ett felaktigt belopp.\n");
}
else
{
amount = (int) - sum;
makeTransactions(trans,amount);
}
break;
case 3:
System.out.println("\nDu valde \"Balance\"");
showTransactions(trans,balance);
break;
case 4:
System.out.println("\nDu valde \"Quit\"");
break;
}
}
}
/**
* MENU
* @return val skickar tillbaka input värdet
*/
public static int menu()
{
Scanner in = new Scanner(System.in);
int choice = 0;
// Den här delen kommer att skriva ut menu
System.out.println("1. deposit");
System.out.println("2. withdrawal");
System.out.println("3. Balance");
System.out.println("4. Quit");
System.out.print("Your choice: ");
choice = in.nextInt();
return choice;
}
/**
* This method will sum up all the ten latest transaction and show the balance
* @param trans array that saves the latest transactions
* @param balance Int that sums up all the values
*/
public static void showTransactions(int[] trans, int balance)
{
System.out.println();
System.out.println("Tidigare transaktioner: ");
System.out.println("-------------------------------------------\n");
for(int i = 0; i < trans.length; i++)
{
if(trans[i] == 0)
{
System.out.print("");
}
else
{
System.out.print(trans[i] + "\n");
balance = balance + trans[i];
}
}
System.out.println("-------------------------------------------\n");
System.out.println("Saldo: " + balance + "KR" + "\n");
}
/**
* This method saves the latest transaction
* @param trans array that saves the latest transactions
* @param amount int that saves the latest transaction
*/
public static void makeTransactions(int[] trans, int amount)
{
int position = findNr(trans);
if(position == -1)
{
moveTrans(trans);
}
else
{
trans[position] = amount;
}
}
/**
* This metod will look for a empty position
* @param trans array that saves the latest transactions
* @return position
*/
private static int findNr(int[] trans)
{
int position = -1;
for(int i = 0; i < trans.length; i++)
{
if (trans[i] == 0)
{
position = i;
break;
}
}
return position;
}
/**
* This method will move the transaction
* @param trans array that saves the latest transactions
*/
private static void moveTrans(int[] trans)
{
for(int i = 0; i < trans.length; i++)
{
trans[0] = trans[i + 1];
}
}
}
EDIT:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at stackOverflow.Testing.Bankomat.moveTrans(Bankomat.java:171)
at stackOverflow.Testing.Bankomat.makeTransactions(Bankomat.java:135)
at stackOverflow.Testing.Bankomat.main(Bankomat.java:41)
Линия 171:
private static void moveTrans(int[] trans)
{
for(int i = 0; i < trans.length; i++)
{
trans[0] = trans[i + 1]; // This is line 171
}
}
Что ваша ошибка? Можете ли вы быть более конкретным? 'ArrayIndexOutOfBoundsException'? – Gosu
Так что это ** ArrayIndexOutOfBoundsException ** внутри 'moveTrans()' .. – Gosu