简体   繁体   English

将字符串数组中的数据调用到另一个类

[英]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.上面是我的 QuestionBank 类,它创建了一个 2X4 字符串数组。 First column being the question, 2nd and 3rd being the answer choices, and 4th being the correct answer.第一列是问题,第二和第三列是答案选项,第四列是正确答案。

Below is my RealDeal class.下面是我的 RealDeal 课程。

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.我想要做的是让 Bank[0][0]、Bank[0][1] 和 Bank[0][2] 在我的 RealDeal 类上输出,然后检查 Bank[0][ 3] 与用户输入匹配。 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.我对 Java 真的很陌生,所以如果有人能真正找出答案并向我解释,那就太好了。

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.我认为最好的方法是阅读一本好的 Java 书籍并熟悉该语言本身,然后尝试自己解决这个问题。 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();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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