-5

我尝试通过函数“IF”将变量 N​​O_OF_QUESTIONS_PER_LEVEL 从值 9 更改为 19

public class Constant {


public static int NO_OF_QUESTIONS_PER_LEVEL = 9  ; 

public static int NO_OF_QUESTIONS_PER_LEVEL (int[] args ) {

    if( NO_OF_QUESTIONS_PER_LEVEL  == 10 ) {
        NO_OF_QUESTIONS_PER_LEVEL = 11 ; //i want change value to 11
    }

    else if( NO_OF_QUESTIONS_PER_LEVEL  == 9 ) {
        NO_OF_QUESTIONS_PER_LEVEL = 19 ; //i want change value to 19
    }

    else {
        NO_OF_QUESTIONS_PER_LEVEL = 10 ; //
    }

}

我的代码中的错误是什么?

4

1 回答 1

1

“如果”逻辑有效。int缺少返回值。这是一个简单的问题,你应该学会自己测试它。

public class ifTTest {

   public static int NO_OF_QUESTIONS_PER_LEVEL = 9;

   public static void main( String[] args ) {
      int[] testVectors = { 1, 10, 9, };
      int[] testOutput =  {10, 11, 19 };
      for( int test=0; test < testVectors.length; test++ ) {
         NO_OF_QUESTIONS_PER_LEVEL = testVectors[test];
         NO_OF_QUESTIONS_PER_LEVEL( null );
         if( NO_OF_QUESTIONS_PER_LEVEL != testOutput[test] )
            System.out.println( "error" );
      }
   }

   public static int NO_OF_QUESTIONS_PER_LEVEL( int[] args ) {
      if( NO_OF_QUESTIONS_PER_LEVEL == 10 ) {
         NO_OF_QUESTIONS_PER_LEVEL = 11; //i want change value to 11
      } else if( NO_OF_QUESTIONS_PER_LEVEL == 9 ) {
         NO_OF_QUESTIONS_PER_LEVEL = 19; //i want change value to 19
      } else {
         NO_OF_QUESTIONS_PER_LEVEL = 10; //
      }
      return 0;
   }

}
于 2018-10-30T19:21:55.470 回答