[英]<identifier> expected when creating an array?
I am writing an airline program that will allow the user to input names and meal choice for each seating section economy, business, and first. 我正在编写一个航空公司程序,该程序将允许用户输入经济舱,商务舱和头等舱座位区的名称和餐食选择。 I am trying to save all the names and meals into an array. 我正在尝试将所有名称和食物保存到一个数组中。 but I am getting a syntax error. 但我收到语法错误。
I get expected message when I implement my flyer array. 实现我的Flyer数组时,我得到了预期的消息。
I have looked on stack overflow. 我看过堆栈溢出。 From what I can tell it should be ok to initialize my array this way. 据我所知,以这种方式初始化数组应该可以。
Thanks for any help. 谢谢你的帮助。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Flyers
{
public Flyers()
{
}
public List<String> seat = new ArrayList<>();
int numberOfFlyers;
int numberOfMeals;
String name;
String meal;
String[][] flyer;
public void addEconomyFlyer()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of economy seats sold: ");
numberOfFlyers = in.nextInt();
flyer = new [numberOfFlyers][numberOfFlyers];
}
}
I want each [][] to have the same number of items that is equal to the number of people on the plane. 我希望每个[] []的项目数等于飞机上的人数。 Then I will add a nested for loop that will add a name for each of the flyers, and a meal choice. 然后,我将添加一个嵌套的for循环,该循环将为每个传单添加一个名称,并选择用餐。 ultimately i need to be able to print out the array Name and their Meal choice. 最终,我需要能够打印出阵列名称及其餐食选择。
Almost, the issue here is you haven't specified a type for the array. 几乎,这里的问题是您尚未为数组指定类型。
flyer = new [numberOfFlyers][numberOfFlyers];
should probably be 应该是
flyer = new String[numberOfFlyers][numberOfFlyers];
However that doesn't make a lot sense. 但是,这没有多大意义。 One solution is to use, 一种解决方法是使用
flyer = new String[numberOfFlyers][2];
where 0 is name and 1 is meal. 其中0是名称,1是餐。 But, really you should probably have a flyer POJO, 但是,实际上您可能应该有一个传单POJO,
flyer = new Flyer[numberOfFlyers];
Where Flyer
might look something like, 在Flyer
可能看起来像的地方,
class Flyer {
Flyer(String name, String mealType) {
this.name = name;
this.mealType = mealType;
}
String name;
String mealType;
public String toString() {
return "Name: " + name + ", Meal: " + mealType;
}
}
Then you can create new Flyer(s) and call toString()
in your loop. 然后,您可以创建新的Flyer,并在循环中调用toString()
。 You might also choose to add getter and setter functions for name
and mealType
. 您可能还选择为name
和mealType
添加getter和setter函数。
change : 变化:
flyer = new [numberOfFlyers][numberOfFlyers];
to: 至:
flyer = new String[numberOfFlyers][numberOfFlyers];
Scanner class 扫描仪类
Scan.scanner.scan
You need scan it!!!! 您需要扫描!!!!
identifier = here
This trickes computer in think identifier there! 这欺骗了计算机在那里认为标识符! YES! 是! WORK! 工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.