【发布时间】:2020-04-13 15:06:20
【问题描述】:
所以,我有了printStats这个方法,它的参数pList的数据类型是Passenger的ArrayList:
public static void printStats(ArrayList<Passenger> pList) {
Scanner file = new Scanner( new File( "titanic.csv" ) );
DecimalFormat percent = new DecimalFormat("#.0%");
String header = file.nextLine();
while (file.hasNextLine()){
String gender;
String survived;
int count = 0;
int males = 0;
int females = 0;
int survivedMales = 0;
int survivedFemales = 0;
for(int i=0; i<pList.length;i++) {
if (gender.equals ("M"))
{ males++;
if (survived.equals("yes"))
survivedMales ++;}
else
{
females++;
}
if (survived.equals("yes"))
survivedFemales ++;}
count++
System.out.println("Total number of passengers: " + count);
System.out.println("Total number of male passengers: " + males);
System.out.println("Total number of female passengers: " + females);
System.out.println("Total number of male passengers survived: " + survivedMales);
System.out.println("Total number of female passengers survived: " + survivedFemales);
System.out.println("Survival rate of male passengers: " + percent.format(survivedMales/males));
System.out.println("Survival rate of female passengers: " + percent.format(survivedFemales/females));
}
我一直在 for 循环中出现语法错误,特别是在“i
for(int i=0; i<pList.length;i++)
任何帮助将不胜感激。
【问题讨论】:
-
你用什么样的编辑器来写Java?大多数编辑器都有一个快捷方式来为您对齐代码,以便更轻松地查看您在哪里犯了错误。
-
我在 Eclipse 中,它突出显示了 pList.length 中的“长度”,但我仍然不确定如何修复它
-
Eclipse 中的自动格式化: CTRL + SHIFT + F 这将使您下次更容易看到错误。我在下面支持 Code-Apprentice 的答案,因为他回答了您的实际问题。
标签: java arrays list loops for-loop