员工计划。 JAVA基础知识

时间:2011-11-16 08:08:33

标签: java

我正在编写一个简单的启动java程序。我面临的问题是公司名称没有正确添加。

public class Company {
    public static BufferedReader br;
    public static BufferedReader br1;
    public static String numberOfCompanies;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CompanyDetails qw = new CompanyDetails();

        try{
            //Scanner in = new Scanner(System.in);
            br = new BufferedReader(new InputStreamReader(System.in));
            br1 = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter number of companies: ");
            numberOfCompanies = br.readLine();
            int G = Integer.parseInt(numberOfCompanies);

            for (int i = 1; i <= G; i++) {
                qw = new CompanyDetails();
                System.out.println("Enter name of the company: ");
                String company = br1.readLine();
                qw.company(company, i);
            }
            for (int i = 0; i <= G; i++) {
            qw.companySummary(G);
            }
        } catch (IOException io) {
            io.printStackTrace();
        }

    }   
}

class CompanyDetails {
    String company, name;
    public String input;
    public static BufferedReader br;
    public double iE;
    public static String numberOfCompanies;
    String nameOfCompany;
    String[] nameofCompany1 = new String[100];
    int ir,i,employee;
    ArrayList<String> bulk = new ArrayList<String>();

    public String[] company(String input, int i) {
        // TODO Auto-generated method stub
        //ArrayList bulk = new ArrayList();
        //for(int ith = i; ith<= 2; i++){
        nameOfCompany = i+input;
        bulk.add(nameOfCompany);
        bulk.add(nameOfCompany);
    //  }
        return nameofCompany1;
    }

    public void employee(double d) {
        // TODO Auto-generated method stub
        ir = (int)d;
    }

    public void companySummary(int G) {

        System.out.println("Number of companies: " + G);
        System.out.println("Name of company: " +bulk +" ");
        System.out.println("Number of employees: "+ir); 
    }   
}

我得到的输出是

enter image description here

为什么我没有在arraylist的位置1获得234?

6 个答案:

答案 0 :(得分:3)

您正在循环的每次迭代中创建一个新的CompanyDetails对象,通过丢失您之前的对象:

for (int i = 1; i <= G; i++) {
   qw = new CompanyDetails();

您已在CompanyDetails方法的开头创建main对象:

CompanyDetails qw = new CompanyDetails();

所以你不必在for循环中再做一次。

答案 1 :(得分:2)

public String[] company(String input, int i) {

nameOfCompany = i+input;
bulk.add(nameOfCompany);
bulk.add(nameOfCompany); .// Why are you adding nameOfCompany twice .
return nameofCompany1;    //Why are you returning nameofCompany1 which is null here 

}

 qw = new CompanyDetails();  //this line should be out of the loop.

Naming Convention上工作。 请提供有关您想要做什么以及您期望的输出的更多详细信息。

答案 2 :(得分:1)

看看代码片段:每个循环实例化qw = new CompanyDetails();

应该是:

 qw = new CompanyDetails();
 for (int i = 1; i <= G; i++) {
  ...
 }

正如@CodeBuzz指出的那样:在bulk.add(nameOfCompany);方法中移除company(),也不要迭代qw.companySummary(G);方法。

答案 3 :(得分:1)

在您的CompanyDetails类中删除方法bulk.add(nameOfCompany);中的一行public String[] company(String input, int i),并将qw = new CompanyDetails();放在for循环中的for循环之外,它将正常工作。

主要课程

public class Company {
    public static BufferedReader br;
    public static BufferedReader br1;
    public static String numberOfCompanies;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CompanyDetails qw = new CompanyDetails();

        try{
            //Scanner in = new Scanner(System.in);
            br = new BufferedReader(new InputStreamReader(System.in));
            br1 = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter number of companies: ");
            numberOfCompanies = br.readLine();
            int G = Integer.parseInt(numberOfCompanies);

            qw = new CompanyDetails();
            for (int i = 1; i <= G; i++) {
                System.out.println("Enter name of the company: ");
                String company = br1.readLine();
                qw.company(company, i);
            }
            for (int i = 0; i <= G; i++) {
            qw.companySummary(G);
            }
        } catch (IOException io) {
            io.printStackTrace();
        }

    }   
}

CompanyDetails类

class CompanyDetails {
    String company, name;
    public String input;
    public static BufferedReader br;
    public double iE;
    public static String numberOfCompanies;
    String nameOfCompany;
    String[] nameofCompany1 = new String[100];
    int ir,i,employee;
    ArrayList<String> bulk = new ArrayList<String>();

    public String[] company(String input, int i) {
        // TODO Auto-generated method stub
        //ArrayList bulk = new ArrayList();
        //for(int ith = i; ith<= 2; i++){
        nameOfCompany = i+input;
        //bulk.add(nameOfCompany);
        bulk.add(nameOfCompany);
    //  }
        return nameofCompany1;
    }

    public void employee(double d) {
        // TODO Auto-generated method stub
        ir = (int)d;
    }

    public void companySummary(int G) {

        System.out.println("Number of companies: " + G);
        System.out.println("Name of company: " +bulk +" ");
        System.out.println("Number of employees: "+ir); 
    }   
}

答案 4 :(得分:1)

我可以看到你的问题得到了很好的回答。我想添加一个与命名约定相关的小注释。我会将company课程中的方法CompanyDetails重命名为AddCompanyregisterCompany,以确保您的读者了解该方法的含义,而无需深入了解其实施细节。

此致

答案 5 :(得分:0)

检索员工详细信息的程序如下:

package employee;
import java.util.*;

public class Employee

{

String name,  gender,address;

 int id;

float salary,da,hra,gross_pay;

public void getdata()

{  
        Scanner in= new Scanner(System.in);

System.out.println("Enter the name of employee");

name=in.next();

System.out.println("Enter the id of employee");

id=in.nextInt();

 System.out.println("Enter the gender of employee");

gender=in.next();

System.out.println("Enter the address of employee");

address=in.next();

 }

public void calc()

 {

 Scanner in= new Scanner (System.in);

System.out.println("enter the salary of employee");

salary=in.nextFloat();

da=salary*15/100;

 hra=salary*10/100;

gross_pay=salary+da+hra;

 }

 public void display()

 {  

System.out.println("Employee Details");

System.out.println("Employee name:"+name+" ");

System.out.println("Employee id:"+id+" ");

System.out.println("Employee gender:"+gender+" ");

System.out.println("Employee address:"+address+" ");

System.out.println("Employee salary:"+salary+" ");

System.out.println("da amount"+da+" ");

System.out.println("hra amount:"+hra+" ");

System.out.println("Gross_pay of employee:"+gross_pay+" ");

 }

public static void main(String[] args) 
{

Employee emp=new Employee();

emp.getdata();

emp.calc();

emp.display();

}

}

输出:

Enter the name of employee

Raju

Enter the id of employee

20

Enter the gender of employee

Male

Enter the address of employee

XYZ Street, India.

Enter the salary of employee

45000

Employee Details

Employee name:Raju 

Employee id:20 

Employee gender:Male 

Employee address:XYZ 

Employee salary:45000.0 

DA amount6750.0 

HRA amount:4500.0 

Gross_pay of employee:56250.0