[英]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: 另外,这与您的问题无关,但我想指出您当前代码的两个问题:
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
字段可以使对该字段具有可见性的任何人基本上可以执行任何操作,通常这不是您想要的 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.