本文介绍了Scanner.nextline()在交换机中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于另一周我班上的作业,我提交了一段代码,看起来类似于下面的代码。 当我的教授对它进行评分时,他告诉我以下事项: blockquote class =quote> Quote:你不能在switch语句中放入input.nextLine()方法。相反,您必须首先创建一个char变量并将nextLine()输出存储到该变量中。然后,您可以检查变量以了解您在案例陈述中指明的条件。你不能把字符串放在switch语句中。 然而,我无法理解它有什么问题。该程序正常运行, Java文档似乎说switch语句支持字符串。 有人可以告诉我我缺少什么吗? 谢谢!扫描仪输入= new Scanner(System.in); switch (input.nextLine()) { case test 1: System.out.println( 右上); break ; case test 2: System.out.println( 再试一次); break ; case test : System.out.println( and?); break ; 默认: break ; } } 解决方案 For an assignment in my class the other week, I turned in a piece of code that looked similar to the code below.When my professor graded it, he told me the following things: I can't understand, however, what's wrong with it. The program functions correctly and the Java Documentation seems to say that the switch statement supports strings.Can someone please tell me what I'm missing?Thanks!Scanner input = new Scanner(System.in); switch(input.nextLine()) { case "test 1": System.out.println("right on"); break; case "test 2": System.out.println("try again"); break; case "test": System.out.println("and?"); break; default: break; } } 解决方案 这篇关于Scanner.nextline()在交换机中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 22:46