繁体   English   中英

没有获得我在程序中寻找的输出,也遇到了用户输入等于字符串循环的麻烦

[英]Not getting the output I'm looking for in program, also having trouble with a user input equaling a string loop

我的代码已经完成并且可以正常工作,这就是我所拥有的

import java.util.*;
import java.io.*;
import java.text.*;

public class Proj5 {
public static void main(String[] args)throws IOException{
    Scanner s = new Scanner(System.in);
    PrintWriter outFile = new PrintWriter(new File("results.txt"));
    int [] quizKey = {1,1,2,2,1,1,3,2,4,1,3,5,4,1,2};
    String [] userAnswers = new String[100];
    String [] wid = new String[100];
    int [] numCorrect = new int[quizKey.length];

DecimalFormat df = new DecimalFormat("#0.0");
    int max;
    int min;

    int lines=0;
    readInText(s);
    s = readInText(s);
    while(s.hasNext()){
        String line = s.nextLine();
        String[] tokens = line.split(",");
        wid[lines] = tokens[0];
        userAnswers[lines] = tokens[1];
        lines ++;

    }// end while loop
    long[][] userAnswersInt = new long[lines][quizKey.length];
    numCorrect = gradeSingleQuiz(lines, quizKey, userAnswers, numCorrect, userAnswersInt);
    double[] percentCorrect = new double[lines];
    percentCorrect = percentCorrect(lines, numCorrect, numCorrect);
    char[] grades = new char[lines];
    grades = grade(numCorrect, lines);
    max=max(numCorrect);
    min=min(numCorrect);

    displayOutput(outFile, wid, lines, numCorrect, grades, percentCorrect);
    averageScore(outFile, max, min, lines, percentCorrect);
}//end main



public static Scanner readInText(Scanner s)throws IOException{
    /*String input;
    System.out.print("Enter name of the file: ");
    input=s.nextLine();
    System.out.println(input);
    while(input!="QuizScores.txt"){
        System.out.println("invalid file entered, please enter the correct anem of the file: ");
        input=s.nextLine();
    }
    */
    Scanner inFile = new Scanner(new File("QuizScores.txt"));

    return inFile;


}// end readInText

public static String[] userAnswers(String userAnswers[]){
    return userAnswers;
}
/**
 * calculates the grades of the tests
 * 
 * @param lines - number of 
 * @param quizKey - answers to the test
 * @param userAnswers - string version of user answers
 * @param numCorrect - number of answers correct
 * @param userAnswersInt - int version of user answers
 * @return numCorrect - number of answers correct
 */
public static int[] gradeSingleQuiz(int lines, int quizKey[], String userAnswers[], int numCorrect[], long userAnswersInt[][]){
    for (int j=0; j<lines; j++){
    numCorrect[j]=0;
        long[] ara = new long[quizKey.length];
        ara [j] = Long.parseLong(userAnswers[j]);
        for(int p=0; p<ara.length; p++){
            userAnswersInt[j][p]=ara[j]%10;
            ara[j]=ara[j]/10;
        }
        int rows = userAnswersInt.length;
            int cols = userAnswersInt[0].length;
            long[][] userAnswersReverse = new long[rows][cols];
        if(j==4){
            for(int q=0; q<userAnswersInt.length;q++){
            for(int w = cols-1; w >=0; w--){
                userAnswersReverse[q][cols-1-w] = userAnswersInt[q][w];
                }
            }
        }
        if(j==4){
            for(int r=0; r<lines; r++){
                for(int n=0; n<quizKey.length; n++){
                    System.out.println(userAnswersReverse[r][n]);
                    if(userAnswersReverse[r][n]==(quizKey[n])){
                    numCorrect[r]++;    
                    }
                }
            }
        }   
    }//end for loop

return numCorrect;
}// end gradeSingleQuiz
/**
 * calculates the max
 * 
 * @param numCorrect - number of correct answers
 * @return max - the highest number of correct answers
 */
public static int max(int numCorrect[]){
    int max = numCorrect[0];
    for(int r=0; r<numCorrect.length; r++){
        if(numCorrect[r]>max){
            max=numCorrect[r];
        }
    }
return max;
}
/**
 * calculates the min
 * 
 * @param numCorrect - number of correct answers
 * @return min - the lowest number of correct answers
 */
public static int min(int numCorrect[]){
    int min = numCorrect[0];
    for(int r=0; r<numCorrect.length; r++){
        if(numCorrect[r]<min){
            min=numCorrect[r];
        }
    }
return min;
}
/**
* grade of the test
*
* @param numCorrect - number of answers correct
* @param lines - number of lines in file
* @return grade - the letter grade is being returned)
*/
public static char[] grade(int numCorrect[], int lines){
    char[] grade = new char[lines];
    for (int j=0; j<lines; j++){


if(numCorrect[j]>=14)
            grade[j]='A';
        else if((numCorrect[j]>=12)&&(numCorrect[j]<14))
            grade[j]='B';
        else if((numCorrect[j]>=11)&&(numCorrect[j]<12))
            grade[j]='C';
        else if ((numCorrect[j]>=9)&&(numCorrect[j]<11))
            grade[j]='D';
        else
            grade[j]='F';
    }
    return grade;
}//end grade
/**
 * percent of the answers correct
 * 
 * @param lines - number of lines in file
 * @param numCorrect - number of answers correct
 * @param quizKey - correct answers
 * @return centCorrect - percent of correct answers (1)
 */
public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
    double[] centCorrect = new double[lines];
    for (int j=0; j<lines; j++){
    centCorrect[j] = (numCorrect[j]/quizKey.length)*100;
    }
return centCorrect;
}
/**
 * average of all the test scores & displays min and max
 * 
 * @param min - lowest score
 * @param max - highest score
 * @param lines - number of lines in file
 * @param percentCorrect - percent of correct answers
 * @return none
 */
public static void averageScore(PrintWriter outFile, int min, int max, int lines, double percentCorrect[]){
    double add=0;
    for(int d=0; d<lines; d++){ 
        add = percentCorrect[d] + add;
    }//end for loop
    add=add/lines;
    System.out.println("Average: " + add + "%");
    outFile.println("Average: " + add + "%");
    System.out.println("High Score: " + max);
    outFile.println("High Score: " + max);
    System.out.println("Low Score: " + min);
    outFile.println("Low Score: " + min);
}// end averageScore
/**
 * displays the student id number correct percent correct and grade
 * 
 * @param wid - wildcat ID
 * @param lines -number of lines in file 
 * @param numCorrect - number of answers correct
 * @param grades - grade of test
 * @param percentCorrect - percent of answers correct
 * @return none
 */
public static void displayOutput( PrintWriter outFile, String wid[], int lines, int numCorrect[], char grades[], double percentCorrect[]){
    System.out.println("Student ID    # Correct    %Correct    Grade");
    outFile.println("Student ID    # Correct    %Correct    Grade");

for(int i=0; i<lines; i++){
        System.out.println("  " + wid[i] + "          " + numCorrect[i] + "        " +
                (percentCorrect[i]) + "%" + "      " + grades[i]);  
        outFile.println("  " + wid[i] + "          " + numCorrect[i] + "        " +

(percentCorrect[i]) + "%" + "      " + grades[i]);
    }

}// end display output

}//end class

这是我不包括用户输入要求时得到的输出:

Student ID    # Correct    %Correct    Grade
 4563123          15        100.0%      A
 2312311          9        0.0%      D
 2312345          13        0.0%      B
 5527687          9        0.0%      D
 7867567          6        0.0%      F
Average: 20.0%
High Score: 0
Low Score: 15

因此,我将从这个问题开始,总的来说,它很好用,只是出于某种原因,它没有将值分配给percentCorrect数组(我认为不是100%),另外还分配了高分(最大)和低分(最小)不能正常工作,我也不知道为什么会这样,所以对这些事情的帮助会很棒,我对此完全感到沮丧。

我的另一个问题是,我需要让用户输入文件名,如果他们输入的文件不正确,请让他们知道自己所做的,然后再次询问直到正确为止,我实现了:

String input;
    System.out.print("Enter name of the file: ");
    input=s.nextLine();
    System.out.println(input);
    while(input!="QuizScores.txt"){
        System.out.println("invalid file entered, please enter the correct anem of the file: ");
        input=s.nextLine();
    }

但这是行不通的,因为即使我输入正确的字符串(国会大厦和所有东西),它仍然会打印“请检查无效的文件,请...”这一行,我也不知道我在做什么错,因此请提供帮助这太棒了。

提前致谢

编辑:

好吧,我认为问题可能出在这些代码块上(同样不是100%)

percentCorrect = percentCorrect(lines, numCorrect, numCorrect);

和/或

public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
    double[] centCorrect = new double[lines];
    for (int j=0; j<lines; j++){
    centCorrect[j] = (numCorrect[j]/quizKey.length)*100;
    }
return centCorrect;
}

和/或

public static int max(int numCorrect[]){
    int max = numCorrect[0];
    for(int r=0; r<numCorrect.length; r++){
        if(numCorrect[r]>max){
            max=numCorrect[r];
        }
    }
return max;

和/或

public static int min(int numCorrect[]){
    int min = numCorrect[0];
    for(int r=0; r<numCorrect.length; r++){
        if(numCorrect[r]<min){
            min=numCorrect[r];
        }
    }
return min;
}

我从中提取的文件是这样的:

4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111

您得到“输入的文件不正确”的原因是因为这一行while(input!="QuizScores.txt")

您不能使用“ ==”或“!=”来检查字符串是否相等,而应使用equals方法,例如input.equals("QuizScores.txt")

其背后的原因是,使用“!=”检查字符串对象是否具有引用相等性。

另外,在您的代码中,您两次调用readInText() ,第一次不执行任何操作。

高分和低分显示不正确,因为averageScore()的方法定义将min作为第二个参数,将max作为第三个参数,当您调用它时,将min和max交换了下来。

编辑:

而%Correct的第一个显示为100%,其余的显示为0%的原因是由于计算百分比的数学方法。

centCorrect[j] = (numCorrect[j]/quizKey.length)*100由于整数除法而无法工作,导致整数,即小数丢失。

为了获得正确的值,将int转换为double就像这样

centCorrect[j] = ((double)numCorrect[j]/(double)quizKey.length)*100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM