gpt4 book ai didi

java - 这段代码似乎编译不干净

转载 作者:行者123 更新时间:2023-12-02 11:14:09 25 4
gpt4 key购买 nike

我正在为一家餐馆用 Java 创建一个程序。我正在使用 ArrayList但由于某种原因,我的入门类(class)似乎没有在主菜单中运行。

这是我的入门类(class):

import java.util.ArrayList;

public class Starter
{
Starter()
{
String[] myList = {"Coffee", "Tea", "Somosas", "Cake"};
//System.out.println(myList[]);
}
}

这似乎是正确的,但是当我尝试从主菜单中进行选择时,它似乎不起作用。

主菜单:
import java.util.Scanner; 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Menu
{
static Scanner input = new Scanner(System.in);

public static void main(String[]args)
{
System.out.println("1=Starter");
System.out.println("2= Main Course");
System.out.println("3=Desert");

int a =input.nextInt();
input.nextLine();

if(a==1)
{
System.out.println("Starter");
Starter OS1=new Starter();
System.out.println("Your starter is "+OS1.myList[]);

}
else if(a==2)
{

System.out.println("Main Course");
MaiinCourse OMC1=new MaiinCourse();
System.out.println("Your MainCourse is "+OMC1.MCname);
System.out.println("The price is "+OMC1.MCprice);
}

else if(a==3)
{
System.out.println("Desert");
Deserrt ODS1=new Deserrt();
System.out.println("Your Desert is "+ODS1.DSname);
System.out.println("The price is "+ODS1.DSprice);
}
else
{
System.out.println("End");
System.out.println("Program Closing");

System.exit(1);

}
}
}

我得到的错误是:
'.class' expected System.out.println("Your starter is "+OS1.myList[]);

如何解决这个问题?

当我运行主菜单时,它应该允许我从数组列表中进行选择。

最佳答案

我对您的代码做了一些更改。现在它起作用了。试试看。

import java.util.Arrays;
import java.util.Scanner;

public class Menu
{
static Scanner input = new Scanner(System.in);

public static void main(String[]args)
{
System.out.println("1=Starter");
System.out.println("2= Main Course");
System.out.println("3=Desert");

int a = input.nextInt();
input.nextLine();

if (a == 1)
{
System.out.println("Starter");
Starter OS1 = new Starter();
System.out.println("Your starter is " + Arrays.toString(OS1.getMyList()));

}
}
}

class Starter
{
private String[] myList = {"Coffee", "Tea", "Somosas", "Cake"};

public String[] getMyList()
{
return myList;
}
}

关于java - 这段代码似乎编译不干净,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com