【问题标题】:ERROR JAVA CODE correct it错误 JAVA 代码更正它
【发布时间】:2018-05-01 22:50:14
【问题描述】:
//my code is on java code on employ details using console
//my input is no.of employess
//i am taking input a employ details
//function on searching for a given employ for a given id

//代码含义 // #String 员工[][]=new String[numberOfEmployees][12];将存储//雇用详细信息 //#String 输入将采用字符串格式的员工详细信息。

//#String selection 将选择为 yes 来搜索另一个员工

/以上代码是关于在员工管理系统上开发简单的 JAVA 代码/

import java.util.Scanner;

public class EmployeeManagement {

    public static void main(String args[]){
        Scanner read=new Scanner(System.in);
        System.out.println("Enter  the no.of employees");
        int numberOfEmployees=read.nextInt();//GIVEN INPUT FOR NO.OF EMPLOYESS
        int employeeId;
        String employee[][]=new String[numberOfEmployees][12];
        for(int inner=0;inner<numberOfEmployees;inner++){/*taken input on employ details*/
            for(int outer=0;outer<11;outer++){
                employee[inner][outer]=read.nextLine();
            }
            System.out.println();
        }
        for(int inner=0;inner<numberOfEmployees;inner++){
                 employee[inner][11] = (int)(Integer.parseInt(employee[inner][5])+Integer.parseInt(employee[inner][6])
                                 +Integer.parseInt(employee[inner][7])+Integer.parseInt(employee[inner][8])
                                 -Integer.parseInt(employee[inner][9])-Integer.parseInt(employee[inner][10]));
                           }
        String choice;


                     do{
            System.out.println("do you want to search enter employee id");
            String input=read.nextLine();
            System.out.println("do you want to continue press yes or YES");
                        read.nextLine();
            choice =read.nextLine();        
        }while(choice=="yes" || choice=="YES");
                     for(int inner=0;inner<numberOfEmployees;inner++){
            if(employee[inner][0]=="input"){   
        for(int outer=0;outer<12;outer++)
            System.out.print(employee[inner][outer]);
                                                               }
        System.out.println();







         }



    }
}

【问题讨论】:

  • a) 正确格式化代码 b) 清楚地提出问题
  • 这是一个愤怒的问题。请避免使用全部大写尤其是,当你制作它超级大和大胆

标签: java class enums


【解决方案1】:

如果您正确格式化问题中的代码,将更容易看出问题所在。但首先:

不要像这样比较字符串:

choice=="yes"

然后您只需检查第一个 String 对象是否与第二个对象相同,在这种情况下它们永远不会。

改为这样:

choice.equals("yes")

或者更好,因为您永远不会冒 NullPointerException 的风险:

"yes".equals(choice)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2014-07-31
    • 2011-10-07
    • 1970-01-01
    • 2015-09-14
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多