【发布时间】:2015-10-02 10:26:15
【问题描述】:
不显示 switch case 中的断言消息。
每当用户输入 6 时,它都应该输出给定的错误消息。
我的断言在默认部分,消息不会显示。我的代码有什么问题?
import java.util.*;
public class DeckOfCards extends Inputs{
//objects
//sets
//int
public DeckOfCards(){
//some initialization
}
public void setValtyp(String val){
this.valtyp = valtyp;
}
public String getValtyp(){
return this.valtyp;
}
public static void main(String args[]){
int exit = 0;
do{
try{
Scanner input = new Scanner(System.in);
int option;
int option2;
String yesno;
int back = 0;
String val;
String typ;
System.out.println("Deck of Cards");
System.out.println("[1]Add");
System.out.println("[2]Delete");
System.out.println("[3]Display");
System.out.println("[4]Search");
System.out.println("[5]Count");
System.out.println("[6]Exit");
System.out.print("Option: ");
option = input.nextInt();
switch(option){
case 1:
//codes
break;
case 2:
//codes
break;
case 3:
//codes
break;
case 4:
//codes
break;
case 5:
do{
try{
System.out.println("\nDeck of Cards");
System.out.println("[1]Heart");
System.out.println("[2]Diamond");
System.out.println("[3]Spade");
System.out.println("[4]Clover");
System.out.println("[5]All");
System.out.println("[6]Back");
System.out.print("Option: ");
option2 = input.nextInt();
switch(option2){
case 1:
System.out.println("\nHearts: "+kerds.heart.size());
break;
case 2:
System.out.println("\nDiamond: "+kerds.diamond.size());
break;
case 3:
System.out.println("\nSpade: "+kerds.spade.size());
break;
case 4:
System.out.println("\nClover: "+kerds.clover.size());
break;
case 5:
int totalsize;
totalsize = kerds.heart.size() + kerds.diamond.size() + kerds.spade.size() + kerds.clover.size();
System.out.println("\nAll: "+totalsize);
break;
case 6:
System.out.println("");
back++;
break;
default:
assert false : "Option is out of range. Please try again.\n";
back++;
break;
}
}
catch(InputMismatchException e){
System.out.println("Invalid Option! Please try again.\n");
back++;
}
}while(back!=1);
break;
case 6:
exit++;
break;
default:
assert false : "Option is out of range. Please try again.\n";
}
}
catch(InputMismatchException e){
System.out.println("Invalid Option! Please try again.\n");
}
}while(exit!=1);
}
}
【问题讨论】:
-
您是否启用了断言?
java -ea -
如何启用断言?
-
同上,使用
-ea参数。如果您使用的是 Eclipse,请转到 Run Configurations,选择运行配置、Arguments 选项卡,然后将-ea放入 VM Arguments 框中。 -
我在网上搜索了如何做到这一点,它的工作原理!谢谢!
标签: java switch-statement assertion