使用charAt方法,不会将它们添加为Int,也不会打印为字符串。会解释得更好 [英] Using charAt method, won't add them as an Int, and wont print as string. Will explain better

查看:748
本文介绍了使用charAt方法,不会将它们添加为Int,也不会打印为字符串。会解释得更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这是我的代码:

Alright, so here is my code:

import java.util.Scanner;

public class CarRental {

    public static String model;
    public static int letternum;
    public static String plate;
    public static String letter;
    public static int total;              
    public static String alphabet = "abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //System.out.println("Car Model:");
        //model = input.nextLine();
        System.out.println("License Plate: ");
        plate = input.nextLine();

        char one = plate.charAt(0);
        char two = plate.charAt(1);
        char three = plate.charAt(2);
        total = one + two + three;
        letternum = total % 24;

        char letter = alphabet.charAt(letternum);

        System.out.println("" + letter + total);

    }
}

这是怎么回事,我正试图让我的牌照输入并取出0,1和2处的角色。牌照中的三个字母。然后,我试图获取他们的ASCII值,将它们全部加在一起并将它们设置为inttotal。然后要找到一个应该在总值之前的字母,我通过使用%6找到总数的剩余部分。然后它将取该值,无论它是多少,说它是4,它将需要字符串alphabet中的第四个字母,并将其设置为charletter。然后它应该做的是打印出字母后跟ASCII值的总和。

What is going on is this, I'm trying to make it take my license plate input and take the characters at the places for 0, 1, and 2. Which in a license plate would be the three letters. Then, i'm trying to take their ASCII Values, add them all together and set them to the int "total". Then to find a Letter that is supposed to be in front of the total value, I find the remainder of the total by using % 6. Then it will take that value, and whatever number it is, say it is 4, it will take the 4th letter in the string "alphabet" and set that to a the char "letter". Then what it should do is print out the letter followed by the totals of the ASCII Value.

以下是我输入的预期结果的示例,其后是实际结果。

Here is an example of what is my input with expected outcome, followed by its ACTUAL outcome.


车牌:CPR 607

License Plate: CPR 607

输出:E836

我的输出与完全相同的车牌是:

My output with the exact same license plate is:


车牌:CPR 607

License Plate: CPR 607

n229

我是不确定我做错了什么,但我最好的线索是它是一个char的事实,它将它视为它的ASCII值,而不是它的String值(我实际上试图得到)

I'm not sure what I'm doing wrong, but my best clue is that the fact that it is a char, it is treating it like its ASCII Value, rather than its String value(which i'm actually trying to get)

如果有人可以提出一些提示,那将是一个很大的帮助。不一定是我可以躲过的代码,但我应该如何正确地做到这一点!

If anyone could suggest some tips, it would be a great help. Not necessarily code I can just leech off of, but how I should go about doing this the right way!

推荐答案

你想要取字符串的第二部分(带有三个数字)并将其添加到总数中。您可以使用以下值获取该值:

You want to take the second part of the string (with the three numbers) and add it to your total. You can take that value with:

Integer.parseInt(plate.split()[1])

Integer.parseInt(plate.split(" ")[1])

这篇关于使用charAt方法,不会将它们添加为Int,也不会打印为字符串。会解释得更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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