java - 从 char 获取 ASCII 不起作用?

标签 java ascii

我正在创建一个遵循以下格式的票务系统:

Write a Java program called Ticket.java that prompts (asks) the user to enter a ticket number. The format of a valid ticket is CCCDD[C][C] where D = a digit and C = a character and [C] means that the character at this position is optional. The three characters at the start may be any characters (letters). If there is only one optional character at the end of the ticket String, then that character may only be 'S' or 'M' If there are two optional characters at the end of the ticket String, then the characters can only be 'S' and 'M' in that order.

示例输入是 LMN25S

现在要获取价格,我必须将前三个字符格式化为 ASCII,将它们加在一起并与下面的规则进行比较。

If the first three characters are "less than" JKL, the base price is $50, if the first three characters are "greater than or equal to" JKL and "less than" TAF, the base price is $100, else the base price, is $150

问题是,问题指南(上面)规定,如果商品小于 TAF 但等于或大于 JKL,则价格必须为 100 美元。但这些项目等于以下内容:

    char j = 'J';char k = 'K';char l = 'L';
    int JKL = (int)j + (int)k + (int)l;
    char t = 'T';char a = 'A';char f = 'F';
    int TAF = (int)t + (int)a + (int)f;
    Print("JKL:" + JKL + "\nTAF:" + TAF);

打印 JKL:225、TAF:219,从而使规则为空,因为 TAF 根据即时转换的方式较小。

这是我的完整代码,以便更好地理解

    import java.util.Scanner;

public class Ticket {
    public static void main(String[] args) {
        Print("Please enter a ticket number:");
        String ticket = getInput();
        if(countValid(ticket) && checkCharacters(ticket) && checkDigits(ticket) && checkLastCharacter(ticket)) {
            Print("Valid!");
            Print("Ticket pricing is " + PrintPrice(ticket));
        }
    }

    public static double PrintPrice(String input) {
        int first = (int)input.charAt(0);
        Print("" + first);
        int second = (int)input.charAt(1);
        Print("" + second);
        int third = (int)input.charAt(2);
        Print("" + third);
        double price = 0;
        Print("Total:" + (first+second+third) + "");
        //225
        char j = 'J';char k = 'K';char l = 'L';
        int JKL = (int)j + (int)k + (int)l;
        char t = 'T';char a = 'A';char f = 'F';
        int TAF = (int)t + (int)a + (int)f;
        Print("JKL:" + JKL + "\nTAF" + TAF);

        char one = input.charAt(0);
        char two = input.charAt(1);
        char three = input.charAt(2);

        int value = (int)one + (int)two + (int)three;
        Print("next total:" + value);
        if(value < JKL) {
            price += 50;
        }else if(value >= JKL && value < TAF) {
            price += 100;
        }else{
            price += 150;
        }
        String discountString = "" + Character.getNumericValue(input.charAt(3)) + Character.getNumericValue(input.charAt(4));
        Print(discountString);
        if(input.length() > 5) {
            if(input.charAt(5) == 'S') {
                price+= 17.45;
            }else if(input.charAt(5) == 'M') {
                price+=29.70;
            }
        }else if(input.length() > 6) {
            //price+=41.25;
        }

        double discount = Double.parseDouble(discountString);
        Print((price * (discount/100)) + "");
        //price = price - (price * (discount/100));
        //price = price * factor;
        return price;
    }


    public static boolean checkLastCharacter(String input) {
        boolean success = false;
        int successCount = 0;
        if(input.length() == 5) {
            success = true;
        }else{
            if(input.length() == 6) {
                char temp = input.charAt(5);
                if(temp == 'S' || temp == 'M') {
                    success = true;
                }else{
                    success = false;
                    Print("Failed due to character not matching 'S' or 'M', '" + temp + "' is invalid");
                }
            }else if(input.length() == 7) {
                char temp = input.charAt(5);
                char temp1 = input.charAt(6);
                if(temp == 'S' && temp1 == 'M') {
                    success = true;
                }else{
                    success = false;
                    Print("Failed due to characters not matching 'SM', '" + temp + temp1 + "' is invalid");
                }
            }else{
                Print("Input characters overloaded! Only accepts up to 7 inputs");
            }
        }
        return success;
    }

    public static boolean countValid(String input) {
        boolean success = false;
        if(input.length() < 9 && input.length() > 4) {
            success = true;
        }else{
            Print("The count is invalid, only numbers between 5 and 7 are accepted!");
        }
        return success;
    }

    public static boolean checkCharacters(String input) {
        boolean success = false;
        int successCount = 0;
        for(int i = 1; i < 3; i++) {
            char temp = input.charAt(i);
            if(Character.isLetter(temp)) {
                successCount++;
            }
        }
        if(successCount == 2) {
            success = true;
        }else{
            Print("First three characters must be letters!");
        }
        return success;
    }

    public static boolean checkDigits(String input) {
        boolean success = false;
        int successCount = 0;
        for(int i = 3; i < 5; i++) {
            char temp = input.charAt(i);
            if(Character.isDigit(temp)) {
                successCount++;
            }
        }
        if(successCount == 2) {
            success = true;
        }else{
            Print("The inputs after 3 letters must be 2 digits!");
        }
        return success;
    }

    //Easier way to print to system.out
    public static void Print(String input) {
        System.out.println(input);
    }

    //Gets the input from a scanner
    public static String getInput() {
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }
}

请忽略所有各种打印,仅用于测试目的。我的转换是否正确,或者问题规则是否正确,但我有转换错误/格式错误

编辑:下面的完整问题陈述以获取帮助:

The user may enter a ticket String of any length. The valid length of a ticket is between 5 and 7 depending on the conditions above. Any ticket that is not in this length range is automatically invalid. A ticket that is invalid for any reason will result in a message being displayed to the screen saying that the ticket is invalid, and no processing will be done with that ticket. This includes not showing any cost. Every effort should be made to give the exact reason why the ticket is invalid. For example, wrong length, starts with an invalid character, or ends with an invalid character. These are not the only reasons why a ticket is invalid, part of your task is to list all the conditions that make a ticket invalid and write code to cover those cases. After checking the length of the user input, your program needs to check that the first three characters in the String are indeed letters ('A' - 'Z' and/or 'a' - 'z', user input must be case insensitive). If anyone of the first three characters are not letters, then an appropriate error message is displayed to the screen and the program closes. After this check is cleared, then your program must check that the next 2 characters in the String are indeed digits (between 0 - 9 ). Again, if there are not 2 digits, then the program displays an appropriate error message and closes. Hint: consider that when we enter 0 at the keyboard we are entering the character '0' which has an ASCII/Unicode of 48. So what we are really storing is the base 10 number 48. We need to convert this from character 0 to integer 0. 1 is actually character '1' with ASCII/Unicode v alue 49 and so on. f the ticket is valid, then the cost of the ticket is calculated and displayed to the screen. If the first three characters are "less than" JKL, the base price is $50, if the first three characters are "greater than or equal to" JKL and "less than" TAF, the base price is $100, else the base price, is $150 The 2 digit characters have to be converted into a base 10 integer. This is the discount to be applied to the final cost of the ticket. If the 2 digits were, for example, 25 then this would be converted in to the base 10 number twenty-five. This would mean that there is a 25% discount on the final ticket price, final meaning after we have processed the 'S' and/or 'M' optional characters, if they are present. The character 'M' at the end of the ticket String, if it is the only optional character adds $29.70 to the cost, of the ticket. The character 'S' at the end of the ticket String, if it is the only optional character adds $17.45 to the cost, of the ticket. The characters 'S' and 'M', in that order, if there are the optional characters at the end of the ticket String add $41.25 to the cost, of the ticket.

最佳答案

也许他们正在谈论字典顺序,而不是字符的总和?

那么 JFK 不如 TAF,PAM = 100$。

在这种情况下,您只需比较字符串: (感谢@Serge Ballesta 的回答)

// to upperCase is a safety to match the case insensitiv requirement
String priceString = input.substring(0, 3).toUpperCase();
double price;
if (priceString.compareTo("JKL") < 0){
    price = 50.;
}
else if (priceString.compareTo("TAF") < 0){
    price = 100.;
}
else{
    price = 150.;
}

不要忘记折扣(数字)和最后两个字符。 祝你好运!

关于java - 从 char 获取 ASCII 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195445/

相关文章:

Java - 在 GUI 中使用线程

python - 如何在 Python 中获取 ASCII 西里尔字符代码?

java - 从动态加载的 jar 文件中加载具有父类(super class)的 Java 类

java - 如何在netbeans设计器中将按钮与键绑定(bind)

PHP preg_replace : unicode modifier for ascii strings

ruby - 如何摆脱 ruby 中的非ascii字符

c - 在 C 中使用 "\n"和 "\0"以外的特殊字符

javascript - 从 LWIP 服务器响应中检索 16 位整数

java - 在微服务架构中聚合原始数据分析的位置

java - org.hibernate.hql.internal.ast.tree.Node 在 jmap 堆转储中意味着什么