首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将用户输入的名称与存储在名称数组中的所有名称进行比较。

将用户输入的名称与存储在名称数组中的所有名称进行比较。
EN

Stack Overflow用户
提问于 2018-05-26 13:32:52
回答 2查看 274关注 0票数 0

我正试着写一个程序来检查两名员工的工资。因此,我首先接受名为employeeName的数组中的所有员工的名称,以及名为AnnualSales的数组中的员工的sales。当我试图用存储在employeeName数组中的名称检查用户输入的名称时,我将面临问题。

代码语言:javascript
复制
import java.util.Scanner;

public class Array {

    public static void main(String[] args) {

        int numberOfEmployees;                              // will store the number of employees
        int compare;                                        // number of employees you want to compare results to

        //Scanner class enables user input
        Scanner sp = new Scanner(System.in);

        System.out.println("Enter the number of employees: ");
        numberOfEmployees = sp.nextInt();

        String[] employeeName = new String[numberOfEmployees];              // this string array will store the name of employees
        double[] AnnualSales = new double[numberOfEmployees];               // this will store the sales of every individual employee

这将存储所有的employeeNames和它们的AnnualSales。

代码语言:javascript
复制
        for(int i = 0 ; i < numberOfEmployees ; i++) {

            System.out.printf("Enter name of employee %d: ",i+1);
            employeeName[i] = sp.next();

            System.out.printf("Enter salary of employee %d: ",i+1);
            AnnualSales[i] = sp.nextDouble();

        }

        System.out.println("Enter the number of employees you want to compare records of: ");
        compare = sp.nextInt();

        if(compare > numberOfEmployees) {

            System.out.println("IndexOutOfBound");
            System.exit(0);
        }

        String[] comparison = new String[compare];

        for(int i = 0; i < compare; i++) {

            System.out.printf("Enter name of employee %d for comparison: ",i+1);
            comparison[i] = sp.next();

            // a loop to go through all the names in the employeeName array
            System.out.println(comparison[i]);

我只想检查雇员的名字是否已经在employeeName数组中了。下面的if条件存在于比较employeeName数组中的第一个名称时,但我希望检查特定雇员的名称和employeeName数组中的所有员工。

代码语言:javascript
复制
            for(int j = 0 ; j < numberOfEmployees ; j++) {  
                if(comparison[i] != employeeName[j]) {
                    System.out.println("Employee does not exist!");
                    System.exit(0);
                }
            }
        }

        // compare salary of 2 employees

        if(AnnualSales[compare-1] > AnnualSales[compare-2]) {

            System.out.printf("Sales of %s are greater than %s",employeeName[compare-1], employeeName[compare-2]);
        }else {
            System.out.printf("Sales of %s are less than %s",employeeName[compare-1], employeeName[compare-2]);
        }

        sp.close();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-26 13:35:07

不要像这样将字符串与==进行比较:

代码语言:javascript
复制
comparison[i] != employeeName[j]

使用equals()代替:

代码语言:javascript
复制
!comparison[i].equals(employeeName[j])
票数 0
EN

Stack Overflow用户

发布于 2018-05-26 13:57:03

代码语言:javascript
复制
You need to correct this as:
    for(int j = 0 ; j < numberOfEmployees ; j++) {  
if(!comparison[i].equals(employeeName[j])) {
                        System.out.println("Employee does not exist!");
                        System.exit(0);
                    }
                }
            }
    **Output after changing this condition of your code:**
    Enter the number of employees: 
    3
    Enter name of employee 1: a
    Enter salary of employee 1: 1
    Enter name of employee 2: b
    Enter salary of employee 2: 2
    Enter name of employee 3: c
    Enter salary of employee 3: 3
    Enter the number of employees you want to compare records of: 
    2
    Enter name of employee 1 for comparison: a
    a
    Employee does not exist!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50543310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档