gpt4 book ai didi

java - 方法中预期错误]

转载 作者:行者123 更新时间:2023-12-02 11:11:58 24 4
gpt4 key购买 nike

我不知道为什么我不能将数组设置为 j 或 g 进行比较?有更聪明的方法吗?

在 makegradeExam 和 makeMissedArray 方法之前我没有任何问题。 if 语句出现这些错误

DriverTest.java:72: error: ']' expected
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: ')' expected
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: ']' expected
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: ')' expected
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: not a statement
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: ';' expected
if (char[j] answersUppercase != (char[j] answerKey){
^
DriverTest.java:72: error: illegal start of expression
if (char[j] answersUppercase != (char[j] answerKey){
^

代码:

public static void main (String [] args){

Scanner input = new Scanner(System.in);
String passedExam = null;
char[] studentAnswers = new char[20];
char[] answerKey = {'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'D'};


// make the string for the answers
collectStudentAnswers(studentAnswers);

//change the answers to uppercase
answersUppercase(studentAnswers);

//comapre the two strings with i=0 to 20
gradeExam(answerKey, studentAnswers);
makeMissedArray(answerKey, studentAnswers);

passedExam(gradeExam);

System.out.printf("** Correct answers: %f", 20 - gradeExam);
System.out.printf("** Incorrect answers: %f", gradeExam);
System.out.printf("** %s", passedExam);
System.out.printf("You missed the following questions:\n ");
System.out.println(Array.toString(makeMissedArray));


} public static void collectStudentAnswers(char[] studentAnswers){

for (int i = 0; i < 20;){

System.out.printf("Question %d:\n ", i + 1);
char[] studentAnswers = input.next().charAt(0);

i++;

}

return studentAnswers;

} public static char[] answersUppercase(char[] studentAnswers){

studentAnswers.toUpperCase();
return answersUppercase;


} public static String passedExam (int gradeExam) {

if (20 - gradeExam >= 15){

passedExam = "You passed the exam.";

} else {

passedExam = "You failed the exam!";

}

return passedExam;


} public static int gradeExam(char[] answerKey, char[] answersUppercase){

for (int j = 0; j < 20;){


if (char[j] answersUppercase != (char[j] answerKey){

gradeExam++;

}

}

return gradeExam;
j++;

} public static int[] makeMissedArray(char[] answerKey, char[] answersUppercase){

for (int g = 0; g < 20;){

if (char[g] answersUppercase != (char[g] answerKey){

makemissedArray[j] = g;


}

g++;
}
}

}

最佳答案

这根本就是不正确的语法:

if (char[j] answersUppercase != (char[j] answerKey){

除了不匹配的括号之外,这不是访问数组元素的方式。

也许你的意思是这样的:

if (answersUppercase[j] != answerKey[j]){ 

但即使在解决这个问题之后,您的代码中仍然存在其他几个问题(以及一些不是“错误”的奇怪之处,只是不惯用)。

我建议您阅读 Java 教程,尤其是有关数组和 for 循环的教程,会对您有所帮助。

关于java - 方法中预期错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com