JAVA高手来,我是新手,实在是不会了....在线等
下面是老师让做的一个程序,要求就是有20个选择题的答案,判断是否通过考试,但是一直做不对,求教....publicclassDriverExam{privateStrin...
下面是老师让做的一个程序,要求就是有20个选择题的答案,判断是否通过考试,但是一直做不对,求教....
public class DriverExam
{
private String [] correct;
private String [] grades;
private int totalCorrect;
private int totalIncorrect;
private boolean passed;
public DriverExam ( String [] g)
{
grades = new String [20];
for (int count =0;count<20;count++)
{
grades[count] = g[count];
}
String [] correct = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
totalCorrect = 0;
totalIncorrect = 0;
passed = false;
}
public int totalCorrect()
{
for (int count =0;count<20;count++)
{
if(grades[count].equalsIgnoreCase(correct[count]))
{
totalCorrect++;
}
}
return totalCorrect;
}
public int totalIncorrect()
{
return 20-totalCorrect;
}
public boolean passed()
{
if (totalCorrect>=15)
{
passed = true;
}
return passed;
}
}
import javax.swing.JOptionPane;
public class CH8Q5
{
public static void main (String [] args)
{
String [] grades = new String [20];
getGrade(grades);
DriverExam test = new DriverExam(grades);
if (test.passed() ==true)
{
System.out.println("you have passed the exam");
System.out.println("the total number of correctly answered questions are "+ test.totalCorrect());
System.out.println("the total number of incorrectly answered questions are "+ test.totalIncorrect());
}
else
{
System.out.println("you didn't pass the exam");
System.out.println("the total number of correctly answered questions are "+ test.totalCorrect());
System.out.println("the total number of incorrectly answered questions are "+ test.totalIncorrect());
}
}
public static void getGrade(String [] x)
{
for (int count=0;count<20;count++)
{
int miss;
x[count] = JOptionPane.showInputDialog("please enter the your answer of question "+(count+1));
System.out.println(x[count]);
if ( x[count].equalsIgnoreCase("A")|| x[count].equalsIgnoreCase("B") || x[count].equalsIgnoreCase("C")|| x[count].equalsIgnoreCase("D"))
{
}
else
{
System.out.println("Invalid input");
if ( x[count] == null)
{
miss = count+1;
}
}
}
}
} 展开
public class DriverExam
{
private String [] correct;
private String [] grades;
private int totalCorrect;
private int totalIncorrect;
private boolean passed;
public DriverExam ( String [] g)
{
grades = new String [20];
for (int count =0;count<20;count++)
{
grades[count] = g[count];
}
String [] correct = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
totalCorrect = 0;
totalIncorrect = 0;
passed = false;
}
public int totalCorrect()
{
for (int count =0;count<20;count++)
{
if(grades[count].equalsIgnoreCase(correct[count]))
{
totalCorrect++;
}
}
return totalCorrect;
}
public int totalIncorrect()
{
return 20-totalCorrect;
}
public boolean passed()
{
if (totalCorrect>=15)
{
passed = true;
}
return passed;
}
}
import javax.swing.JOptionPane;
public class CH8Q5
{
public static void main (String [] args)
{
String [] grades = new String [20];
getGrade(grades);
DriverExam test = new DriverExam(grades);
if (test.passed() ==true)
{
System.out.println("you have passed the exam");
System.out.println("the total number of correctly answered questions are "+ test.totalCorrect());
System.out.println("the total number of incorrectly answered questions are "+ test.totalIncorrect());
}
else
{
System.out.println("you didn't pass the exam");
System.out.println("the total number of correctly answered questions are "+ test.totalCorrect());
System.out.println("the total number of incorrectly answered questions are "+ test.totalIncorrect());
}
}
public static void getGrade(String [] x)
{
for (int count=0;count<20;count++)
{
int miss;
x[count] = JOptionPane.showInputDialog("please enter the your answer of question "+(count+1));
System.out.println(x[count]);
if ( x[count].equalsIgnoreCase("A")|| x[count].equalsIgnoreCase("B") || x[count].equalsIgnoreCase("C")|| x[count].equalsIgnoreCase("D"))
{
}
else
{
System.out.println("Invalid input");
if ( x[count] == null)
{
miss = count+1;
}
}
}
}
} 展开
2个回答
展开全部
String [] correct = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
这里错了。!! 你重复定义了!!!
把这句删了。
将上面的 private String[] correct;
修改成private String[] correct= {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
就可以了。
还有 你的判断,没有做好哦。
我就改了这里。
得到的答案是:
you didn't pass the exam //没过
the total number of correctly answered questions are 20 //正确20
the total number of incorrectly answered questions are 0 错误0
汗
这里错了。!! 你重复定义了!!!
把这句删了。
将上面的 private String[] correct;
修改成private String[] correct= {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
就可以了。
还有 你的判断,没有做好哦。
我就改了这里。
得到的答案是:
you didn't pass the exam //没过
the total number of correctly answered questions are 20 //正确20
the total number of incorrectly answered questions are 0 错误0
汗
展开全部
新手的话 你好好看一下代码吧,这个是我按照你的意思 重新写的。
package test;
public class DriverExam {
private String[] correct;
private String[] grades;
private int totalCorrect;
private int totalIncorrect;
private String missage;
private int passednum;
private boolean passed;
public DriverExam(String[] correct,String[] grades,int passednum) {
this.passednum=passednum;
this.correct=correct;
this.grades=grades;
this.missage=
package test;
public class DriverExam {
private String[] correct;
private String[] grades;
private int totalCorrect;
private int totalIncorrect;
private String missage;
private int passednum;
private boolean passed;
public DriverExam(String[] correct,String[] grades,int passednum) {
this.passednum=passednum;
this.correct=correct;
this.grades=grades;
this.missage=
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询