为什么我的变量没有增加?

时间:2015-10-06 15:37:55

标签: java

好的,所以我的老师要求我们让用户输入一组字母和数字。所以我完成了所有工作,除了我有一个问题,我的变量不会增加。这是我的代码......

import java.util.Scanner;

public class Find {

    public static int LetterAdd = 0;

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int Number;
        String Words;

        System.out.print ("Enter some letters: ");
        Words = scan.next();

        System.out.print ("Enter a number: ");
        Number = scan.nextInt();

        Find (Words, Number);
    }

    public static void Find (String Words, double Number)
    {

        for (int Counter = 0; Counter < Words.length(); Counter++)
        {
            Words.toUpperCase();
            String X = Words;
            String X2;
            X2 = X.substring(Counter, Counter + 1);

            if (X2 == "A") {
                LetterAdd += 1;
            } else if (X2 == "B") {
                LetterAdd += 2;
            } else if (X2 == "C") {
                LetterAdd += 3;
            } else if (X2 == "D") {
                LetterAdd += 4;
            } else if (X2 == "E") {
                LetterAdd += 5;
            } else if (X2 == "F") {
                LetterAdd += 6;
            } else if (X2 == "G") {
                LetterAdd += 7;
            } else if (X2 == "H") {
                LetterAdd += 8;
            } else if (X2 == "I") {
                LetterAdd += 9;
            } else if (X2 == "J") {
                LetterAdd += 10;
            } else if (X2 == "K") {
                LetterAdd += 11;
            } else if (X2 == "L") {
                LetterAdd += 12;
            } else if (X2 == "M") {
                LetterAdd += 13;
            } else if (X2 == "N") {
                LetterAdd += 14;
            } else if (X2 == "O") {
                LetterAdd += 15;
            } else if (X2 == "P") {
                LetterAdd += 16;
            } else if (X2 == "Q") {
                LetterAdd += 17;
            } else if (X2 == "R") {
                LetterAdd += 18;
            } else if (X2 == "S") {
                LetterAdd += 19;
            } else if (X2 == "T") {
                LetterAdd += 20;
            } else if (X2 == "U") {
                LetterAdd += 21;
            } else if (X2 == "V") {
                LetterAdd += 22;
            } else if (X2 == "W") {
                LetterAdd += 23;
            } else if (X2 == "X") {
                LetterAdd += 24;
            } else if (X2 == "Y") {
                LetterAdd += 25;
            } else if (X2 == "Z") {
                LetterAdd += 26;
            }
        }
        System.out.println ("The sum of your letters plus your number is: " + (LetterAdd + Number));
    }
}

0 个答案:

没有答案