简单切换“for”循环和toLowerCase()方法

时间:2015-04-29 17:05:55

标签: java

我是Java的初学者。我正在做一个简单的开关,用户在其中输入一个单词。为了训练,我添加了一个“for”循环。

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()”方法。

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

切换声明

在你的switch子句中,你正在测试一个已经变成小写形式的zkServer.sh start-foreground 。因此,唯一可能匹配的case子句是“one”,因为其他都包含大写字符。

将所有case子句更改为小写,只要您正确输入数据,它就会起作用。

String

不接受输入

关于程序不接受输入的问题,您必须在致电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(),有关原因,请参阅here

只需要一台扫描仪。

答案 2 :(得分:1)

switch语句的约定类似于if-else if-else链。将根据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")){

永远不会评估为真,因为在执行区分大小写与“tHREE”的比较之前,“tHREE”第一次被删除为全部小写“3”。

答案 3 :(得分:1)

  

为什么第一个循环会生成默认代码?

正如Skipping nextLine() after use next(), nextInt() or other nextFoo() methods中所述,nextInt不会使用换行符。设置sck.nextLine();后,请致电counter

  

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

不,只需要一个Scanner

  

为什么计数器和单词中的数字不能正常工作?

首先,您的for循环条件为i <= counter,这意味着它已设置为运行counter + 1次。将其更改为i < counter

其次,除switch之外的"one"个案包含大写字母,因此它们永远不会匹配ch.toLowerCase()。将案例标签更改为全部小写字母的字符串。

第三,在counter循环结束时递减for,减少运行的迭代次数。而是使用表达式(counter - i - 1)输出剩余的循环数。

答案 4 :(得分: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;