简体   繁体   中英

Call data from a string array to another class

public class QuestionBank {  

    public static void main(String[] args) {

        int k = 0;

        String Bank[][] = {{"The sun is hot.","A. True","B. Flase","A"},
           {"Cats can fly.","A. True","B. False","B"}};
    }
}

Above is my QuestionBank class that creates a 2X4 string array. First column being the question, 2nd and 3rd being the answer choices, and 4th being the correct answer.

Below is my RealDeal class.

import javax.swing.JOptionPane;
import java.util.Scanner;

public class RealDeal {
    public static void main(String[] args) {

        input = JOptionPane.showInputDialog(Bank[0][0]\nBank[0][1]\nBank[0][2]);
        if (input == Bank[0][3]) {
            input = 10;
        } else {
            input = 0;
        }

        total = input/1;

        JOptionPane.showMessageDialog(null,"You scored a " + total + " out of 10. Great job!");

        System.exit(0);
    }
}

What I'm trying to do is to get Bank[0][0], Bank[0][1], and Bank[0][2] to output on my RealDeal class and then to check whether Bank[0][3] matches with the users input. Can anyone please help me with this. Im really new to java so if anyone could actually draw out the answer and explain it to me that would be great.

I think the best way is reading a good Java book and become familiar with the language itself and then try to solve this by your own. If you then have a real question there is no problem asking it here again. But your code is... not really working at all. I don't think this portal is a "please do my work for me" portal.

To call anything from another class you will need to either setup a method for a return or make the variables public.

So:

public class Class1
{
    // for method 1
    public String s1 = "This is a string"

    // for method 2
    public Class1 {}

    public returnString()
    { 
         return s1;
    }
}

public class CLASS2
{
   public static void main(String args[])
   {
       // get the class
       cls1 = new Class1();

      // retrieving - method 1
      String str = cls1.s1;

      // retrieving - method2
      str = cls1.returnString();
   }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM