如何使程序在Java中检查字母的实例?

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

How to make the program to check the instance of a letter in java?

问题

我正在创建一个现金收银机,我必须使用一个扫描仪,并且只能有5个输入金额。还必须包括 hst(即税额),通过在金额之后或之前只有一个 "h" 来表示。我的问题是程序如何识别我在金额之后或之前放置了 "h"?这似乎只能通过使用字符串变量来完成,那么我该如何实现?我必须将输入存储在一个数组中,所以我已经做到了。

我的代码:

// 导入 Scanner 类
import java.util.Scanner;

// 创建类和方法
class Main {
    public static void main(String[] args) {

        // 声明 Scanner 对象并创建扫描仪变量
        Scanner inp = new Scanner(System.in);
        System.out.println("按任意键开始");
        String key = inp.nextLine();
        System.out.println("\n输入每个项目的金额");
        System.out.println("最多允许 5 个输入!\n");

        // 定义一个 double 数组变量,将限制设置为 5 个输入
        double[] numbers = new double[5];

        // 创建一个 for 循环,以输入任意数字 5 次
        for (int i = 0; i < numbers.length; i++) {

            // 添加一个扫描仪输入,以便用户输入值
            numbers[i] = inp.nextDouble();
        }
    }
}
英文:

I am creating a cash register where I have to use a scanner and can only have 5 input amounts. It has to also include hst and that is by only having a "h" after or before an amount. My question is how would the program recognize that I have put an "h" after or before an amount? This seems to be done only using a string variable, so how would I accomplish that? I have to store the inputs in an array, and so I got that to work.

My Code:

 // Import scanner class
 import java.util.Scanner;

// Create class and method
class Main {
public static void main(String[] args) {

// Declare the scanner object and create 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;Upto 5 inputs are allowed!\n&quot;);

// Define an array double variable, set the limit to 5 inputs
double[] numbers = new double[5];

// Create a for loop to input any numbers 5 times
for (int i = 0; i &lt; numbers.length; i++){

    // Add a scanner input to let the user type out the values
    numbers[i] = inp.nextDouble();
    }
  }
} 

答案1

得分: 1

以下代码要求用户输入5次,只有有效的值才会被放入数组中。有效值是以'h'开头或结尾的值,并且只能出现一次,即'h'同时出现在开头和结尾,或者出现多次均为无效。

public static void main(String[] args) throws ParseException {
    int counter = 1;
    Double[] result = new Double[5];
    
    int index = 0;
    while(counter <= 5) {            
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an Amount ");
        String value = scanner.nextLine();        
        int indexOfH = value.indexOf("h");
        int lastIndexOfH = value.lastIndexOf("h");
        boolean containsHatstartsOrEnd = indexOfH == 0 || indexOfH == (value.length()-1);
        
        if(containsHatstartsOrEnd && indexOfH==lastIndexOfH){ //Validate h at begins or end and should contains only once
            result[index] = Double.parseDouble(value.replace("h", ""));
            index++;
        }
        counter++;
    }
    System.out.println("Printing Valid values");

    for(int i=0; i< result.length; i++) {
        if(result[i]!=null) {
            System.out.println(result[i]);
        }
    }
}

输入与结果

输入一个数额 13.45h
输入一个数额 55h.65
输入一个数额 32h.33h
输入一个数额 h100.23
输入一个数额 h20
打印有效值
13.45
100.23
20.0
英文:

below code asks the user input for 5 times , and only valid values will be in the Array , Vald values are the values with 'h' at start or end and should only occur once. i.e. at 'h' at both end and start or more than once is invalid.

public static void main(String[] args) throws ParseException {
	int counter = 1;
	Double[] result = new Double[5];
	
	int index = 0;
	while(counter &lt;= 5) {			
		Scanner scanner = new Scanner(System.in);
		System.out.print(&quot;Enter an Amount &quot;);
		String value = scanner.nextLine();		
		int indexOfH = value.indexOf(&quot;h&quot;);
		int lastIndexOfH = value.lastIndexOf(&quot;h&quot;);
		boolean containsHatstartsOrEnd = indexOfH == 0 || indexOfH == (value.length()-1);
	
		if(containsHatstartsOrEnd &amp;&amp; indexOfH==lastIndexOfH){ //Validate h at begins or end and should contains only once
			result[index] = Double.parseDouble(value.replace(&quot;h&quot;, &quot;&quot;));
			index++;
		}
		counter++;
	}
	System.out.println(&quot;Printing Valid values&quot;);

	for(int i=0; i&lt; result.length; i++) {
		if(result[i]!=null) {
			System.out.println(result[i]);
		}
	}
}

input & result

Enter an Amount 13.45h
Enter an Amount 55h.65
Enter an Amount 32h.33h
Enter an Amount h100.23
Enter an Amount h20
Printing Valid values
13.45
100.23
20.0

huangapple
  • 本文由 发表于 2020年10月8日 23:48:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64266140.html
匿名

发表评论

匿名网友

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

确定