首先我對標題感到抱歉,我想不出一個更好的方式來表達它。實際的錯誤在選項3中(每當我嘗試將選項1中的所有銷售加在一起)。當我嘗試使用salesList.length
來跟蹤數組的大小時,我得到了cannot find symbol- variable length
我很新使用數組列表,並且該方法在較早的數組中工作,但該數組不是動態的。有沒有一種特定的方式來跟蹤動態數組列表的長度?。嘗試對ArrayList使用for循環時出現.length錯誤
import java.util.*;
public class CustomerTest
{
public static void main(String[] args)
{
double totalSales = 0;
ArrayList<String> nameList;
nameList = new ArrayList<String>();
ArrayList<Double> salesList;
salesList = new ArrayList<Double>();
Scanner myScanner = new Scanner(System.in);
boolean done = true;
do
{
System.out.println("1) Add a new customer \n 2) Print all customers \n 3) Compute and print the total sales \n 4) Quit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Add a new customer ");
String answer = myScanner.nextLine();
nameList.add(answer);
System.out.print("Enter their sales ");
String answer2 = myScanner.nextLine();
double answer3 = Double.parseDouble(answer2);
salesList.add(answer3);
}
else if(choice == 2)
{
System.out.println("Customers: " + nameList);
System.out.println("Sales: " + salesList);
}
else if(choice == 3)
{
for(int i = 0; i < salesList.length; i++)
{
totalSales = totalSales + salesList[i];
}
System.out.println(totalSales);
}
else if(choice == 4)
{
System.out.println("Goodbye *Bows gracefully*");
done = false;
}
else
System.out.println("Invalid Choice");
}
while (done);
System.exit(0);
}
}
@ user3451158肯定。 – Azar