如何使用Java将此方法的返回值传递给另一个类?

时间:2015-09-05 15:41:02

标签: java

我创建了一个通过用户输入返回一行数组的方法。我设法通过分配userDigits中的每个值,使用System.out.println在同一个班级上显示输入数字的集合。
我的问题是,如何在另一个类中传递相同的值?

public class LottoTicket extends Ticket {

public int NUM_DIGITS = 5;
public int[] userDigits = new int[NUM_DIGITS];

@Override
public void buyTicket() {
    Scanner input = new Scanner(System.in);

    int number = 0;
    double amount = 0;
    System.out.println("-------=======LOTTO TICKET SCREEN=======--------");
    System.out.println("");
    System.out.println("There are three Types (Prima, Ambo and Terro)");
    System.out.print("Please select one (P, A, T): ");
    String lotteryOption = input.next();

    switch (lotteryOption) {
        case "P": {
            break;
        }
        case "A": {
            break;
        }
        case "T": {

           amount = getAmount(amount);
           getUserData(userDigits);
           int ticketSold;
            ticketSold = +1;
            int tik = ticketSold +1;
            System.out.println(amount);

            System.out.println("Your numbers are:  " + userDigits[0] + " "
                    + userDigits[1] + " " + userDigits[2] + " " + userDigits[3]
                    + " " + userDigits[4] + " ");
            System.out.println("Ticket/s Sold: " + tik);
            System.out.println("Thank you for playing");
            Ticket.pressAnyKeyToContinue();
            LotteryApplication.loginScreen();
            break;
        }
        default: {
            System.out.println("Invalid Input. Please try again.");
            System.out.println("");
            buyTicket();
            break;
        }
    }
}


@Override
public double getAmount(double amount) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter amount of money: ");
    amount = input.nextDouble();
    //input.close();
    return amount;
}

@Override
public int getUserData(int[] userInput) {
    Scanner input = new Scanner(System.in);

    System.out.print("Choose a number from 1 to 90: ");
    userDigits[0] = input.nextInt();
    System.out.print("Choose a number from 1 to 90: ");
    userDigits[1] = input.nextInt();
    System.out.print("Choose a number from 1 to 90: ");
    userDigits[2] = input.nextInt();
    System.out.print("Choose a number from 1 to 90: ");
    userDigits[3] = input.nextInt();
    System.out.print("Choose a number from 1 to 90: ");
    userDigits[4] = input.nextInt();

    //key.close();
    //Returns last value of array
    return userDigits[4];
}
//public int userDigits[];
public static void printTicket(){

   // System.out.println(getUserData.userDigits[4]);
   // for (int i = 0; i < array.length)
}

}

2 个答案:

答案 0 :(得分:0)

嗯,在这种情况下,由于您将userDigits定义为public,因此您可以轻松完成 - 任何具有LottoTicket实例可见性的类都可以访问它。使用当前设置,您可以执行以下操作:

// you need to instantiate LottoTicket somewhere in your app
LottoTicket ticket = new LottoTicket();

// get the input from user
ticket.buyTicket();

// at this point you can access the numbers and pass them to some other method like this
checkNumbers(ticket.userDigits);

上面发布的示例需要您提供方法checkNumbers,该方法具有以下签名:

public void checkNumbers(int[] numbers) {
// the comparison code goes here...

此外,这与您的问题有点无关,但我想指出您当前代码的两个问题:

  1. 将类字段定义为public的工作很少,更常见​​的方法是将这些字段定义为private并提供获取/设置字段值的方法;这会将这些字段上的操作限制为仅允许您使用的字段 - public字段允许任何对该字段具有可见性的人基本上执行任何操作,这通常不是您想要的操作
  2. 如果用户未选择有效的彩票类型,则从buyTicket()致电default;在极端情况下,这可能导致StackOverflowError - 更好的方法是使用一个循环,一直要求用户输入,直到提供有效的
  3. 希望这有助于至少一点。

答案 1 :(得分:-3)

        You can easily pass input array to method of another class arguments.

    class PassValue{
     public static void display(int[] elements){
      Anotherclass obj= new Anotherclass();
      obj.display2(elements);
    }
    }
   class Anotherclass{

     public void display2(int[] values){

       do whatever you want
    }
   }