将变量的值更改为另一个变量的值

时间:2018-10-30 19:14:39

标签: java

我尝试通过函数“ IF”将变量NO_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 ; //
    }

}

我的代码中哪个错误?

1 个答案:

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

}