-1

我正在使用 Java 和 SQL 库处理 Oracle 数据库。我正在尝试计算平均值与平均值相差一个标准差的学生,添加一列来标记这些学生,并计算有多少学生符合标准并将它们添加到新表中。我收到一条错误消息,提示“allAvgs”变量和“theSD”变量“无法解析为变量”。错误发生在我的 if 语句中,但我已经实例化了这些变量。为什么我会收到此错误?任何帮助表示赞赏。

try{
         Statement stOne, stTwo, stThree, stFour;
         String SelectAverage = "SELECT MEAN FROM STUDENTS";
          ResultSet rsOne = stOne.executeQuery(SelectAverage);

    String TotalAverage = "SELECT Avg(MEAN) AS averages FROM STUDENTS";
          ResultSet rsTwo = stTwo.executeQuery(TotalAverage);     

          String student_stan_dev = "SELECT STDEV(MEAN) AS standardDeviation FROM STUDENTS";
          ResultSet rsThree = stThree.executeQuery(student_stan_dev);

           int onesdMean = 1;


           //Loop Duration_Sec column
           while(rsOne.next()){

           //Convert values into float values
           float allAvgs = rsOne.getFloat("MEAN");
           float totalAvg = rsTwo.getFloat("averages"); 
           float StDev = rsThree.getFloat("standardDeviation");

           float theSD = allAvgs - (onesdMean * StDev);
           }

        String flaggedStudents = "ALTER TABLE STUDENTS ADD FlaggedStudents INT";
        ResultSet rsFour = stFour.executeUpdate(flaggedStudents);

           if(allAvgs >= theSD){
               String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
               st.executeUpdate(FlagHint);
           }

        String countInstances = "SELECT STUDENTS.NAME, STUDENTS.FlaggedStudents" +
                "COUNT(*)OVER(PARTITION BY STUDENTS) AS cnt FROM STUDENTS";
           st.executeQuery(countInstances);


        st.executeUpdate("CREATE TABLE IF NOT EXISTS StudentCount" +
        "(NAME INT , cnt INT)");

        String insertVals = String.format("INSERT INTO StudentCount" +
                "(NAME , cnt INT") +
                " VALUES ('%s','%s')");

        st.execute(insertVals);enter code here
4

2 回答 2

1

因为它们是在while(){...}循环内部声明的,并且限制在循环的块范围内while()。您需要在循环之外声明它们以扩展它们的范围。

// you need to declare the variables outside
while(rsOne.next()){
    // this defines a scope , anything declared here is invisible outside
    // this scope

     float allAvgs = rsOne.getFloat("MEAN");
     float totalAvg = rsTwo.getFloat("averages"); 
     float StDev = rsThree.getFloat("standardDeviation");
     float theSD = allAvgs - (onesdMean * StDev);
}
......
......
// compiler has no idea about allAvgs and theSD here
if(allAvgs >= theSD){
    String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
    st.executeUpdate(FlagHint);
}
于 2013-07-24T18:46:53.510 回答
0

您已theSDwhile循环块内声明。

//Loop Duration_Sec column
while(rsOne.next()){

    ...
    float theSD = allAvgs - (onesdMean * StDev);
}

但是尝试在它之后立即使用它。

...

if(allAvgs >= theSD){ // theSD doesn't exist in this scope
    String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
    st.executeUpdate(FlagHint);
}

它不存在于while循环范围之外。您需要在 while 循环之外声明(并初始化)它(以及您需要的所有其他内容)。考虑如果while循环评估为false并且没有运行会发生什么。

于 2013-07-24T18:48:44.360 回答