简单的单词算术?

时间:2016-01-02 05:32:14

标签: java math logic word addition

package pls;
import java.util.Scanner;
public class pls {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        int count = 0;
        int amount;
        int amount2;
        int[] Array = new int[100];
        int[] Array1 = new int[100];
        int total;
        char b;
        String bad = "";
        String finalA = "";
        System.out.println("Input your first word/letter (must be upper case): ");
        String first = keyboard.next();
        amount = first.length();
        System.out.println("Input your second word/letter (must be upper case): ");
        String second = keyboard.next();
        amount2 = second.length();

        while (amount < amount2)//to add on 'A' to first word
        {
        first = 'A' + first;
        amount++;
        }

        while (amount2 < amount)//to add on 'A' to second word
        {
        second = 'A' + second;
        amount2++;  
        }

        for (int i = 0; i < amount; i++) {
            Array[i] = (int)first.charAt(i) - 65;
        }

        for (int i = 0; i < amount2; i++) {
            Array1[i] = (int)second.charAt(i) - 65;
        }

        for (int i = amount; i >= 0; i--) {
            total = Array[amount] + Array1[amount2];    

                if (total > 25) {
                    int pls = total % 26;
                    b = (char)(pls + 65);
                    finalA = b + finalA;
                    total = total / 26;
                    b = (char)(total + 65);
                    finalA = b + finalA;
                    total = pls + total;
                    b = (char)(total + 65);
                    finalA = b + finalA;

                }
                else {
                    b = (char)(total + 65);
                    finalA = b + finalA;
                }
                amount--;
                amount2--;
                }

        finalA = finalA.substring(0, finalA.length()-1);

        while (finalA.charAt(0) == 'A') {
            finalA = finalA.substring(0, 0) + finalA.substring(0 + 1);
        }
        System.out.println(finalA);
    }       
    }

我需要制作一个程序,让用户可以添加从右到左的单词。他们会给出数值,然后你可以添加它们。以下是一些例子:CAT&amp; DOG制作FOZ,MOM&amp; DAD制作POP,ABA&amp;制作BA,BCD&amp; BC制造BDF。

现在到目前为止,我已经设法让它发挥作用,但是下一个让我搞砸了... ZZZ + ZZZ = BZZY ..这就是这部分程序的解释:&# 34;例如,Z + Z = BY(因为Z + Z = 50,50模26是24,它是Y,并且进位是你将50除以26得到的商,即1,表示B )&#34。我感谢任何帮助!在此先感谢!(代码在顶部,因为错误。)

1 个答案:

答案 0 :(得分:0)

在循环中,您只需要为每个传递创建一个字符,即使用mod运算符计算的字符。

另一个计算应该将除法存储为进位。

总数必须包括上一次计算中的字母和进位。

完成后,您不必删除finalA的最后一个字符。