我已经很长时间了,而且我遇到了我的代码的一部分,即使在stackexchange的帮助下也无法弄清楚。我似乎无法用我的另一种方法调用数组。我有很多问题,我想检查答案是否正确。如果你能帮助我,那就太好了。我已经坚持了一段时间,没有多少人可以帮助我所以我必须转到这里。
public static void Questions()
{
String [] Question = new String [5];
Question[0] = "";
Question[1] = ""; <---- these actually contain stuff in them, I just filtered them out.
for (int i=0; i<=4; i++)
{
JOptionPane.showInputDialog(Question[i]);
}
}
public static int CheckIfCorrect()
{
int score = 0;
if (Question[0].equals("a"))
{
score++;
}
问题是,我不能打电话给问题[0]并检查它是否正确。得分++在上面初始化,并且有效,直到我必须检查答案以确定它们是否正确。如果您需要更多代码,请告诉我。感谢。
答案 0 :(得分:3)
Question[]
必须是:
CheckIfCorrect
。请注意,如果执行选项1,则该类的所有实例将共享相同的值,并且可能会发生一些并发错误。
另外:根据普遍接受的Java名称约定,Question[]
应该被称为question[]
,而Questions()
应该被命名为askQuestions()
(camelcase中的动词)。应该调用CheckIfCorrect checkIfCorrect
(方法和变量名以小写开头)。
示例1
public class ThisClass {
private static String[] question = new String [5]; //declaration
public static void ask(){
question[0] = "";
question[1] = ""; <---- these actually contain stuff in them, I just filtered them out.
for (int i=0; i<=4; i++){
JOptionPane.showInputDialog(question[i]);
}
}
public static int check(){
int score = 0;
if (question[0].equals("a"))
{
score++;
}
}
}
示例2
public class ThisClass {
public static void ask(){
String[] question = new String[5];
question[0] = "";
question[1] = ""; <---- these actually contain stuff in them, I just filtered them out.
for (int i=0; i<=4; i++){
JOptionPane.showInputDialog(question[i]);
}
}
public static int check(String[] param){
int score = 0;
if (param[0].equals("a")) {
score++;
}
}
}