【问题标题】:switch statement doesn't workswitch 语句不起作用
【发布时间】:2009-10-28 22:17:31
【问题描述】:

这个 java switch 语句怎么老是告诉我我的语句不是语句

public void setConstant(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;

    switch (this.yourLetterGrade)
    {
        case 'A':
            this.yourNumberGrade >= 0.90;
            break;

        case 'B':
            this.yourNumberGrade >= .8;
            break;

        case 'C':
            this.yourNumberGrade >= .7;
            break;

         case 'D':
            this.yourNumberGrade >= .6;// not a statement

         default:

    } // end switch
}

【问题讨论】:

  • 你的问题与switch语句无关。我建议你使用 Eclipse 之类的 IDE,这样你的错误就会更加明显。
  • 我建议阅读更多有关编程的内容...
  • 我想做的是说如果 yourNumberGrade 是 > or = to .9 case 'A':
  • 请看我下面的回复,你的逻辑很复杂。在这种情况下,case 'A' 所做的是“如果 yourLetterGrade == 'A'”。
  • 更具体地说 - 了解表达式和语句之间的区别。

标签: java syntax switch-statement


【解决方案1】:

我知道你想做什么,但我认为你绕错了方向。您似乎正在尝试做的是基于开关设置“字母等级”,而不是数字等级!我认为您真正想做的是:

public void setGrades(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;

    if( this.yourNumberGrade >= 0.90)
        this.yourLetterGrade = 'A';
    else if(this.yourNumberGrade >=0.80)
        this.yourLetterGrade = 'B';
    else if (this.yourNumberGrade >=0.70)
        this.yourLetterGrade= 'C';
    else if (this.yourNumberGrade >=0.60)
        this.yourLetterGrade= 'D';
    else
        this.yourLetterGrade= 'F';    
}

您不能在 Java 中打开范围。如果你想用开关做这个,你必须做一个switch(true),然后做case this.yourNumberGrade>=0.90:

正如我所料,您误解了开关的工作原理。如果您真的需要通过 switch 执行此操作(if/else/else if 更好),您必须这样做:

public void setGrades(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;
    switch(true)
    {
        case this.yourNumberGrade >= 0.90:
            this.yourLetterGrade = 'A';
            break;
        case this.yourNumberGrade >=0.80:
            this.yourLetterGrade = 'B';
            break;
        case this.yourNumberGrade >=0.70:
            this.yourLetterGrade= 'C';
            break;
        case this.yourNumberGrade >=0.60:
            this.yourLetterGrade= 'D';
            break;
        default:
            this.yourLetterGrade= 'F';    
            break;
    }//end switch
}

【讨论】:

  • 他的问题被标记为 Java。您的第二个 sn-p 无法在 Java 中编译,因为 switch 表达式必须是 int 或 enum 类型,而不是 boolean。
  • 啊,哇,我没有意识到这一点。非常感谢您指出这一点!我通常做 C++/C# 开发,所以我认为它也可以在 Java 中工作。
  • 哇,这肯定是使用 switch 的疯狂方式!
  • 这甚至可以用 C++ 编译吗?据我所知,case 语句中的表达式必须是常量
【解决方案2】:

因为this.yourNumberGrade >= .6; 不是编译器告诉您的有效语句。这将是一个有效的声明:

b = this.yourNumberGrade >= .6;

-- 或--

this.yourNumberGrade = .6;

这取决于你想要完成什么。

【讨论】:

    【解决方案3】:

    你到底想做什么? >= 是比较而不是赋值,这就是你得到错误的原因......只需在所有地方删除 >

    【讨论】:

      【解决方案4】:

      Eric 很好地解释了如何去做你似乎想要完成的事情,但让我澄清一下你哪里出错了。

      switch/case 结构将给定变量(switch 参数)与可能的值(case 参数)进行比较,然后在匹配的 case 语句和下一个 break 语句之间执行代码(或者,如果语言不支持 fall -through,在下一个 case 语句之前)。

      您要做的不是将变量与常量表达式进行比较,而是将变量与条件进行比较。 if/elseif 结构可能是一种更简洁的表达方式:

      if (this.yourNumberGrade >= 0.90) {
          this.yourLetterGrade = 'A';
      } else if (this.yourNumberGrade >= 0.80) {
          this.yourLetterGrade = 'B';
      } else if (this.yourNumberGrade >= 0.70) {
          this.yourLetterGrade = 'C';
      } else if (this.yourNumberGrade >= 0.60) {
          this.yourLetterGrade = 'D';
      } else { // you left the default out, but I assume this will be an F for Failed
          this.yourLetterGrade = 'F';
      }
      

      如果您希望它更短,您可以尝试像这样尝试使用三元运算符:

      this.yourLetterGrade = (
          this.yourNumberGrade >= 0.90 ? 'A' : (
              this.yourNumberGrade >= 0.80 ? 'B' : (
                  this.yourNumberGrade >= 0.70 ? 'C' : (
                      this.yourNumberGrade >= 0.60 ? 'D' : 'F'
                  )
              )
          )
      )
      

      如您所见,这会降低您的可读性,因此 if/else 可能是最简洁的方法。

      Eric 试图向您展示的是这样的结构:

      switch (true) { // We compare the boolean constant "true" to the case arguments
      case this.yourNumberGrade >= 0.90:
      // this is a boolean expression and evaluates either
      // to "true" (matches the switch argument) or
      // to "false" (does not match the switch argument)
          this.yourLetterGrade = 'A';
          break;
      case this.yourNumberGrade >= 0.80:
          this.yourLetterGrade = 'B';
          break;
      case this.yourNumberGrade >= 0.70:
          this.yourLetterGrade = 'C';
          break;
      case this.yourNumberGrade >= 0.90:
          this.yourLetterGrade = 'D';
          break;
      default:
      // This is executed if none of the case arguments evaluate
      // to the value of the switch argument.
          this.yourLetterGrade = 'F';
          // No break needed, because the end of the switch structure follows:
      }
      

      我希望这可以为您解决问题。您可能必须更加注意您尝试使用的结构的确切语义。这些结构在大多数语言中都非常相似。

      对于踢腿和咯咯笑,你甚至可以用数组来做:

      // Our letter grades in ascending order (from bad to good).
      String letterGrades[] = {'F','D','C','B','A'};
      // Our number grade is in the range [0.0;1.0]. As floating point numbers are
      // too precise for indexes, we want to round them down to the cut-off
      // (0.9, 0.8, etc) and turn them into integer values we can use as array indices.
      int gradeIndex = (int) Math.floor(this.yourNumberGrade*10);
      // The lowest cut-off is 0.6, so we can treat everything lower than that the same
      gradeindex = gradeindex - 5;
      gradeIndex = Math.max(gradeIndex, 0);
      // With Math.max we have ensured that no index can be lower than 0, now we need
      // to make sure that no index is larger than the largest index in our array
      // (which by definition is equal to the array's length (i.e. number of elements)
      // minus 1 (because the lowest index is 0, an array of e.g. size 4 has the
      // indices 0,1,2,3, but lacks an index 4 -- better get used to it, that's how
      // programmers count, too).
      gradeIndex = Math.min(gradeIndex, letterGrades.length-1);
      // Now that our index is clean and guaranteed to be within range, we can use it
      // to look up the letter grade:
      this.yourLetterGrade = letterGrades[gradeIndex];
      

      没有 cmets 和一些速记,这甚至更短:

      // Grades are as follows: A: 90%+, B: 80%+, C: 70%+, D: 60%+, F: <60%
      String letterGrades[] = {'F','D','C','B','A'};
      int gradeIndex = Math.min(
          Math.max(0, (int) Math.floor(this.yourNumberGrade*10) - 5),
          letterGrades.length-1
      );
      this.yourLetterGrade = letterGrades[gradeIndex];
      

      请注意,这使得字母等级的确切截止点在哪里变得不太清楚,这就是它需要 cmets 的原因。此外,如果临界值因任何原因发生变化(例如 A:85%+ 或 F:Math.floor(this.yourNumberGrade*10)-5 部分),但这将使其更难遵循,并且如果成绩不仅仅是渐进式的,也无济于事。但是,对于传统系统,这是一种快速简便的方法。

      【讨论】:

        【解决方案5】:

        你只是在做一个在这种情况下不是有效陈述的比较。

        你可能是想完成一项任务

        【讨论】:

          【解决方案6】:

          如果您想要完成,请将 ">=" 替换为 "="。

          【讨论】:

            【解决方案7】:

            问题是您正在进行比较而不是分配值。也许你可以这样做:

            public void setConstant(float inNumGrade)
            {
                this.yourNumberGrade = inNumGrade;
            
                switch (this.yourLetterGrade)
               {
                    case 'A':
                        this.yourNumberGrade = 0.90;
                        break;
            
                    case 'B':
                        this.yourNumberGrade = .8;
                        break;
            
                    case 'C':
                        this.yourNumberGrade = .7;
                        break;
            
                    case 'D':
                        this.yourNumberGrade = .6;
                    default:
            
                } // end switch
            }
            

            这实际上会将值分配给“yourNumberGrade”。但这只是等级的下限。最好将“yourNumberGrade”替换为“yourLetterGrade”并让它确定字母等级根据数字等级...

            【讨论】:

              【解决方案8】:

              您必须添加一个中断;每个案例块的内部。

              switch(this.grade){
                   case 'A':
                       System.out.println("You got an A");
                       break;
                  default:
                       System.out.println("INVALID GRADE");
                       break;}
              

              【讨论】:

                猜你喜欢
                • 2014-03-08
                • 1970-01-01
                • 1970-01-01
                • 2014-06-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多