//私は助けが必要なものを教えています。だから私はユーザーの入力を受け取り、今私は別の方法を使用して入力を印刷しようとしています。私の配列の内容は印刷されなければなりません。オブジェクトとクラス。 forループを使用してユーザー入力を印刷するにはどうすればよいですか?
import java.util.Scanner;
public class Running_Calcs {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
final int MAX_RUNNERS = 4;
banner();
Runner[] ar = new Runner[MAX_RUNNERS];
// This is an array for user input. Getter and setter are already set for name,distance,time.
for (int i = 0; i < ar.length; i++) {
System.out.println("\nInformation for runner 1/3");
System.out.print("Name: ");
String name = kbd.next();
System.out.print("Miles: ");
double distance = kbd.nextDouble();
System.out.print("Minutes: ");
int time = kbd.nextInt();
}
kbd.close();
}
public static void printRunners(Runner[] ar) {
for (int i = 0; i < ar.length; i++) {
System.out.println("First Print of runners");
System.out.println("-----------------------");
System.out.println(ar[i]);
}
banner();
}
// I am trying to print the info from the array in this method. I don't know how.
//Along with that , I am trying to print the pace and Temp which I already have in my other class.
private static void banner() {
System.out.println("---------------------------");
System.out.println("Track runner info and pace\nGustavo Bruno");
System.out.println("---------------------------");
}
}
ゲッターとセッターがあれば、どうして使ってみませんか? –
私はそれらをPrintRunnersメソッドで呼び出す必要がありますか? –
ループの各繰り返しで新しいランナーをインスタンス化し、それらのセッターメソッドを使用して属性を設定し、そのインスタンスを配列に格納する必要があります。 printRunnersメソッドでは、各ランナーの対応するゲッターメソッドを呼び出す必要があります。 – RubioRic