简体   繁体   English

Java - 家庭作业 - 使用变量的字符串值来命名 class 的实例

[英]Java - Homework - Using the string value of a variable to name an instance of a class

Okay, so I'm working on a homework assignment where I have a staff of 10 salespeople.好的,我正在做一项家庭作业,我有 10 名销售人员。 We have a contest for the greatest number of sales.我们有一个争取最大销售量的竞赛。 The assignment wants the user to enter 10 integer values as a number of sales and then once they've all been entered the salesperson with the highest value will be returned.该作业要求用户输入 10 个 integer 值作为销售数量,然后在输入所有值后返回值最高的销售人员。

What she wants:她想要什么:

  • A Class "Sales" with (String name and Integer sales) values.具有(字符串名称和 Integer 销售额)值的 Class“销售额”。
  • A while loop where the user inputs integers for number of sales用户输入销售数量整数的 while 循环

What I'm attempting to do.我正在尝试做什么。 I assume the names of the salespeople at the company are known, so I just created an array strSalesPerson of 10 fictitious names.我假设公司销售人员的名字是已知的,所以我只创建了一个包含 10 个虚构名字的数组 strSalesPerson。 I created a counter salespersonCounter to create the counter for the while loop for user input.我创建了一个计数器 salespersonCounter 来为用户输入的 while 循环创建计数器。 I'm trying to basically create salesperson1, salesperson2, salesperson3, and so on by creating a variable with the string "salesperson" concatenated with the counter.我试图通过创建一个字符串“salesperson”与计数器连接的变量来基本上创建 salesperson1、salesperson2、salesperson3 等。 I want to use that as the name of the instance for each salesperson entered into the Sales Class.我想将其用作输入销售 Class 的每个销售人员的实例名称。

Sales Class销售 Class

Just for reference, the class I've created and trying to create instances of is as follows:仅供参考,我创建并尝试创建实例的 class 如下所示:

    
private String salesname;
private Integer numsales;

public Sales(String name, Integer sales) {
this.salesname = name;

    if (sales >= 0.0) {
        this.numsales = sales;
    }
} 

// Setter for name
public void setName(String name) {
    this.salesname = name;
}

// Getter for name
public String getName() {
    return salesname;
}

// Setter for Sales
public void setSales(Integer sales) {
if (sales >=0) {
    this.numsales = sales;
} 
}
// Getter for Sales
public int getSales() {
    return numsales;
}


} // End of Class 

TestSales试销

This is where I will get the user input and save it as an instance of the Sales class. However, right now I'm just going to use the current instance from the array of names and the static integer 3 to ensure that I get the other pieces functioning correctly and then I'll switch over to user inputs from there.这是我将获取用户输入并将其保存为 Sales class 实例的地方。但是,现在我将使用名称数组中的当前实例和 static integer 3 来确保我得到其他部分正常运行,然后我将从那里切换到用户输入。


// Import Scanner
import java.util.Scanner;

public class TestSales {
    public static void main(String[] args) {
    
    // create scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    
    // Initialize variables
    int salespersonCounter = 1;
    
    // Fill Salesperson names
    String[] strSalesPerson = new String[]{"Mark Hasselback","Gary Moore","Shelly Hemingway", "Susan Meagre","Nick Pantillo","Craig Grey","Alice Reese","Mickey Greene","Chaz Ramirez","Kelly Southerland"};
            
    while (salespersonCounter <=10) {
        String salespersoninstance = ("salesperson"+ salespersonCounter);
        String currsalesperson = strSalesPerson[salespersonCounter -1];
        System.out.printf("%s");
        Sales salespersoninstance = new Sales(strSalesPerson[salespersonCounter -1],);
        System.out.printf("%s is %s!%n",salespersoninstance,currsalesperson);
        salespersonCounter += 1;
    }
    
    }
}

The problem I am running into is here:我遇到的问题在这里:

        Sales salespersoninstance = new Sales(strSalesPerson[salespersonCounter -1],3);

instead of accepting the string value of salespersoninstance (in this case salesperson1) as the name of the instance of the Sales Class, it is telling me that salespersoninstance is a duplicate local variable.它没有接受 salespersoninstance(在本例中为 salesperson1)的字符串值作为 Sales Class 实例的名称,而是告诉我 salespersoninstance 是一个重复的局部变量。 It is interpreting it I guess as me trying to define a new variable with the same name as one I've already declared?我猜它正在解释它,因为我试图定义一个与我已经声明的变量同名的新变量?

Basically what I want is with the while counter, create a string variable salesperson1, salesperson2, salesperson3 and so on with "salesperson" + salespersonCounter, and use that resulting string to name the instance of the Sales class. That way I can then say Sales salespersoninstance = new Sales(strSalesperson[salespersoncCounter -1], userinput)基本上我想要的是 while 计数器,用“salesperson”+ salespersonCounter 创建一个字符串变量 salesperson1、salesperson2、salesperson3 等等,然后使用生成的字符串命名 Sales class 的实例。这样我就可以说 Sales salespersoninstance = new Sales(strSalesperson[salespersoncCounter -1], userinput)

To help you a bit forward:为了帮助你前进一点:

        String[] salePersonNames = new String[]{"Mark Hasselback", "Gary Moore", "Shelly Hemingway", "Susan Meagre", "Nick Pantillo", "Craig Grey", "Alice Reese", "Mickey Greene", "Chaz Ramirez", "Kelly Southerland"};
        for (int i = 0; i < salePersonNames.length; i++) {
            Sales salesPerson = new Sales(salePersonNames[i], 3);
            System.out.printf("%s is %s!%n", salesPerson.getName(), salesPerson.getSales());
        }

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

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