开关盒中的断言消息不显示
Assertion message in switch case do not display
不显示 switch case 中的断言消息。
每当用户输入 < 0 或 >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);
}
}
从
中删除back++
default:
assert false : "Option is out of range. Please try again.\n";
back++;
break;
当用户输入错误的值时,您会跳出循环,因为此时 back == 1
并且您的 do-while
循环结束并且您的第一个 do-while
循环再次执行,并且我不认为那是你想要做的,而是 运行 在错误的输入下再次循环
不显示 switch case 中的断言消息。
每当用户输入 < 0 或 >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);
}
}
从
中删除back++
default:
assert false : "Option is out of range. Please try again.\n";
back++;
break;
当用户输入错误的值时,您会跳出循环,因为此时 back == 1
并且您的 do-while
循环结束并且您的第一个 do-while
循环再次执行,并且我不认为那是你想要做的,而是 运行 在错误的输入下再次循环