在测试用例的以下代码中,当我从case内调用scrollDown()方法时,还将执行默认case。当我不调用scrollDown方法时,一切运行正常。你能帮忙吗?
导入java.util。*;
public class Front
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String command;
boolean run = true;
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nWelcome. Type help for a list of commands.");
while(run == true)
{
System.out.print("Enter a command: ");
command = kb.nextLine();
command = command.toLowerCase();
switch(command)
{
case "help":
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nThis is the help menu.");
System.out.println("Commands are \n help \n exit \n test \n");
break;
case "exit":
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nThank you. Goodbye.");
run = false;
break;
case "test":
System.out.print("Enter an interger for the test: ");
scrollDown(kb.nextInt());/* when I call this method the default gets executed also. When I don't call this method it works fine*/
System.out.println("Why?????");
break;
default:
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nNot a recognized command. Type help for list of commands or exit to quit.");
break;
}
}
}
public static void scrollDown(int num)
{
for (int count = 1; count <= num; count++) /* counts to the number you enter on the test case option */
System.out.println(count);
}
}