简体   繁体   English

我的输出有问题

[英]I have some issue with my output

The Cullerton Part District holds a mini-Olympics each summer.每年夏天,Cullerton Part District 都会举办一场小型奥运会。 Create a class named Participant with fields for a name, age, and street address.创建一个名为 Participant 的类,其中包含姓名、年龄和街道地址的字段。 Include a constructor that assigns parameter values to each field and a toString() method that returns a String containing all the values.包括一个为每个字段分配参数值的构造函数和一个返回包含所有值的字符串的 toString() 方法。 Also include an equals() method that determines two Participants are equal if they have the same values in all three fields.还包括一个 equals() 方法,如果两个参与者在所有三个字段中具有相同的值,则该方法确定两个参与者相等。 Create an application with two arrays of at least 5 Participants each--one holds the Participants in the mini-marathon and the other holds Participants in the diving competition.创建一个具有两个数组的应用程序,每个数组至少有 5 个参与者——一个持有迷你马拉松的参与者,另一个持有潜水比赛的参与者。 Prompt the user for Participants who are in both events save the files as BC.java and ABC.java.提示用户同时参加这两个事件的参与者将文件保存为 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]

You are getting that output because of this method:由于此方法,您将获得该输出:

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());
    }
}

You are creating a BC with null for name and add and 0 for age.您正在创建一个 BC,名称为 null,添加为 0,年龄为 0。 You are then printing it twice.然后您将其打印两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM