2015-04-29 21 views
-1

我非常喜欢Java中的初学者。我正在做一个简单的开关,用户在其中输入一个数字。为了训练,我添加了一个“for”循环。“for”循环和toLowerCase()方法中的简单开关

package JavaExc; 
import java.util.Scanner; 

public class JavaStart { 

public static void main(String[] args) { 
    Scanner sck = new Scanner(System.in); 
    System.out.println("Type loop counter "); 

    int counter = sck.nextInt(); 

    System.out.println("Counter is: " + counter); 


    for (int i = 0; i <= counter; i++) { 


     System.out.println("Type number in words"); 
     String ch = sck.nextLine(); 

     switch (ch.toLowerCase()) { 


      case "one": 
       System.out.println("CHOICE 1"); 
       break; 
      case "Two": 
       System.out.println("CHOICE 2"); 
       break; 
      case "THREE": 
       System.out.println("CHOICE 3"); 
       break; 
      case "FoUr": 
       System.out.println("CHOICE 4"); 
       break; 
      case "fiVE": 
       System.out.println("CHOICE 5"); 
       break; 

      default: 
       System.out.println("Wrong Data"); 
       break; 

     } 

     System.out.println("Loops left:\n " + counter--); 
    } 
    System.out.println("End of Switch"); 


}} 

下面是结果:

Type loop counter 
5 
Counter is: 5 
Type number in words 
Wrong Data     // ?? 
Loops left: 
5 
Type number in words 
one 
CHOICE 1 
Loops left: 
4 
Type number in words 
three      // Should be ok, so what is wrong here? 
Wrong Data 
Loops left: 
3 
End of Switch    //To early break loop I think 

Process finished with exit code 0 

我的问题是:为什么第一循环设为默认代码?我应该制作2台扫描仪吗?一个用于计数器,另一个用于开关? 为什么计数器和单词中的数字无法正常工作?

我知道我可以用表等来做,但一般来说,这个程序的目的是测试“toLowerCase()”方法。

+0

ch的价值是多少? –

+1

如果您将ch转换为小写,您将永远不会看到大写字母的情况。“THREE”,“FoUr”和“fiVE”的要点是什么? – CubeJockey

+0

“三”和“三”不一样。 Java是区分大小写的 – Stultuske

回答

1

你正在用大写字母(“三”)检查你的字符串小写(“三”)。确保它们都是相同的,它不应该失败。

0

重新输入病例:

  case "one": 
       System.out.println("CHOICE 1"); 
       break; 
      case "two": 
       System.out.println("CHOICE 2"); 
       break; 
      case "three": 
       System.out.println("CHOICE 3"); 
       break; 
      case "four": 
       System.out.println("CHOICE 4"); 
       break; 
      case "five": 
       System.out.println("CHOICE 5"); 
       break; 
1

switch语句

在你开关子句中,您将测试已变成其自身的小写形式的String。因此,唯一可能匹配的case子句是“one”,因为其他匹配项都包含大写字符。

只要你输入正确的数据,将所有的case子句改为小写字母,它就会工作。

case "one": 
    System.out.println("CHOICE 1"); 
    break; 
case "two": 
    System.out.println("CHOICE 2"); 
    break; 
case "three": 
    System.out.println("CHOICE 3"); 
    break; 
case "four": 
    System.out.println("CHOICE 4"); 
    break; 
case "five": 
    System.out.println("CHOICE 5"); 
    break; 

不接受输入

关于你提到的问题,该程序不接受输入,你必须调用sck.nextLine()调用sck.nextInt()后,看到here关于为什么这是细节。

只需要一个扫描仪。

1

switch语句的约定类似于if-else if-else链。 switch语句的括号之间的值将针对switch语句中存在的每个情况进行评估。

的switch语句:

String condition = "foo"; 

switch(condition){ 
    case "example":{ 
    } 

    case "foo":{ 
    } 

    case "bar":{ 
    } 

    default:{ 
    } 
} 

类似表达:

if(condition.equals("example")){ 

}else if(condition.equals("foo")){ 

}else if(condition.equals("bar")){ 

}else{ 

} 

注意,通过使您的输入到switch语句小写的,它永远不会匹配任何情况下是大写。

e.g:

if("tHREE".toLowerCase().equals("tHREE")){ 

永远不会评估正确的,因为“三”是第一被丢弃到所有小写的“三化”进行区分大小写比较,“三”之前。

+0

好的。我明白。我想要转换字符串,无论它是大写还是小写,并尝试与案例匹配。 – Buckethead

+0

@ m4rtin77继续使用toLowerCase()方法,但要确保代码中的每个案例都是小写。在这种情况下,最初混合大小写的字符串将被转换为小写字母,然后再与您提供的其中一种情况进行匹配。 – initramfs

1

为什么第一个循环创建默认代码?

Skipping nextLine() after use next(), nextInt() or other nextFoo() methods,nextInt中所解释的,不消耗换行符。设置counter后,请拨打sck.nextLine();

我应该制造2台扫描仪吗?一个用于计数器,另一个用于开关?

不,只有一个Scanner是必要的。

为什么柜台中的计数器和数字无法正常工作?

首先,你for循环条件为i <= counter,这意味着它的成立运行counter + 1倍。将其更改为i < counter

二,您的switch个案除了"one"包含大写字母,所以他们永远不会匹配ch.toLowerCase()。将您的案例标签更改为全部使用小写字母的字符串。

第三,您在for循环结束时递减counter,减少了运行的迭代次数。相反,使用表达式(counter - i - 1)输出左侧的循环数。

+0

好吧,我明白了。非常感谢解释。 – Buckethead

0

好吧,我添加了更改,它工作正常。现在我将尽力使输入的文字成为可能,无论它们是大写还是小写。感谢帮助! StackOverflow非常棒!

package JavaExc; 
import java.util.Scanner; 

public class JavaStart { 

public static void main(String[] args) { 
    Scanner sck = new Scanner(System.in); 
    System.out.println("Type loop counter "); 

    int counter = sck.nextInt(); 
    sck.nextLine(); 
    System.out.println("Counter is: " + counter); 

    int i; 
    for (i = 0; i < counter; i++) { 


     System.out.println("Type number in words"); 
     String ch = sck.nextLine(); 

     switch (ch.toLowerCase()) { 


      case "one": 
       System.out.println("CHOICE 1"); 
       break; 
      case "two": 
       System.out.println("CHOICE 2"); 
       break; 
      case "three": 
       System.out.println("CHOICE 3"); 
       break; 
      case "four": 
       System.out.println("CHOICE 4"); 
       break; 
      case "five": 
       System.out.println("CHOICE 5"); 
       break; 

      default: 
       System.out.println("Wrong Data"); 
       break; 

     } 

     System.out.println("Loops left:\n " + (counter - i - 1) ); 

    } 
    System.out.println("End of Switch"); 


}}