word显示字符和unicode值的总和? [英] word display sum of characters unicode value?

查看:137
本文介绍了word显示字符和unicode值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理将接受一个单词并显示包含其角色的unicode值的数字总和的作业。



我正在考虑声明并初始化变量某些字符如 hello d1 = h ... d2 = e ...等等。然后将字符转换为int,但是有更简单的方法吗?请帮忙!谢谢!!

Working on an assignment that will take a word and display the sum of the numbers comprising its character's unicode values.

I am thinking about declaring and initialize the variable of some of the characters like hello d1=h...d2=e...and so on. then convert character to int, but is there an easier route to do? Please help! Thanks!!

推荐答案

从你的问题我推断出的内容,你想要在单词中加上所有数字。这就是你如何实现这个目标:



您可以只获取字符串输入然后使用for循环查找其所有字符的总和然后最后打印它,这是相同的简单逻辑:





From your question what i have deduced that, you want to add up all numbers in the word.Here is how you can achieve that:

You can just get the string input and then , use a for loop to find sum of all of its characters and then finally print it , here is the simple logic for the same:


String str="1234567";
       int sum=0;
       for(int i=1;i<str.length();i=i+2){
           sum+=(str.charAt(i)-'0');
       }
       System.out.println(sum);





您可以从用户那里获取数字,也可以这样做







You can take input from user for the number and can do the same


BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter number : ");
    String number=null;
    try{
        number = reader.readLine();
    }
    catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
    int sum=0;
    for(int i=1;i<number.length();i=i+2){
        sum+=(number.charAt(i)-'0');
    }
    System.out.println(sum);





在这两种情况下,您都可以得到数字的总和。这既简单又正确。



没有胡扯没有模糊......你就是嗡嗡声..:P



我希望这会有所帮助。



最好的问候



In both cases you will get the sum of the numbers. This is both easy and correct.

No huzz no fuzz.. and you kill the buzz.. :P

I hope that will help.

Best Regards


这个问题并不像看起来那么简单。只是对 int 进行类型转换应该可以工作,但在意识形态上是不正确的。 Unicode在语言和平台API中的支持应该是透明的:它不应该依赖于一个或另一个平台内部使​​用的任何特定UTF的知识(例如JVM或每个实现所基于的OS)。



此外,在大多数UTF(32位除外)中,字符占用不同的字节数。比方说,在UTF16LE中,大多数实际使用的字符由16位字表示,但有些在BMP之上(适合16位表示的基本平面)使用两个这样的字(称为 surrogage 对)。如果你假设某些字符的特定按位表示,你可以得到一些字符的正确结果而对其他字符则是错误的。



所以,这个方面应该委托给API。在Java中,此类API是 codePointAt 以及其他名为* codePoint *或与代码点参数相关的方法。你可以在这里找到所有这些: http ://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#codePointAt-char:A-int- [ ^ ]。



-SA
The problem is not as simple as it may seem. Just typecasting to int is supposed to work but is ideologically incorrect. The support of Unicode in languages and platform API's should be transparent: it should not rely on the knowledge of any particular UTF which is internally used by one or another platform (such as JVM or the OS each implementation is based on).

Also, in most UTFs (except 32-bit ones), the character takes different number of bytes. Say, in UTF16LE, most practically used characters are represented by a 16-bit words, but some are above the BMP (base plane which fits in 16-bit representation) uses two such words (called surrogage pairs). If you assume some particular bitwise representation of characters, you can get correct results for some characters and wrong for others.

So, this aspect should be delegated to API. In Java, such API is codePointAt and other methods named "*codePoint*" or related to code point parameters. You can find all of them here: http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#codePointAt-char:A-int-[^].

—SA


这篇关于word显示字符和unicode值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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