import javax.swing.*;
public class Menu2 {
protected String[] entreeChoice = {"Rosemary Chicken", "Beef Wellington", "Maine Lobster"};
private String menu = "";
private int choice;
protected char initial[] = new char[entreeChoice.length];
public String displayMenu(){
for(int x = 0; x < entreeChoice.length; ++x){
menu = menu + "\n" + (x+1) + "for" + entreeChoice[x];
initial[x] = entreeChoice[x].charAt(0);
}
throws menuException
String input = JOptionPane.showInputDialog(null, "Type your selection, then press Enter." + menu);
choice = Integer.parseInt(input);
return (entreeChoice[choice - 1]);
}
}
我在throws menuException
上遇到错误。它说:非法启动类型。
我差不多完成了代码,只是代码需要修改(照片附加),当我这样做时,我得到了代码放置位置的错误。
答案 0 :(得分:2)
取决于您想要做什么(抛出异常,或声明您的方法可能抛出该类型的异常):
要么改变它:
public String displayMenu() throws menuException {
for(int x = 0; x < entreeChoice.length; ++x){
menu = menu + "\n" + (x+1) + "for" + entreeChoice[x];
initial[x] = entreeChoice[x].charAt(0);
}
...
}
或者:
public String displayMenu(){
for(int x = 0; x < entreeChoice.length; ++x){
menu = menu + "\n" + (x+1) + "for" + entreeChoice[x];
initial[x] = entreeChoice[x].charAt(0);
}
if (someCondition)
throw new menuException();
...
}
答案 1 :(得分:2)
throws menuException
应该......
Exception
类(不是已经存在的实例)...... 例如......
public String displayMenu() throws MenuException {
for(int x = 0; x < entreeChoice.length; ++x){
menu = menu + "\n" + (x+1) + "for" + entreeChoice[x];
initial[x] = entreeChoice[x].charAt(0);
}
//throws menuException
String input = JOptionPane.showInputDialog(null, "Type your selection, then press Enter." + menu);
choice = Integer.parseInt(input);
return (entreeChoice[choice - 1]);
}
您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码