如何声明一个包含四个字符的字符串以及使用charat()来比较每个单独的字符 [英] How do I declare a string of four characters as well as using charat() to compare each individual character

查看:139
本文介绍了如何声明一个包含四个字符的字符串以及使用charat()来比较每个单独的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序将4位十六进制数转换为十进制数。应指示用户输入4位十六进制数字。应输出相应的十进制数(0到65,535)。请注意,从左到右的4位十六进制数的位值是4096,256,然后是16,然后是1.必须输入十六进制数字作为字符串,然后使用charAt(N)方法比较每个字符到'A','B','C','D','E','F'和'0'到'9',并将数字的值乘以相应的位置值到总数。



例如,1A3016 = 1 * 4096 + 10 * 256 + 3 * 16 + 0 * 1 = 670410



首先,提示并输入一个包含四个字符的字符串。



如果输入不是四个字符,请输出相应的信息并退出。



使用if语句,每个语句都表示如果第二个字符等于'A',则第二个数字的值为256 * 10。如果任何字符不是'0' - '9'或'A' - 'F',请打印出相应的消息并退出。



为了比较每个字符输入字符串为char,使用==,并确保使用单引号而不是双引号。



您不能使用任何内置转换方法这个节目。



我尝试过:



public static void main(String [] args){

Scanner sc = new Scanner(System.in);

String hexdecimal;

int characters;

System.out.println(输入一个四位十六进制数字:);

十六进制= sc.nextLine();

< br $>










}





}

解决方案

你可以尝试更多。

开始将单个十六进制字符转换为数字。

提示:如果是hexadecima l charachter,说 h 介于'0''9'那么相应的十进制数是(h-'0')。另一方面,如果它在'A''F'之间,那么相应的数字是(h-'A'+ 10)


很难知道你的例子是什么:

< blockquote class =quote>

Quote:

例如,1A3016 = 1 * 4096 + 10 * 256 + 3 * 16 + 0 * 1 = 670410



- 你必须翻译4个十六进制数字,但例子是6.

- 公式只有4位数。

- 结果超出范围(0到65,535)并且不是公式的结果。



C语言具有将字符串转换为数字的功能,如果字符串格式正确,它们直接完成工作。

1A3016必须格式化为0x1A3016



如果您不能使用该功能,我建议建立一个将单个十六进制数转换为十进制的函数。


Write a program to convert a 4 digit hexadecimal number into decimal. The user should be instructed to enter a 4-digit hexadecimal number. The corresponding decimal number (0 to 65,535) should be output. Note that the place values for a 4-digit hexadecimal number from left to right are 4096, 256, then 16, then 1. You must input the Hexadecimal number as a String, then use the charAt(N) method to compare each individual character to 'A', 'B', 'C', 'D', 'E', 'F' and '0' through '9', and add the value of the digit times the corresponding place value to the total.

For example, 1A3016 = 1*4096+10*256+3*16+0*1 = 670410

First, prompt for and input a String of four characters.

If the input is not four characters, print out an appropriate message and exit.

Use if statements, each meaning something like "If the second character is equal to 'A', the value of the second digit is 256 * 10". If any character is not '0'-'9' or 'A'-'F', print out an appropriate message and exit.

In order to compare each character of the input String to a char, use ==, and be sure to use single quotes rather than double quotes.

You cannot use any built-in conversion methods for this program.

What I have tried:

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String hexdecimal;
int characters;
System.out.println("Enter a four digit HexDecimal number:");
hexdecimal = sc.nextLine();






}


}

解决方案

Well you could try more.
Start converting a single hexadecimal character to a number.
Hint: if hexadecimal charachter, say h is between '0' and '9' then the corresponding decimal number is (h-'0'). On the other hand, if it is between 'A' and 'F' then the corresponding number is (h-'A'+10).


Difficult to know what is what in your example:

Quote:

For example, 1A3016 = 1*4096+10*256+3*16+0*1 = 670410


- you have to translate a 4 hex digits but example is 6.
- the formula is 4 digit only.
- and result is out of range (0 to 65,535) and is not the result of formula.

C languages have functions to convert strings to numeric, if string is properly formatted, they do the job directly.
"1A3016" have to be formatted as "0x1A3016"

If you can't use that function, I recommend to build a function that translate a single hex digit to decimal.


这篇关于如何声明一个包含四个字符的字符串以及使用charat()来比较每个单独的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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