i am a student that learning java
This time, I'm making a code for recommending a prize as a school assignment.
Sorry for the inconvenience because it's Google Translate
I would like to ask you how to get the class referenced array into another method.
In the code below, if the Customer[] a array created in CustomerGenerator is checked in the Run method, the value is not saved and output.
thank you for your help
package assignment_1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class LotteryApp {
private static Scanner scanner;
public LotteryApp() {
scanner = new Scanner(System.in);
}
public void GoodGenerator(Good[] b) {
int nGoods;
while(true) {
System.out.print("게임의 당첨 경품은 몇개입니까>>");
try {
nGoods = scanner.nextInt();
break;
}
catch(InputMismatchException e) {
System.out.println("정수를 입력해야만 합니다.");
scanner.nextLine(); // <Enter> 키까지 읽어서 버린다.
continue;
}
}
b = new Good[nGoods]; // Player [] 레퍼런스 배열 생성
// 각 참여자의 이름을 입력받아 Player 객체 생성
for(int i=0; i<nGoods; i++) {
System.out.print("경품의 이름을 입력하세요>>");
String name = scanner.next();
b[i] = new Good(name);
}
}
static void CustomerGenerator(Customer[] a) {
int nPlayers;
while(true) {
System.out.print("게임에 참가하는 인원은 몇명입니까>>");
try {
nPlayers = scanner.nextInt();
break;
}
catch(InputMismatchException e) {
System.out.println("정수를 입력해야만 합니다.");
scanner.nextLine(); // <Enter> 키까지 읽어서 버린다.
continue;
}
}
a = new Customer[nPlayers]; // Player [] 레퍼런스 배열 생성
// 각 참여자의 이름을 입력받아 Player 객체 생성
for(int i=0; i<nPlayers; i++) {
System.out.print("참가자의 이름을 입력하세요>>");
String name = scanner.next();
a[i] = new Customer(name);
}
}
public void run() {
int Whack;
Customer[] a = {};
Good[] b = {};
while(true) {
System.out.print("게임에 꽝은 몇개입니까>>");
try {
Whack = scanner.nextInt();
break;
}
catch(InputMismatchException e) {
System.out.println("정수를 입력해야만 합니다.");
scanner.nextLine(); // <Enter> 키까지 읽어서 버린다.
continue;
}
}
CustomerGenerator(a);
GoodGenerator(b);
System.out.println(a[0].CustomerName);
int A = a.length;
System.out.println(A);
}
public static void main(String[] args) {
LotteryApp game = new LotteryApp();
game.run();
}
}
class Good{
private Scanner scanner;
public String GoodName;
public int GoodPrice;
public boolean GoodUse;
public Good(String GoodName) {
this.GoodName = GoodName;
scanner = new Scanner(System.in);
}
}
class Customer{
private Scanner scanner;
public String CustomerName;
public String WinningPriceName;
public boolean Winning;
public Customer(String CustomerName) {
this.CustomerName = CustomerName;
scanner = new Scanner(System.in);
}
}
Think of a function parameter like a local variable of that function:
You can put a value in it from inside the function, but it will not go outside.
You have to return the array you allocate from CustomerGenerator
like this:
static Customer [] CustomerGenerator() {
/** skip same code **/
Customer [] outArray = new Customer[nPlayers]; // Player [] 레퍼런스 배열 생성
// 각 참여자의 이름을 입력받아 Player 객체 생성
for(int i=0; i<nPlayers; i++) {
System.out.print("참가자의 이름을 입력하세요>>");
String name = scanner.next();
outArray[i] = new Customer(name);
}
return outArray;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.