英文:
Need to turn char into lower case and replace it in Java
问题
// 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 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 in the while loop
boolean go = true;
while (go) {
String value = inp.nextLine();
value = value.toLowerCase(); // Convert input to lowercase
// 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 the beginning or end
numbers[index] = Double.parseDouble(value.replace("h", ""));
index++;
System.out.println("HST will be taken into 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]);
}
}
}
英文:
So 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 got most of the program working, but I cannot get my code to recognize the upper case H to put the tax in it.
Here is my code, Thx in advance for any information:
// 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;
// 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.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]);
}
}
}
答案1
得分: 0
我将完整的代码内容翻译如下:
// 我会完全删除 `indexOfh`:你可以将一系列语句使用 `||` 进行逻辑“或”操作。这是安全的,因为 `>=` 在 `||` 之前[求值][1]。
// 设置索引值为 "h" 或 "H"
boolean containsh = value.indexOf('h') >= 0
|| value.indexOf('H') >= 0;
// 此外,你可以通过使用 `startsWith` 和 `endsWith` 来更严格地满足你的需求,以忽略中间的 'H' 或 'h'。
// 设置索引值为 "h" 或 "H"
boolean containsh = value.startsWith("h")
|| value.startsWith("H") || value.endsWith("h")
|| value.endsWith("H");
// 另外,你的代码存在资源泄漏,因为你从未关闭 `Scanner`:你可以使用 [try-with-resources][2] 让 Java 来管理这个。结合一些其他简化,得到如下新版本。
// 导入 Scanner 类
import java.util.Scanner;
// 创建类和方法
public class so64283526 {
public static void main(String[] args) {
// 创建 scanner 对象并设置 scanner 变量
try (Scanner inp = new Scanner(System.in)) {
System.out.println("按任意键开始");
System.out.println("\n输入每个项目的金额");
System.out.println("最多允许输入 5 个值!\n");
// 初始化计数器和索引变量以在循环中使用
int counter = 0;
int index = 0;
// 创建双精度数组变量,并将限制设置为 5
double[] numbers = new double[5];
while (counter++ < 5) {
String value = inp.nextLine();
value.toLowerCase();
// 设置索引值为 "h" 或 "H"
boolean containsh = value.startsWith("h")
|| value.startsWith("H") || value.endsWith("h")
|| value.endsWith("H");
if (containsh) { // 验证是否在开头或结尾包含 'h'
numbers[index] = Double
.parseDouble(value.toLowerCase().replace("h", ""));
index++;
System.out.println(
"这个值将计算 HST");
}
}
System.out.println("HST 值:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
}
运行结果:
按任意键开始
输入每个项目的金额
最多允许输入 5 个值!
1
2H
这个值将计算 HST
H3
这个值将计算 HST
h4.7
这个值将计算 HST
5.h6
HST 值:
2.0
3.0
4.7
0.0
0.0
[1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
[2]: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
英文:
I would remove indexOfh
entirely: you can OR
a bunch of statements together. This is safe because >=
is evaluated before ||
.
// Set the index value to "h" or "H"
boolean containsh = value.indexOf('h') >= 0
|| value.indexOf('H') >= 0;
Also, you can make your requirement a little tighter by using startsWith
and endsWith
to ignore an 'H' or 'h' in the middle.
// Set the index value to "h" or "H"
boolean containsh = value.startsWith("h")
|| value.startsWith("H") || value.endsWith("h")
|| value.endsWith("H");
Additionally, you have a resource leak because you never close your Scanner
: you can use try-with-resources to have Java manage this for you. That, along with a few other simplifications, yields a new version like this.
// Import scanner class
import java.util.Scanner;
// Create class and method
public class so64283526 {
public static void main(String[] args) {
// Create scanner object and set scanner variables
try (Scanner inp = new Scanner(System.in)) {
System.out.println("Press any key to start");
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;
// Create a double array variable, and set the limit to 5
double[] numbers = new double[5];
while (counter++ < 5) {
String value = inp.nextLine();
value.toLowerCase();
// Set the index value to "h" or "H"
boolean containsh = value.startsWith("h")
|| value.startsWith("H") || value.endsWith("h")
|| value.endsWith("H");
if (containsh) { // Validate h at beginning or end
numbers[index] = Double
.parseDouble(value.toLowerCase().replace("h", ""));
index++;
System.out.println(
"HST will be taken account for this value");
}
}
System.out.println("HST Values:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
}
Results:
Press any key to start
Enter the amount of each item
Upto 5 inputs are allowed!
1
2H
HST will be taken account for this value
H3
HST will be taken account for this value
h4.7
HST will be taken account for this value
5.h6
HST Values:
2.0
3.0
4.7
0.0
0.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论