无法找出java.lang.NullPointerException错误
所以,我几乎整天都在做这个项目,我不知道为什么会出现这些错误。我知道将setKey从DemoTestGrade传递到TestGrade是有原因的,但正如我所说的,我无法理解 该程序由两个文件组成,它们共同构成a级考试 程序1:测试平地机无法找出java.lang.NullPointerException错误,java,arrays,string,Java,Arrays,String,所以,我几乎整天都在做这个项目,我不知道为什么会出现这些错误。我知道将setKey从DemoTestGrade传递到TestGrade是有原因的,但正如我所说的,我无法理解 该程序由两个文件组成,它们共同构成a级考试 程序1:测试平地机 // Chris Brocato // 04-30-15 // This program will perform the methods for DemoTestGrader public class TestGrader { private cha
// Chris Brocato
// 04-30-15
// This program will perform the methods for DemoTestGrader
public class TestGrader {
private char[] setKey;
private char[] grade;
public TestGrader(char[] key){
grade = key;
}
public boolean passed (){
return (totalCorrect() > 14);
}
public int totalCorrect(){
int correct = 0;
for (int i = 0; i < setKey.length; i++){
if (setKey[i] == grade[i])
correct++;
}
return correct;
}
public int totalMissed(){
int tmissed = 0;
tmissed = setKey.length - totalCorrect();
return tmissed;
}
public int[] questionsMissed(){
int size = setKey.length - totalCorrect();
int[] missed = {};
if (size < 1)
return missed;
else
missed = new int [size];
int pos = 0;
for (int i = 0; i < setKey.length; i++){
if (setKey[i] != grade[i]){
missed[pos] = (i + 1);
pos = pos + 1;
}
}
return missed;
}
}
// Chris Brocato
// 05-02-15
// This program will use TestGrader.java to grade a test of 20 questions
import java.util.Scanner;
public class DemoTestGrader {
public static void main(String[] args) {
// Declare variables and objects
char[] setKey = {'A', 'D', 'C', 'D', 'A', 'B', 'B', 'D', 'A', 'C', 'D', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'A', 'B'};
char[] grade = new char[20];
Scanner keyScan = new Scanner(System.in);
//
for(int x = 0; x == setKey.length; x++){
char input;
do{
input = Character.toUpperCase(keyScan.next().charAt(0));
}while(input < 'A' || input >'D');
// store answer
setKey[x] = input;
}
// Ask user for input and loop through and ask for an answer for each question
System.out.println("Please enter the letter chosen for each answer: ");
for(int i = 0; i < grade.length; i++){
char input;
do{
System.out.print(i + 1 + ". ");
input = Character.toUpperCase(keyScan.next().charAt(0));
}while(input < 'A' || input >'D');
// store answer
grade[i] = input;
}
// Print the output to the screen
TestGrader test = new TestGrader(grade);
System.out.println();
System.out.println("You " + (test.passed()?"passed" : "did not pass") + ".\n");
System.out.println("Correct: " + test.totalCorrect() + "\n");
System.out.println("Incorrect: " + test.totalMissed() + "\n");
System.out.println("Questions missed: " + test.questionsMissed());
// Close scanners
keyScan.close();
}
}
//克里斯·布罗卡托
// 04-30-15
//此程序将执行降级爬坡器的方法
公共类测试分级器{
私有字符[]设置密钥;
私家车【】级;
公共测试分级器(字符[]键){
等级=关键;
}
已传递公共布尔值(){
返回(totalCorrect()>14);
}
公共整数totalCorrect(){
int correct=0;
对于(int i=0;i
程序2(主):降级分级器
// Chris Brocato
// 04-30-15
// This program will perform the methods for DemoTestGrader
public class TestGrader {
private char[] setKey;
private char[] grade;
public TestGrader(char[] key){
grade = key;
}
public boolean passed (){
return (totalCorrect() > 14);
}
public int totalCorrect(){
int correct = 0;
for (int i = 0; i < setKey.length; i++){
if (setKey[i] == grade[i])
correct++;
}
return correct;
}
public int totalMissed(){
int tmissed = 0;
tmissed = setKey.length - totalCorrect();
return tmissed;
}
public int[] questionsMissed(){
int size = setKey.length - totalCorrect();
int[] missed = {};
if (size < 1)
return missed;
else
missed = new int [size];
int pos = 0;
for (int i = 0; i < setKey.length; i++){
if (setKey[i] != grade[i]){
missed[pos] = (i + 1);
pos = pos + 1;
}
}
return missed;
}
}
// Chris Brocato
// 05-02-15
// This program will use TestGrader.java to grade a test of 20 questions
import java.util.Scanner;
public class DemoTestGrader {
public static void main(String[] args) {
// Declare variables and objects
char[] setKey = {'A', 'D', 'C', 'D', 'A', 'B', 'B', 'D', 'A', 'C', 'D', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'A', 'B'};
char[] grade = new char[20];
Scanner keyScan = new Scanner(System.in);
//
for(int x = 0; x == setKey.length; x++){
char input;
do{
input = Character.toUpperCase(keyScan.next().charAt(0));
}while(input < 'A' || input >'D');
// store answer
setKey[x] = input;
}
// Ask user for input and loop through and ask for an answer for each question
System.out.println("Please enter the letter chosen for each answer: ");
for(int i = 0; i < grade.length; i++){
char input;
do{
System.out.print(i + 1 + ". ");
input = Character.toUpperCase(keyScan.next().charAt(0));
}while(input < 'A' || input >'D');
// store answer
grade[i] = input;
}
// Print the output to the screen
TestGrader test = new TestGrader(grade);
System.out.println();
System.out.println("You " + (test.passed()?"passed" : "did not pass") + ".\n");
System.out.println("Correct: " + test.totalCorrect() + "\n");
System.out.println("Incorrect: " + test.totalMissed() + "\n");
System.out.println("Questions missed: " + test.questionsMissed());
// Close scanners
keyScan.close();
}
}
//克里斯·布罗卡托
// 05-02-15
//该程序将使用TestGrader.java对20个问题的测试进行评分
导入java.util.Scanner;
公开课成绩评定员{
公共静态void main(字符串[]args){
//声明变量和对象
char[]setKey={'A','D','C','D','A','B','D','A','C','C','B','B','C','D','A','A','B'};
字符[]等级=新字符[20];
Scanner keyScan=新扫描仪(System.in);
//
for(int x=0;x==setKey.length;x++){
字符输入;
做{
输入=Character.toUpperCase(keyScan.next().charAt(0));
}while(输入<'A'|输入>'D');
//存储应答
设置键[x]=输入;
}
//询问用户的输入,循环并询问每个问题的答案
System.out.println(“请输入为每个答案选择的字母:”);
对于(int i=0;i'D');
//存储应答
等级[i]=输入;
}
//将输出打印到屏幕上
TestGrader测试=新的TestGrader(等级);
System.out.println();
System.out.println(“您”+(test.passed()?“passed”:“未通过”)+“\n”);
System.out.println(“正确:“+test.totalCorrect()+”\n”);
System.out.println(“不正确:“+test.totalMissed()+”\n”);
System.out.println(“遗漏的问题:+test.questionsMissed());
//近距离扫描器
keyScan.close();
}
}
如果有帮助,这里是作业说明。
宾夕法尼亚大学要求你编写一个程序,对某项考试的笔试部分进行评分。这次考试有20道选择题。以下是正确答案:
A、 D,C,D,A,B,B,D,A,C,B,B,B,C,D,B,A,A,B
为此,您应该创建一个TestGrader类。该类将有一个包含20个字符的答案数组,该数组将保存正确的测试答案。它将有两个公共成员函数,允许用户程序与类交互:setKey和grade。setKey函数接收包含正确答案的20个字符的字符串,并将此信息复制到其答案数组中。grade函数接收包含考生答案的20个字符的数组,并将每个答案与正确答案进行比较。申请人必须正确回答20个问题中的15个或更多才能通过考试。“评分”考试后,评分函数应创建一个字符串并返回给用户,该字符串包含以下信息:
- 指示申请人考试是通过还是不通过的消息
- 正确回答的问题总数,错误回答的问题总数
谢谢。您的类
DemoestGrade
创建了一个自己的数组setKey
,但它实际上从未在类TestGrade
中设置名为setKey
的字段
您应该调整构造函数以接受等级
和设置键
:
public TestGrader(char[] grade_, char[] setKey_){
grade = grade_;
setKey = setKey_;
}
我也不明白您的setKey
循环用于什么:
for(int x = 0; x == setKey.length; x++){
char input;
do{
input = Character.toUpperCase(keyScan.next().charAt(0));
}while(input < 'A' || input >'D');
// store answer
setKey[x] = input;
}
for(int x=0;x==setKey.length;x++){
字符输入;
做{
输入=Character.toUpperCase(keyScan.next().charAt(0));
}while(输入<'A'|输入>'D');
//存储应答
设置键[x]=输入;
}
通过设置循环条件
x
,将其修复为实际更新setKey
,或者将其丢弃并使用上面的预设数组。查看setKey
:
public class TestGrader {
private char[] setKey;
private char[] grade;
你把它放在哪里?没有。所以当你跑步时:
public int totalCorrect(){
int correct = 0;
for (int i = 0; i < setKey.length; i++){
if (setKey[i] == grade[i])
correct++;
}
return correct;
}
public int totalCorrect(){
int