Java 对数组中的每个项目应用乘法

标签 java

好吧,所以我正在尝试创建一个“销售税程序”,用户可以在其中输入项目并将其添加到一个名为“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/

相关文章:

java - Android - 在 Activity 类 android.app.application 中找不到方法 playaudio(View)

java - FirebaseException : Failed to parse node with class in NodeUtilities. NodeFromJSON

Java 函数指针和自动装箱

java - 警惕将较长的 List<String> 扁平化为 String

java - IntelliJ 无法编译为 "Java resource does not exist"

java - struts2 MVC框架中哪些文件专门负责Model、View和Controller?

java - JVM到底是如何编译三元运算符的?我应该关注不同的 api 版本吗?

Java内部类和静态嵌套类

java - quartz 作业调度程序不作为 OSGI bundle 工作

java - TinyMCE 在保存时转义所有标签