I've been a long time lurker, and I've come across a part of my code I cannot figure out even with the help of stackexchange. I can't seem to call an Array from another method of mine. I have a bunch of questions and I would like to check if the answers are correct or not. If you could help me, that would be great. I've been stuck on it for a while, not many others can help me so I must turn here.
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++;
}
The thing is, is that I can't call Question[0] and check if it's right or not. the score++ is initialised up above, and that works, up until the point where I have to check the answers to see if they are right. If you need more code just let me know. Thanks.
Question[]
must be either:
CheckIfCorrect
. Note that if you do option 1, then all instances of the class will share the same value and some concurrency errors could happen.
Also: According to universally accepted Java name conventions, Question[]
should be called question[]
, and Questions()
should be named something like askQuestions()
( a verb in camelcase ). CheckIfCorrect should be called checkIfCorrect
( method and variable name begin with lowercase ).
Example 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++;
}
}
}
Example 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++;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.