gpt4 book ai didi

java - switch case 中的断言消息不显示

转载 作者:行者123 更新时间:2023-12-01 11:06:22 25 4
gpt4 key购买 nike

不显示 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 循环再次执行,我认为这不是您想要做的,而是在错误的输入下再次运行第二个循环

关于java - switch case 中的断言消息不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905208/

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