使用Java中的方法输入和输出变量数组?

时间:2015-12-08 12:02:29

标签: java arrays

我有2个课程,测试和主要。类测试有2个属性a和b。我想让程序使用数组和用户输入的值。

public class Testing{
private String a,b;
public Testing(a,b){
    this.a=a;
    this.b=b;
 }  
public void getA(){
    return a;
 }
public void getB(){
    return b;
 }
}

接下来是我的主要课程

public class Main{
public static void main(String[] args){
Scanner ss = new Scanner(System.in);
Scanner s = new Scanner(System.in);
int x,i;
System.out.print("How many lines? ");
x = s.nextInt();
//method inputData
Testing ts[] = new Testing[x];
for (i=0; i<x; i++)
    System.out.print("Enter Value A :"); String a = ss.nextLine();
    System.out.print("Enter Value B "); String b = s.nextInt();
    ts[i] = new Testing(a,b);
 }
//method outputData
System.out.println("Output---");
for(i=0; i<x; i++){
    System.out.print("Value A"+i+" "+ts[i].getA());
    System.out.print("Value B"+i+" "+ts[i].getB());
 }
}

它实际上工作正常,但我想将代码移动//方法输入数据和//方法输出数据从Main转到Testing。所以,我只是从Main调用方法inputData()outputData()。但我不知道怎么做。 如果你帮助我,我会感激不尽。

4 个答案:

答案 0 :(得分:1)

首先,应该在main方法之外声明方法。只需将方法的部分复制到测试文件即可。然后,您实例化一个测试对象:

Testing test = new Testing(...)
test.inputData()
test.outputData()

答案 1 :(得分:1)

我无法理解,为什么你这样做但你可以这样尝试;

1

Testing.java;

public class Testing {
    private String a, b;

    public Testing(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public static int outputData(int x, Testing[] ts) {
        int i;
        for (i = 0; i < x; i++) {
            System.out.print("Value A" + i + " " + ts[i].getA());
            System.out.print("Value B" + i + " " + ts[i].getB());
        }
        return i;
    }

    public static Testing[] inputData(Scanner ss, Scanner s, int x) {
        int i;
        Testing ts[] = new Testing[x];
        for (i = 0; i < x; i++) {
            System.out.print("Enter Value A :");
            String a = ss.nextLine();
            System.out.print("Enter Value B ");
            String b = s.nextLine();
            ts[i] = new Testing(a, b);
        }
        return ts;
    }
}

和Main.java;

public class Main {
    public static void main(String[] args) {
        Scanner ss = new Scanner(System.in);
        Scanner s = new Scanner(System.in);
        int x, i;
        System.out.print("How many lines? ");
        x = s.nextInt();
//method inputData
        Testing[] ts = Testing.inputData(ss, s, x);
//method outputData
        System.out.println("Output---");
        i = Testing.outputData(x, ts);
    }

}

2

同样@Fabiotk说,你可以inputDataoutputData方法non-static使用并创建类对象。

Testing.java;

public class Testing {
    private String a, b;

    public Testing(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public int outputData(int x, Testing[] ts) {
        int i;
        for (i = 0; i < x; i++) {
            System.out.print("Value A" + i + " " + ts[i].getA());
            System.out.print("Value B" + i + " " + ts[i].getB());
        }
        return i;
    }

    public Testing[] inputData(Scanner ss, Scanner s, int x) {
        int i;
        Testing ts[] = new Testing[x];
        for (i = 0; i < x; i++) {
            System.out.print("Enter Value A :");
            String a = ss.nextLine();
            System.out.print("Enter Value B ");
            String b = s.nextLine();
            ts[i] = new Testing(a, b);
        }
        return ts;
    }
}

和Main.java;

  public class Main {
    public static void main(String[] args) {
        Scanner ss = new Scanner(System.in);
        Scanner s = new Scanner(System.in);
        Testing test = new Testing("a", "b");

        int x, i;
        System.out.print("How many lines? ");
        x = s.nextInt();
        //method inputData
        Testing[] ts = test.inputData(ss, s, x);
        //method outputData
        System.out.println("Output---");
        test.outputData(x, ts);
    }
}

答案 2 :(得分:1)

你可以做这样的事情,但这些方法应该用主要方法创建: 要调用这些方法,您应该在主方法中使用它:

  Testing  ts = new Testing();
    ts.inputData(4);
    ts.outputData(3);

要创建方法,请执行以下操作:

  //method inputData
  Testing ts[] = new Testing[x];
   for (i=0; i<x; i++)
  System.out.print("Enter Value A :"); String a = ss.nextLine();
  System.out.print("Enter Value B "); String b = s.nextInt();
   ts[i] = new Testing(a,b);
}

方法inputData变为:

 public int[] inputData(int x){
    Testing ts[] = new Testing[x];
     for (i=0; i<x; i++)
     System.out.print("Enter Value A :"); String a = ss.nextLine();
     System.out.print("Enter Value B "); String b = s.nextInt();
   ts[i] = new Testing(a,b);
   return ts;
   }

此:

 System.out.println("Output---");
for(i=0; i<x; i++){
System.out.print("Value A"+i+" "+ts[i].getA());
System.out.print("Value B"+i+" "+ts[i].getB());
}

成为方法outputData:

 public void outputData(int x){
  for(i=0; i<x; i++){
   System.out.print("Value A"+i+" "+ts[i].getA());
    System.out.print("Value B"+i+" "+ts[i].getB());
   }
 }

答案 3 :(得分:0)

你不能。你有一组Testing-instances。它必须类似于Testing类中的静态print方法,它接受一组Testing-instances。你可以这样做,但这是一个错误的关注点分离。 输入法也是如此。

您可以在Testing中编写一个print方法,它将为该特定的Testing 实例输出a和b 的值,然后在main方法中调用该print方法。

可能是这样的:

public class Testing {
// ...

    public void getDataFromUser(Scanner s) {
         System.out.print("Enter Value A :"); this.a = s.nextLine();
         System.out.print("Enter Value B "); this.b = s.nextInt();
    }

    public void printToSysout() {
         System.out.print("Value A"+i+" "+getA());
         System.out.print("Value B"+i+" "+getB());
    }
}

然后你将循环保存在主方法中:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    // ..get x and such
    for (i=0; i<x; i++) {
        ts[i] = new Testing(); // create a no-args constructor for Testing
        ts[i].getDataFromUser(s);
    }

    System.out.println("Output---");
    for(i=0; i<x; i++) {
        ts[i].printToSysout();
    }
}

此外,&#39;测试&#39;是一个非常糟糕的类名。类名通常是名词,描述它们包含的内容。也许TestData还是什么?