如何解决如何通过使用replace方法将值存储在新数组中?
我想在数组中存储非“ h”值。因此,为了给您提供背景知识,我需要制作一个基本的收银机,该收银机可接受具有价格的数组中的5个项目。但是,某些项目将包含HST(税)。要知道哪些物品有税,哪些没有税。用户将在输入美元金额之前或之后按h或H。我已经将HST的值存储在数组中,但是如何存储非HST的值?
注意:我尝试按照与我的“ h”值相同的方式执行此操作,但这无法正常工作,这就是为什么我很困惑
我无法使用Arrayslist或任何其他数组方法
样本输入:
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 scanner class
import java.util.Scanner;
// Create class and method
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("Upto 5 inputs are allowed!\n");
// Initialize counter and index variables to use it in the while loop
int counter = 0;
int index = 0;
int index2 = 0;
// Create a double array variable,and set the limit to 5
Double[] numbers = new Double[5];
Double[] numbers2 = 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");
}else{
numbers2[index2] = Double.parseDouble(value.replace("","")); // value.replace is an issue
}
counter++;
if (counter == 5) {
go = false;
}
}
System.out.println("\nHST Values:");
for (int i = 0; i < numbers.length; i++) {
// If there is any absence of values,print the HST values
if (numbers[i] != null) {
System.out.println(numbers[i]);
}
}
System.out.println("\nNon-HST Values:");
for (int x = 0; x < numbers2.length; x++){
if (numbers2[x] != null){
System.out.println(numbers2[x]);
}
}
}
}
解决方法
尝试一下:
我更改的内容:
-
numbers2[index2] = Double.parseDouble(value);
//无需在此处替换任何内容 -
index2++
,我看到您增加index
而不是index2
-
当您打印HST和非HST值时,您无需走到
numbers.length
或numbers2.length
,因为您知道index
和{{1 }},您已经知道每个数组中的值。
如果采用这种方式,则在打印时无需进行空检查。
index2
,
解析非HST值失败的问题通常可以在@Anon's answer above中解决,但是还有一些其他方面可以使代码更简洁:
- 提供常量而不是数组
5
的硬编码长度 - 删除多余的邮件
- 改进逻辑,重命名变量并删除多余的变量
- 改进对用户输入错误的处理
- 使用不区分大小写的正则表达式使用字符串
matches
方法检查项目是否为HST
public static void main(String[] args) {
final int SIZE = 5;
// Create a double array variable,and set the limit
double[] hstItems = new double[SIZE];
double[] nonHstItems = new double[SIZE];
// Create scanner object and set scanner variables
Scanner inp = new Scanner(System.in);
System.out.printf("Enter up to %d HST or non-HST items%n",SIZE);
// Initialize counter and index variables to use it in the while loop
int counter = 0;
int hstCount = 0;
int nonHstCount = 0;
while (counter++ < SIZE) {
String value = inp.nextLine();
try {
if (value.matches("(?i)(h.+|.+h)")) {
hstItems[hstCount++] = Double.parseDouble(value.replaceAll("(?i)h",""));
System.out.println("HST will be taken into account for this value");
} else {
nonHstItems[nonHstCount++] = Double.parseDouble(value);
System.out.println("No HST is taken into account for this value");
}
} catch (Exception ex) {
System.out.printf("Failed to parse value: %s,error: %s,please try again%n",value,ex.getMessage());
counter--;
}
}
System.out.println("\nHST Values:");
for (int i = 0; i < hstCount; i++) {
System.out.println(hstItems[i]);
}
System.out.println("\nNon-HST Values:");
for (int i = 0; i < nonHstCount; i++) {
System.out.println(nonHstItems[i]);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。