如何使用Java switch case

时间:2016-09-10 19:51:03

标签: java android switch-statement

我制作了一个简单的开关盒(之前从未使用过......所以请耐心等待。)

        select_diff.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
            rating = (int)(select_diff.getRating() * 10);
            Toast.makeText(timestables.this, Integer.toString(rating) , Toast.LENGTH_SHORT).show();

            switch (rating) {

                case 10:
                    questions.setText("10 Questions.");
                    time.setText("10 Seconds For Each Question.");
                    timestables.setText("2 to 5 Times Tables");

                case 15:
                    questions.setText("13 Questions.");
                    time.setText("8 Seconds For Each Question.");
                    timestables.setText("2 to 6 Times Tables");

                case 20:
                    questions.setText("15 Questions.");
                    time.setText("7 Seconds For Each Question.");
                    timestables.setText("2 to 7 Times Tables");

                case 25:
                    questions.setText("20 Questions.");
                    time.setText("6 Seconds For Each Question.");
                    timestables.setText("2 to 8 Times Tables");

                case 30:
                    questions.setText("25 Questions.");
                    time.setText("5 Seconds For Each Question.");
                    timestables.setText("2 to 9 Times Tables");

                case 35:
                    questions.setText("30 Questions.");
                    time.setText("4 Seconds For Each Question.");
                    timestables.setText("2 to 10 Times Tables");

                case 40:
                    questions.setText("35 Questions.");
                    time.setText("3 Seconds For Each Question.");
                    timestables.setText("2 to 11 Times Tables");

                case 45:
                    questions.setText("40 Questions.");
                    time.setText("2.5 Seconds For Each Question.");
                    timestables.setText("2 to 12 Times Tables");

                case 50:
                    questions.setText("40 Questions.");
                    time.setText("2 Seconds For Each Question.");
                    timestables.setText("2 to 12 Times Tables");

            }
        }
    });

变量"评级"属于INT类型。来自android的Toast总是会显示正确的大小写值(即当我选择2个星星......它是toast" 20")但文本框没有被设置...只有案例45(由于某种原因: S?)似乎无论如何工作......无论我选择哪种评级。

1 个答案:

答案 0 :(得分:1)

你需要在每个案例的最后添加break语句,以避免在第一个案例发生之后进入每个案例。

请参阅this