Scanner.nextline()在交换机中 [英] Scanner.nextline() in a switch

查看:97
本文介绍了Scanner.nextline()在交换机中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于另一周我班上的作业,我提交了一段代码,看起来类似于下面的代码。

当我的教授对它进行评分时,他告诉我以下事项: 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 ;
}
}

解决方案

问题是一些编译器和解释器如何使用Nextline生成可执行文件( )作为程序调用。如果我们将这一点表达为一系列的IF语句,那么更清楚的是什么可能出错。



  if  input.newline()=   test 1
系统。 out .println( on right< /跨度>);
break ;
if input.newline()= 测试2
系统。 out .println( 再试一次);
break ;
if input.newline()= test
System。 out .println( )?;
break ;





此时我们可以看到,在最坏的情况下,input.newline()将分别执行3次获得新价值的时间。 input.newline()是一种破坏性的方法,每次调用它时都会返回一个不同的流段。



我不能说这是如何用Java实现的,但是作为形式问题,最好将换行结果分配给临时变量并根据该变量验证输入。但是,正如之前的海报所说的那样切换字符串并不是很好的形式,因为通常有更好的方法。


原来教授在一台小于Java 7的机器上测试了程序在上面。这就是它失败如此剧烈的原因。


感谢所有提出建议的人!


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:

Quote:

You can't put an input.nextLine() method in a switch statement. Instead, you have to create a char variable first and store the nextLine() output into that variable. Then you can check the variable for the conditions you indicate in your case statements. You can't put strings in a switch statement.

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;
    }
   }

解决方案

The problem is how some compilers and interpreters generate the executable here with the Nextline() as a procedure call. If we express this to a series of IF statements then it is clearer what can go wrong.

if input.newline() = "test 1":
   System.out.println("right on");
   break;
if input.newline() =  "test 2":
   System.out.println("try again");
   break;
if input.newline() = "test":
   System.out.println("and?");
   break;



At this point we can see that in the worst case the input.newline() will be executed 3 times each time obtaining a new value. input.newline() is a destructive method which returns a different segment of a stream each time it is called.

I cannot say how this is implemented in Java, but as a matter of form it is better to assign the result of newline to a temp variable and validate the input against that. But as the previous poster stated switches on strings this way is not great form as there is commonly a better way.


Turns out that the Professor tested the program on a machine with less than Java 7 on it. That was why it failed so dramatically.

Thanks to everyone who made suggestions!


这篇关于Scanner.nextline()在交换机中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆