久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何獲得循環中產生的 char 值的總和?

How to get sum of char values produced in a loop?(如何獲得循環中產生的 char 值的總和?)
本文介紹了如何獲得循環中產生的 char 值的總和?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

對不起,如果標題具有誤導性或令人困惑,但這是我的困境.我正在輸入一個字符串,并想為字母表中的每個大寫字母(A=1,.. Z=26)分配一個值,然后添加該字符串中每個字母的值.

Sorry if the title is misleading or is confusing, but here is my dilemma. I am inputting a string, and want to assign a value to each capitalized letter in the alphabet (A=1, .. Z=26) and then add the values of each letter in that string.

示例: ABCD = 10(因為 1 + 2 + 3 + 4)

Example: ABCD = 10 (since 1 + 2 + 3 + 4)

但我不知道如何將字符串中的所有值相加

But I don't know how to add all the values in the string

注意:這僅適用于大寫字母和字符串

NOTE: This is only for capitalized letters and strings

public class Test {

    public static void main(String[] args) {

        Scanner scannerTest = new Scanner(System.in);
        System.out.println("Enter a name here: ");

        String str = scannerTest.nextLine();

        char[] ch = str.toCharArray();
        int temp_integer = 64;

        for (char c : ch) {
            int temp = (int) c;

               if (temp <= 90 & temp >= 65){
            int sum = (temp - temp_integer);
            System.out.println(sum);
        }   
      }
    }
}

所以,如您所見,我打印出每次循環的總和,含義:如果我輸入AB",輸出將是1和2.

So, as you can see I print out the sum for each time its looped, meaning: if I input "AB", the output will be 1 and 2.

但是,我想更進一步,將這兩個值加在一起,但我很困惑,有什么建議或幫助嗎?(注意:這不是作業或任何東西,只是練習問題集)

However, I want to go a step further, and add these two values together, but I'm stumped, any suggestions or help? (NOTE: this is not a assignment or anything, just practising problem sets)

推薦答案

我更喜歡使用字符文字.你知道范圍是AZ(126),所以你可以減去'A'從每個 char 開始(但您需要添加 1,因為它不是從 0 開始的).我還會在輸入行上調用 toUpperCase .類似的,

I would prefer to use the character literals. You know that the range is A to Z (1 to 26), so you can subtract 'A' from each char (but you need to add 1 because it doesn't start at 0). I would also call toUpperCase on the input line. Something like,

Scanner scannerTest = new Scanner(System.in);
System.out.println("Enter a name here: ");
String str = scannerTest.nextLine().toUpperCase();
int sum = 0;
for (char ch : str.toCharArray()) {
    if (ch >= 'A' && ch <= 'Z') {
        sum += 1 + ch - 'A';
    }
}
System.out.printf("The sum of %s is %d%n", str, sum);

我用你的例子測試過

Enter a name here: 
ABCD
The sum of ABCD is 10

這篇關于如何獲得循環中產生的 char 值的總和?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲精品中文字幕av | 国产福利在线 | 国产久 | 午夜资源| 国产一区在线免费观看 | 日韩视频免费 | 天天操夜夜操 | 中文字幕日韩av | 91精品久久久久久久久中文字幕 | 日本午夜网 | 中文字幕专区 | 成人精品在线观看 | 亚洲嫩草| 国产ts人妖一区二区三区 | 中文字幕亚洲精品 | 成人在线视频网址 | 91网站在线观看视频 | 欧美 日本 国产 | 国产精品久久精品 | 日本三级全黄三级a | 美美女高清毛片视频免费观看 | 久久久久成人精品亚洲国产 | 亚洲综合精品 | 一区二区三区四区免费在线观看 | 成人深夜福利网站 | 日韩免费高清视频 | 久久久久久久久久久久久九 | 在线日韩欧美 | 亚洲一区二区三区四区在线观看 | 黄色毛片一级 | 99久久精品免费看国产高清 | 欧美激情精品久久久久 | 成人av在线播放 | 亚洲一区二区三区四区五区中文 | 欧美不卡一区二区 | 欧美精品第一页 | 亚洲精品视频在线看 | 精品国产乱码久久久久久闺蜜 | 日韩一区二区三区av | 日韩精品一区二区三区 | 久久久久亚洲精品国产 |