Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中如何在数组中存储值?_Java_Arrays - Fatal编程技术网

在java中如何在数组中存储值?

在java中如何在数组中存储值?,java,arrays,Java,Arrays,我想在数组中存储非“h”值。所以为了给你们一个背景,我需要做一个基本的收银机,它可以接受5个项目,每个项目都有一个价格。然而,有些项目将包含HST(税)。了解哪些项目有税,哪些没有税。用户将在输入美元金额之前或之后按h或h。我已经将HST的值存储在一个数组中,但是如何存储非HST的值呢 样本输入: 4.565H H2.3435 4.565h 5.234 5.6576h 样本输出: HST Values: 4.565 2.3435 4.565 5.6576 Non-HST Values 5.2

我想在数组中存储非“h”值。所以为了给你们一个背景,我需要做一个基本的收银机,它可以接受5个项目,每个项目都有一个价格。然而,有些项目将包含HST(税)。了解哪些项目有税,哪些没有税。用户将在输入美元金额之前或之后按h或h。我已经将HST的值存储在一个数组中,但是如何存储非HST的值呢

样本输入:

4.565H
H2.3435
4.565h
5.234
5.6576h
样本输出:

HST Values:
4.565
2.3435
4.565
5.6576

Non-HST Values
5.234

我的代码:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // Create scanner object and set scanner variables
        Scanner inp = new Scanner(System.in);
        System.out.println("Press any key to start");
        String key = inp.nextLine();
        System.out.println("\nEnter the amount of each item");
        System.out.println("Up to 5 inputs are allowed!\n");

        // Initialize counter and index variables to use it in the while loop
        int counter = 0;
        int index = 0;
 
        // Create a double array variable, and set the limit to 5
        double[] numbers = new double[5];

        // Create a boolean variable to use it in the while loop
        boolean go = true;

        while (go) {           
            String value = inp.nextLine();      
            value = value.toLowerCase();
  
            // Set the index value to "h" or "H"
            int indexOfh = value.indexOf('h');

            boolean containsh = (indexOfh == 0 || indexOfh == value.length() - 1);
            
            if (containsh) { //Validate h at beginning or end
                numbers[index] = Double.parseDouble(value.replace("h", ""));
                index++;
                System.out.println("HST will be taken account for this value");
            }

            counter++;
            if (counter == 5) {
                go = false;
            }
        }

        System.out.println("HST Values:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
//创建扫描仪对象并设置扫描仪变量
扫描仪inp=新扫描仪(System.in);
System.out.println(“按任意键启动”);
String key=inp.nextLine();
System.out.println(“\n输入每个项目的金额”);
System.out.println(“最多允许5个输入!\n”);
//初始化计数器和索引变量以在while循环中使用它
int计数器=0;
int指数=0;
//创建一个双数组变量,并将限制设置为5
double[]数字=新的double[5];
//创建一个布尔变量以在while循环中使用它
布尔go=true;
while(go){
字符串值=inp.nextLine();
value=value.toLowerCase();
//将索引值设置为“h”或“h”
int indexOfh=value.indexOf('h');
布尔containsh=(indexOfh==0 | | indexOfh==value.length()-1);
如果(containsh){//在开始或结束时验证h
numbers[index]=Double.parseDouble(value.replace(“h”)和“);
索引++;
System.out.println(“该值将考虑HST”);
}
计数器++;
如果(计数器==5){
go=假;
}
}
System.out.println(“HST值:”);
for(int i=0;i
您只需要两个数组,一个用于H值,另一个用于非H值。为什么不使用列表?名单会更好


但是,如果要求是数组,只需通过像您一样检查“h”值来存储h值,如果“h”的索引为-1,这意味着该值不包含h,则只需将其添加到另一个数组中,并在最后打印出来。

我建议您使用
ArrayList

import java.util.ArrayList;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // Create scanner object and set scanner variables
        Scanner inp = new Scanner(System.in);
        System.out.println("Press any key to start");
        String key = inp.nextLine();
        System.out.println("\nEnter the amount of each item");
        System.out.println("Up to 5 inputs are allowed!\n");

        // Initialize counter and index variables to use it in the while loop
        int counter = 0;
 
        // Create a double array variable, and set the limit to 5
        ArrayList<Double> numbers = new ArrayList<Double>();

        // Create a boolean variable to use it in the while loop
        boolean go = true;

        while (go) {           
            String value = inp.nextLine().toLowerCase();
            int indexOfH = value.indexOf('h');

            if (indexOfH == 0 || indexOfH == value.length() - 1) { //Validate h at beginning or end
                numbers.add(Double.parseDouble(value.replace("h", "")));
                System.out.println("HST will be taken account for this value");
            }

            counter++;
            if (counter == 5)
                go = false;
        }

        System.out.println("HST Values:");
        System.out.println(numbers);
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
班长{
公共静态void main(字符串[]args){
//创建扫描仪对象并设置扫描仪变量
扫描仪inp=新扫描仪(System.in);
System.out.println(“按任意键启动”);
String key=inp.nextLine();
System.out.println(“\n输入每个项目的金额”);
System.out.println(“最多允许5个输入!\n”);
//初始化计数器和索引变量以在while循环中使用它
int计数器=0;
//创建一个双数组变量,并将限制设置为5
ArrayList编号=新的ArrayList();
//创建一个布尔变量以在while循环中使用它
布尔go=true;
while(go){
字符串值=inp.nextLine().toLowerCase();
int indexOfH=value.indexOf('h');
如果(indexOfH==0 | | indexOfH==value.length()-1){//在开始或结束时验证h
add(Double.parseDouble(value.replace(“h”),“”);
System.out.println(“该值将考虑HST”);
}
计数器++;
如果(计数器==5)
go=假;
}
System.out.println(“HST值:”);
系统输出打印项次(数字);
}
}

将非HST值以与HST值相同的方式存储在数组中有什么问题?如果(containsh){}else{…store it…}?如果限制输入值,则可能需要for循环,而不是while循环。如果无法使用数组,则没有安全的解决方案;数组在Java中是不可压缩的。您可以执行
System.arraycopy(elementData,index,elementData,index+1,size-index)
但是这是不安全的(顺便说一下,这是
ArrayList.add()方法的主要指令)