【发布时间】:2013-11-26 12:10:16
【问题描述】:
这个程序是我们在音乐商店中可以找到的简化程序。我们输入我们想要的命令,它会执行该命令应该做的事情。这些是选项。
创建“客户”|“音乐”|“销售”
列出“客户”|“音乐”|“销售”
擦除“客户”|“音乐”|“销售”(使用或不使用生成的代码当我们创建新语句时)
程序必须执行,直到我们输入“close”。
我在这里发布我到目前为止所做的事情。
主要问题是,如果我们输入了错误的命令或输入了此处不存在的内容,如何抛出异常。现在,如果发生这种情况,它就会回到开始。如果您就您认为可能对程序更好的任何更改或修改给我建议,我也会很感激,我只是从这个开始,我知道这可以以更好的方式完成:
public class Main {
static GestionarMusica musiclist= new GestionarMusica(20);
static GestionarCliente clientlist= new GestionarCliente(20);
static Scanner input= new Scanner(System.in);
static String instructions;
public static void main (String[] args){
do{
try{
System.out.println("Waiting for instructions: ");
instructions= input.nextLine();
switch (instructions){
case "create client":
createClient();
break;
case "create music":
createMusic();
break;
case "create selling":
//createSelling();
break;
case "list client":
listClient();
break;
case "list music":
listMusic();
break;
case "list selling":
//listSelling();
break;
}
}catch (NullPointerException npe){
System.out.println("There are not articles on the list");
}
if (instructions.equals("erase client")){
eraseClientWithoutCode();
}
if (instructions.length() <= 18 && instructions.length() >= 17 && instructions.substring(0, 16).equals("erase client")){
String client_code = instructions.substring(16);
client_code = client_code.trim();
int code = Integer.parseInt(client_code);
eraseClientWithCode(code);
}
if (instruction.equals("erase music")){
eraseMusicWithoutCode();
}
if (instructions.length() <= 17 && instructions.length() >= 16 && instructions.substring(0, 15).equals("erase music")){
String music_code = instructions.substring(15);
music_code = music_code.trim();
int code = Integer.parseInt(music_code);
eraseMusicWithCode(code);
}
if (instructions.equals("erase selling")){
eraseSellingWithoutCode();
}
if (instructions.length() <= 16 && instructions.length() >= 15 && instructions.substring(0, 14).equals("erase selling")){
String selling_code = instructions.substring(14);
selling_code = selling_code.trim();
int code = Integer.parseInt(selling_code);
eraseSellingWithCode(code);
}
}while(!instructions.equals("close"));
}
public static void createMusic() {
System.out.println("Insert album title: ");
String title = teclado.nextLine();
System.out.println("Insert album autor: ");
String autor = teclado.nextLine();
System.out.println("Insert format: ");
String format = input.nextLine();
musiclist.add(new Music(title, autor, format, musiclist.generateCode()));
}
public static void listMusic() {
System.out.println(musiclist.toString());
}
public static void eraseMusicWithCode(int code) {
musiclist.delete(code);
System.out.println("Article deleted");
}
public static void eraseMusicWithoutCode() {
System.out.println("Insert article code: ");
int code = input.nextInt();
musiclist.delete(code);
System.out.println("Article deleted");
}
UPDATE--关于switch语句中default的使用
因为这个原因,我不能使用默认值。在开关里面我有 create 和 list 命令,但我必须在女巫之外设置 erase 命令,因为这些取决于我是否输入带有代码的命令,或者没有它。所以,如果我输入一个擦除命令,并且如果我在开关中有默认值,它将显示异常。
【问题讨论】:
-
在switch中,使用默认:\\抛出异常。
-
要在 switch 语句中抛出异常,您将抛出异常。唯一令人意外的是,如果 case 只抛出异常——没有
if逻辑——那么如果你使用它,break将会死掉并且编译器会抱怨。所以跳过break。 -
发布关于使用默认命令的更新
标签: java exception switch-statement