我的输出

时间:2015-11-08 04:03:55

标签: java arrays

Cullerton Part District每年夏天举办一次迷你奥运会。创建名为Participant的类,其中包含名称,年龄和街道地址的字段。包括为每个字段分配参数值的构造函数和返回包含所有值的String的toString()方法。还包括equals()方法,如果它们在所有三个字段中具有相同的值,则确定两个参与者是相等的。创建一个包含两个阵列的应用程序,每个阵列至少包含5个参与者 - 一个参与者参加迷你马拉松比赛,另一个参加者参加潜水比赛。向用户提示两个事件中的参与者将文件保存为BC.java和ABC.java。

import javax.swing.JOptionPane;
import java.util.*;

public class ABC {
     private  static Participant mini[] = new Participant[2];


    public static void main(String[] args) {

        setParticipant();
        displayDetail();

    }


        // BC p=new BC(name,age,add);
    //displayDetails();
        // System.out.println(    p.toString());
    public static void displayDetail() {

        String name=null;
       String add = null;
        int age=0;

        System.out.println("Name\tAdress\tAge");
      BC p=new BC(name,age,add);
        for (int x = 0; x < mini.length; x++) {
//Participant p1=mini[x];
            System.out.println(p.toString());
        }
    }



           public static String getName() {
              Scanner sc = new Scanner(System.in);
              String name;
             System.out.print(" Participant  name: ");
              return name = sc.next();
           }

         // System.out.print(" Participant  name: ");
         // name = sc.next();
          public static int getAge() {
int age;
               System.out.print(" Enter age ");
                 Scanner sc=new Scanner(System.in);;
                return age= sc.nextInt();
          }
         public static String getAdd() {
             String add;

            Scanner sc=new Scanner(System.in);;
       System.out.print("Enter  Address: ");
       return add=sc.next(); 
         }


    public static void setParticipant(){
        for (int x = 0; x < mini.length; x++) {
         System.out.println("Enter loan details for customer " + (x + 1) + "...");
//Character loanType=getLoanType();
        //String loanType=getLoanType();
        String name=getName();
          String add=getAdd();
          int age=getAge();
         System.out.println();
        }


      }
    }

    //another class
public class BC {


    private String name;
    private int age;
    private String address;


    public BC(String strName, int intAge, String strAddress) {

        name = strName;
        age = intAge;
    address = strAddress;
    }


    @Override
    public String toString() {
        return "Participant [name=" + name + ", age=" + age + ", address=" + address + "]";
    }
public boolean equals(Participant value){
    boolean result;
    if (name.equals(name) && age==value.age && address.equals(address))
            result=true;
    else 
        result=false;
    return result;
        }





}

outPut:
Enter loan details for customer 1...
 Participant  name: hddgg
Enter  Address: 122
 Enter age 12

Enter loan details for customer 2...
 Participant  name: ddjkjde
Enter  Address: hdhhd23
 Enter age 12
//Why I'm not getting right output
Name    Adress  Age
Participant [name=null, age=0, address=null]
Participant [name=null, age=0, address=null]

1 个答案:

答案 0 :(得分:1)

由于此方法,您正在获得该输出:

public static void displayDetail() {

    String name=null;
    String add = null;
    int age=0;

    System.out.println("Name\tAdress\tAge");
    BC p=new BC(name,age,add);
    for (int x = 0; x < mini.length; x++) {
        //Participant p1=mini[x];
        System.out.println(p.toString());
    }
}

您正在创建一个BC,其名称为null,add为0,年龄为0。然后你打印两次。