好吧,所以我正在尝试创建一个“销售税程序”,用户可以在其中输入项目并将其添加到一个名为“costArray”的数组中。我几乎只知道如何使用String(因为我需要 costArray.length 进行循环),但我有点迷失了。任何人都可以帮助我指出正确的方向,这样我就可以:取一个数字数组( double )并对其应用乘数(0.08 为销售税百分比)并输出总数(双倍)?这是我到目前为止所得到的,你能告诉我我是否接近吗?谢谢!:
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; } // /** // * Mutator method to calculate tax on each item // */ // public void calculateTax(){ // for(int index=0; index < costArray.length; index++){ // System.out.println(0.08 * costArray[index]); // } // } }
最佳答案
数字存储在字符串中,您必须将其“转换”为实数( double 值)
具体操作方法如下:
String s = "-1.234";
double value = Double.parseDouble(s);
关于Java 对数组中的每个项目应用乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5667439/