如何在Java中将值存储在数组中?

huangapple go评论73阅读模式
英文:

How would I store values in an array in java?

问题

我想在一个数组中存储非“h”值。所以给你一些背景,我需要制作一个基本的现金注册机,它接受一个包含价格的数组中的5个项目。然而,有些项目将包括HST(税金)。为了知道哪些项目有税收,哪些没有。用户将在输入美元金额之前或之后按下h或H。我已经将带有HST的值存储在一个数组中,但是如何存储非HST的值呢?

示例输入:

4.565H
H2.3435
4.565h
5.234
5.6576h

示例输出:

HST 值:
4.565
2.3435
4.565
5.6576

非-HST 值:
5.234


我的代码:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // 创建扫描器对象并设置扫描器变量
        Scanner inp = new Scanner(System.in);
        System.out.println("按任意键开始");
        String key = inp.nextLine();
        System.out.println("\n输入每个项目的金额");
        System.out.println("最多允许输入5个值!\n");

        // 初始化计数器和索引变量以在 while 循环中使用
        int counter = 0;
        int index = 0;
 
        // 创建一个 double 数组变量,并将限制设置为 5
        double[] numbers = new double[5];

        // 创建一个布尔变量以在 while 循环中使用
        boolean go = true;

        while (go) {           
            String value = inp.nextLine();      
            value = value.toLowerCase();
  
            // 将索引值设置为“h”或“H”
            int indexOfh = value.indexOf('h');

            boolean containsh = (indexOfh == 0 || indexOfh == value.length() - 1);
            
            if (containsh) { // 验证 h 在开头或结尾
                numbers[index] = Double.parseDouble(value.replace("h", ""));
                index++;
                System.out.println("此值将考虑 HST");
            }

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

        System.out.println("HST 值:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}
英文:

I want to store non "h" values in an array. So to give you a background, I need to make a basic cash register that accepts 5 items in an array with a price on them. Nevertheless, some items will have HST(tax) included with them. To know which items have tax and which don't. The user will press h or H before or after entering the dollar amount. I have stored the values with the HST in an array, but how would I store the non-HST values?

Sample input:

4.565H
H2.3435
4.565h
5.234
5.6576h

Sample Output:

HST Values:
4.565
2.3435
4.565
5.6576
Non-HST Values
5.234

My Code:

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(&quot;Press any key to start&quot;);
String key = inp.nextLine();
System.out.println(&quot;\nEnter the amount of each item&quot;);
System.out.println(&quot;Up to 5 inputs are allowed!\n&quot;);
// 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 &quot;h&quot; or &quot;H&quot;
int indexOfh = value.indexOf(&#39;h&#39;);
boolean containsh = (indexOfh == 0 || indexOfh == value.length() - 1);
if (containsh) { //Validate h at beginning or end
numbers[index] = Double.parseDouble(value.replace(&quot;h&quot;, &quot;&quot;));
index++;
System.out.println(&quot;HST will be taken account for this value&quot;);
}
counter++;
if (counter == 5) {
go = false;
}
}
System.out.println(&quot;HST Values:&quot;);
for (int i = 0; i &lt; numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

答案1

得分: 1

我建议您改用ArrayList

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

class Main {
    public static void main(String[] args) {
        // 创建扫描器对象并设置扫描器变量
        Scanner inp = new Scanner(System.in);
        System.out.println("按任意键开始");
        String key = inp.nextLine();
        System.out.println("\n输入每个项目的金额");
        System.out.println("最多允许输入5个金额!\n");

        // 初始化计数器和索引变量以在 while 循环中使用
        int counter = 0;

        // 创建一个双精度数组变量,并将限制设置为5
        ArrayList<Double> numbers = new ArrayList<Double>();

        // 创建一个布尔变量以在 while 循环中使用
        boolean go = true;

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

            if (indexOfH == 0 || indexOfH == value.length() - 1) { // 验证是否在开头或结尾处有 'h'
                numbers.add(Double.parseDouble(value.replace("h", "")));
                System.out.println("这个值将考虑HST");
            }

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

        System.out.println("HST 值:");
        System.out.println(numbers);
    }
}
英文:

I would suggest you to work with ArrayList instead:

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(&quot;Press any key to start&quot;);
String key = inp.nextLine();
System.out.println(&quot;\nEnter the amount of each item&quot;);
System.out.println(&quot;Up to 5 inputs are allowed!\n&quot;);
// 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&lt;Double&gt; numbers = new ArrayList&lt;Double&gt;();
// 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(&#39;h&#39;);
if (indexOfH == 0 || indexOfH == value.length() - 1) { //Validate h at beginning or end
numbers.add(Double.parseDouble(value.replace(&quot;h&quot;, &quot;&quot;)));
System.out.println(&quot;HST will be taken account for this value&quot;);
}
counter++;
if (counter == 5)
go = false;
}
System.out.println(&quot;HST Values:&quot;);
System.out.println(numbers);
}
}

答案2

得分: 0

你只需要两个数组,一个用于存储 H 值,另一个用于存储非 H 值。为什么不使用列表呢?使用列表会更好。

然而,如果要求使用数组,只需通过检查 'H' 值将 h 值存储起来,如果 'H' 的索引为 -1,表示该值不包含 h,然后将这些值添加到另一个数组中,并在最后一起打印出来。

英文:

You just need two arrays, one for H values and the other for non H. Why not use lists? Lists would be better

However, if the requirement is arrays, just store the h values by checking 'H' values like you have, and if index of 'H' is -1 that means the value does not contain h, then just add those to the other array and print it out as well at the end.

huangapple
  • 本文由 发表于 2020年10月10日 04:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64287142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定