如何使程序检查Java中字母的实例? [英] How to make the program to check the instance of a letter in java?

查看:166
本文介绍了如何使程序检查Java中字母的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个收银机,必须使用扫描仪,并且只能有5个输入金额.它也必须包括hst,即只包含一个"h".一定数量之后或之前.我的问题是程序将如何识别出我已经输入了"h"?之前还是之后?这似乎只能使用字符串变量来完成,那么我将如何实现呢?我必须将输入存储在数组中,这样我才能正常工作.

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.

我的代码:

 // 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("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");

// 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 < numbers.length; i++){

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

下面的代码

推荐答案

要求用户输入5次,只有有效值在Array中,Vald值是在开头或结尾带有'h'的值并且只能出现一次.也就是说,在结尾处和开始处都在"h"处,或者多次无效.

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 <= 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]);
        }
    }
}

输入和结果

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

这篇关于如何使程序检查Java中字母的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆