【问题标题】:Scanners and class扫描仪和类
【发布时间】:2015-03-28 01:58:25
【问题描述】:

我们现在在类中做类、构造函数和方法,我得到了基础知识。跳到凌晨 2 点和 4 小时在网上搜索我的问题的解决方案,我回到 Stack Overflow 寻求建议。

我已经建立了我的课程,并在其中有我的 get 和 sets:

 public class staff 
     {
        private String empName;
        private int prefix;
        private int dialCode;
        private int telNum;
        private double grossSalary;


    public staff( String name, int prefix1, int dial, int celNum, double salary)
     {
        empName = name;
        prefix = prefix1;
        dialCode = dial;
        telNum = celNum;
        grossSalary = salary;
    }

下一部分是我感到困惑的地方。我现在正在尝试使用扫描仪填充这一切,但出现错误。与往常一样,我不是在寻找一个正确的答案,而是在寻找正确的方向。这是我的代码的第二部分。

    import java.util.Scanner;

public class staffInfo 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        String staffName  [] = new String [4];
        int staffPrefix   [] = new int    [4];
        int staffDialCode [] = new int    [4];
        int staffCellNum  [] = new int    [4];
        double staffSalary[] = new double [4];

        int i = 0, j = 0;

        for(j = 0; j < staffName.length; j++)
        {
            for(i = 0; i < staffName.length; i++)
            {
                System.out.print("Please enter staff name and press return : ");
                staffName [i] = input.nextLine();
                System.out.print("Please enter there cell number prefix : ");
                staffPrefix [i] = input.nextInt();
                System.out.print("Please enter dialing code : ");
                staffDialCode[i] = input.nextInt();
                System.out.print("Please enter cell number : ");
                staffCellNum[i] = input.nextInt();
                System.out.print("Please enter there salary : ");
                staffSalary[i] = input.nextDouble();
            }
            staff staff[j] = new staff(staffName,staffPrefix,staffDialCode,staffCellNum,staffSalary);
        }

    }

}

【问题讨论】:

  • " 跳到凌晨 2 点 ....提前感谢您为我提供的任何帮助。" - 建议:上床睡觉。
  • 您遇到了哪个错误?请将其添加到您的帖子中。
  • 在床上躺了一会儿斯蒂芬,但问题是让我保持清醒。感谢您的建议。
  • “我现在正在尝试使用扫描仪来填充这一切,但我遇到了错误” - 建议 #2:如果您希望人们帮助您调试代码,你需要解释你的问题是什么。
  • Alexander 我在人员人员 [j] = 新人员部分和此行的多个标记下得到错误行 - 构造函数人员(String [],int [],int [],int [], double[]) 未定义 - 标记“j”上的语法错误,删除此标记 - 类型不匹配:无法从人员转换为人员[]

标签: java class methods constructor java.util.scanner


【解决方案1】:

您的最后一行将无法编译。

查看您传递给员工类的构造函数的参数,类型是否匹配?

此外,您必须将人员数组的声明与分配分开。你对 staffName、staffPrefix 等都做的很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多