有//告诉我需要什么帮助。所以我接受了用户输入,现在我尝试使用另一种方法打印输入。我的阵列的内容必须打印出来。
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("---------------------------");
}
}
答案 0 :(得分:0)
我建议覆盖Runner类的ToString
方法
并在方法printRunners
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].toString()); //here!!!
}
banner();
}
答案 1 :(得分:0)
main()方法在方法中定义名为“ar”的变量。在方法之外无法访问此值。为了访问它,您需要将其声明为类的静态变量:
private static Runner[] ar;
打印出来时,你需要循环它:
for( Runner r : ar ){
// somehow print out the Runner class (you haven't included this)
System.out(r.getName() + "\t" + r.getDistance() + "\t" + r.getTime();
}
如果你的跑步者
现在,你遇到的另一个问题是没有任何东西调用printRunners()方法。我假设您希望在main()方法结束时调用它?