简体   繁体   English

将用户输入的双精度列表添加到数组

[英]Adding a list of user-inputted doubles to an Array

So my program, when instantiated asks the user how many "items" they want.所以我的程序在实例化时询问用户他们想要多少“项目”。 My program then creates two arrays, one for the "item name" and one for the "item price".然后我的程序创建了两个 arrays,一个用于“项目名称”,一个用于“项目价格”。 I use a loop to let the user input each item name and item price in their respective arrays, however I'm lost with the item price array.我使用循环让用户在各自的 arrays 中输入每个项目名称和项目价格,但是我迷失了项目价格数组。 To use my loop, I need to utilize the "itemprice.length" element but I can't do that when I'm not working with Strings.要使用我的循环,我需要使用“itemprice.length”元素,但当我不使用字符串时,我不能这样做。

After the user inputs the "prices" of each item, I need to apply a multiplier to each array item and output it.用户输入每个项目的“价格”后,我需要对每个数组项目和 output 应用乘数。 So I want to, for example, have 3 items in the array: 1.20, 1.30, 1.40, and then I want the program to ask me for the "sales tax" of which I can enter 0.08 and it will then multiply 0.08 to each item and output a total.因此,例如,我想在数组中有 3 个项目:1.20、1.30、1.40,然后我希望程序询问我可以输入 0.08 的“销售税”,然后将每个项目乘以 0.08项目和 output 共。

Is there a way that I can make my program work so it allows the user to enter, let's say, 5 items and their prices and am I going about it the right way?有没有一种方法可以让我的程序正常工作,这样它就允许用户输入,比如说,5 个项目及其价格,我是否以正确的方式进行? Any way of doing this easier?有什么方法可以更轻松地做到这一点? Thanks!谢谢!

public class Input
{
private Scanner keybd;
private String item;
private double cost;
private String[] costArray;
private String[] itemArray;

/**
 * Constructor for objects of class Scanner
 */
public Input(int anyAmountofItems)
{
    keybd = new Scanner(System.in);
    costArray = new String[anyAmountofItems];
    itemArray = new String[anyAmountofItems];
}
 /**
 * Mutator method to set the item names and costs
 */
public void setArray(){
    for(int index=0; index < itemArray.length; index++){ 
    System.out.println("Enter the item name: ");
    itemArray[index] = keybd.next();}
    for(int indexa=0; indexa < itemArray.length; indexa++){
        System.out.println(itemArray[indexa]);
    }
    for(int indexb=0; indexb < costArray.length; indexb++){ 
    System.out.println("Enter the item cost: ");
    costArray[indexb] = keybd.next();}
    for(int indexc=0; indexc < costArray.length; indexc++){
        System.out.println(costArray[indexc]);
    }
}
    /**
     * Accessor method to return the items cost with tax
     */
    public double getTax(){
        return costArray.length;
    }

Use a Float[] and use Float.parseFloat(String str) to convert from a string to a float.使用Float[]并使用Float.parseFloat(String str)从字符串转换为浮点数。

As an aside, when dealing with money, floating point is a bad idea, since there are always issues with precision.顺便说一句,在处理金钱时,浮点是一个坏主意,因为总是存在精度问题。 It is best to use ints/longs with the appropriate lowest currency unit (ie cents in the US etc.)最好使用具有适当最低货币单位的整数/多头(即美国的美分等)

It's not clear to me what your question is.我不清楚你的问题是什么。 Are you having problems with the item cost data?您对项目成本数据有疑问吗? You are just reading in a String.你只是在读一个字符串。 You should read in an array of doubles instead.您应该改为读取一个双精度数组。

costArray = new double[anyAmountOfItems];
// Then when reading use the appropriate Scanner method
costArray[indexb] = keybd.nextDouble();

A couple style notes:几个风格说明:

  1. You might want to consider using Lists instead of arrays.您可能需要考虑使用列表而不是 arrays。 That way you don't have to worry about fixing the size of the arrays.这样您就不必担心固定 arrays 的大小。
  2. There is no need to use new variable names in each of the for loops.不需要在每个 for 循环中使用新的变量名。 It just adds confusion.它只会增加混乱。 Instead of indexa, indexb, etc just use i.而不是 indexa、indexb 等,只需使用 i。
  3. Better yet, use the enhanced for loop for cases where you don't really need the index:更好的是,在不需要索引的情况下使用增强的 for 循环:

    for( String item: itemArray ) { System.out.println(item); for(String item: itemArray) { System.out.println(item); } }

you can try as:你可以试试:

System.out.println("Enter the sales tax: ");
double salesTax = keybd.next();

double totalTax =0.0;
double total = 0.0;

for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
totalTax = totalTax + (cost * salesTax);
total = total + cost;
}

System.out.println("Total: " + (total-totalTax));

EDIT: TO calculate the total during inserting the cost.编辑:在插入成本期间计算总数。

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

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