简体   繁体   English

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

[英]How can I pass the return value from this method to another class using Java?

I created a method that returns a row of arrays via user input. 我创建了一个通过用户输入返回数组行的方法。 I managed to display the set of inputted number on the same class using System.out.println by assigning each value in userDigits. 通过在userDigits中分配每个值,我设法使用System.out.println在同一类上显示一组输入数字。
My question is, how can I pass the same value in another class? 我的问题是,如何在另一个类中传递相同的值?

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)
}

}

Well, in this case, since you defined userDigits as public , you can do it easily - it is accessible to any class that has a visibility to your instance of LottoTicket . 好吧,在这种情况下,由于您将userDigits定义为public ,因此可以轻松实现-任何对LottoTicket实例可见的类都可以访问它。 With the current setup you could do something like this: 使用当前设置,您可以执行以下操作:

// 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);

The example posted above needs you to provide a method checkNumbers that has, for example, the following signature: 上面发布的示例需要您提供一种方法checkNumbers ,该方法例如具有以下签名:

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

Also, this is somewhat unrelated to your question, but I'd like to point out two issues with your current code: 另外,这与您的问题无关,但我想指出您当前代码的两个问题:

  1. defining class fields as public is done rather rarely, more common approach is to define these as private and provide methods for getting/setting the field values; 很少将类字段定义为public字段,更常见的方法是将它们定义为private字段,并提供获取/设置字段值的方法; this restricts the operation on these fields to only those you allow - having public field allows anyone who has a visibility to that field to perform basically any operations, which is usually not what you want 这将对这些字段的操作限制为仅允许您执行的操作-使用public字段可以使对该字段具有可见性的任何人基本上可以执行任何操作,通常这不是您想要的
  2. you call buyTicket() from default in case user didn't select valid lottery type; 如果用户未选择有效的彩票类型,则default情况下调用buyTicket() in extreme cases this could lead to StackOverflowError - better approach would be to use a loop that keeps asking user for input until a valid one is provided 在极端情况下,这可能会导致StackOverflowError更好的方法是使用一个循环,不断询问用户输入,直到提供有效的循环为止

Hope this helps at least a bit. 希望这至少可以有所帮助。

        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
    }
   }

暂无
暂无

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

相关问题 如何在另一个类中调用方法并在java中返回它的值? - How to invoke method in another class and return value from it in java? 如何使用带有Java的Selenium WebDriver将一个类的值传递给另一个类? - How can I pass one class value into another class using Selenium WebDriver with Java? 我可以将getter方法的返回值传递给不同类中另一个对象的setter方法吗? - Can I Pass a return value of a getter method to a setter method of another object in a different class? 我如何从Java集获取返回值 <String> 在java的另一个类中 - how i can get the return value from java set<String> in another class in java 如何在Java中使用TestNG将字符串从主类传递到另一个类? - How can I pass a string from main class to another class using TestNG in java? 我如何使用另一个类的方法从一个Java类的MD数组中删除重复项 - how can i remove duplicates from a MD array in one java class, using a method from another class 如何在Java中将变量从一类传递到另一类? - How can I pass variables from one class to another in Java? 如何使用ASM将操作码从方法复制到Java类文件中的另一个方法? - How can I copy opcodes from a method to another method in Java Class file using ASM? 我如何从一种方法返回值并交换 - How can i return value from one method to another & swapping 如何从AsyncTask类的onPostExecute方法返回值? - How can i return value from AsyncTask class onPostExecute method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM