gpt4 book ai didi

java - 方法标题: error ']' expected (Java)中的数组

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

我对数组和方法还很陌生,而且我一直在通过几个程序来看到此错误:出现错误“[”。

在每种情况下,当我进行其他调整时,它似乎都可以自我纠正,但是在这种情况下,我完全感到沮丧。

顺便说一句,我正在使用几种方法和数组来创建测验(在您问,是的,这是一个作业,我同意,列表是处理此数据的更好方法-但这不是一种选择)。

我可能在方法之间未正确传递数组,因为我对该过程有些困惑。据我了解,为了在方法之间发送/接收(即导入/导出)数组或其他变量,我必须在方法 header 参数中声明该变量/数组。

import java.util.Scanner;

public class H7pseudo
{

public static void main(String[] args)
{
//call getAnswerkey method
getAnswerkey(answerkey[i]);

//call getAnswers method
getAnswers(answers[i]);

//call passed method? necessary or no?
boolean passed = passed(answerkey[i], answers[i], qMissed[i], points);

//Print results of grading
if (passed)
{
System.out.println("Congratulations! You passed.");
}
else
{
System.out.println("Try again, sucka. You FAILED.");
}

//call totalPoints
totalIncorrect(points);

//call questionsMissed
questionsMissed(qMissed[i]);
}

//get answer key (create answerkey array & export)
public static void getAnswerkey(answerkey[i])
{
//create answerkey array here
char[] answerkey;

//determine number of questions (indices)
answerkey = new char[20];

//input values (correct answers) for each index
//for our purposes today, the answer is always 'c'.
for (int i = 0; i <=20; i++)
{
answerkey[i] = 'c';
}
}

//get student answers (create answers array & export)
public static void getAnswers(answers[i])
{
//initialize scanner for user input
Scanner scan = new Scanner(System.in);

//create answer array here
char[] answers;

//determine number of questions (indices)
answers = new char[20];

//prompt for user input as values of each index
for (int i = 0; i <= 20; i++) {
answers[i] = scan.nextChar();
}
}

//grade student answers (import & compare index values of arrays:answers&answerkey
//create & export qMissed array
public static boolean passed(answerkey[i], answers[i], qMissed[i], points)
{
int points = 0;

//create new array: qMissed
boolean[] qMissed;

//determine number of questions to be graded
qMissed = new boolean[20];

//initialize values for array
for (int i = 0; i <= 20; i++) {
qMissed[i] = false;
}

//cycle through indices of answerkey[i] & answers[i];
for (int i = 0; i =< 20; i++)
{
if (answers[i] == answerkey[i])
{
correct = true;
points = points+1;
qMissed[i] = true;
}
else {
qMissed[i] = false;
}
}

//evaluate whether or not the student passed (15+ correct answers)
if (points >= 15)
{
passed = true;
}
else
{
passed = false;
}
return passed;
}

public static void totalIncorrect(points)
{
int missed = 20 - points;
System.out.println("You missed " + missed + " questions.");
}


public static void questionsMissed(qMissed[i])
{
// for each index of the array qMissed...
for (int i = 0; i < qMissed.length; i++)
{
//...print correct and false answers.
system.out.println(i + ": " + qMissed[i] + "\n");
}
}


}

最佳答案

您无法在Java中的方法签名中定义数组大小。

关于java - 方法标题: error ']' expected (Java)中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23666145/

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