Android计算字母值

时间:2013-11-21 09:27:54

标签: java android

我正在开发一个Android应用程序...这是一个基于数字命理的应用程序,其中...... AI的值为1-9,然后是JR和SZ,所以每个字母都有值并计算它...我需要显示计算用户名字的结果,例如我的名字是ROSHAN PETER ...我的名字是ROSHAN和姓氏PETER所以值是9 + 6 + 1 + 8 + 1 + 5即总价值是30 ...接下来我的年龄是27岁,所以我需要接受ROSHA,因为它的总值是25,当我计算N的值时它将变为30.所以我需要显示我的当前值是N到30年。假设我的名字是RAM我的总值是9 + 1 + 4所以值是14而我的年龄是27所以我需要计算一次agin的总数,所以它会得到28所以我当前的字母是M .. 我做了这样的代码,但我的问题是......我的名字的值是计算一次..但我需要计算它直到我的年龄来... ...以名称RAM为例...

我在这里更新我的代码..

MainActivity

public void gReport(View V)
{

    long sum17 = 0;

    EditText et1 = (EditText) findViewById (R.id.editText1);
    EditText et2 = (EditText) findViewById (R.id.editText2);
    EditText et3 = (EditText) findViewById (R.id.editText3);
    EditText et7 = (EditText) findViewById (R.id.editText7);        
    EditText et8 = (EditText) findViewById (R.id.editText8);
    EditText et9 = (EditText) findViewById (R.id.editText9);

    sum17 = getSum17(et1.getText().toString());

    Intent i = new Intent(this, FullExplanation.class);

    int day = Integer.parseInt(et7.getText().toString());
    int month = Integer.parseInt(et8.getText().toString());
    int year = Integer.parseInt(et9.getText().toString());

    String s = String.valueOf(FullExplanationEntry.this.getAge(year, month, day));

    i.putExtra("name17", sum17 + "");
    i.putExtra("a", s + "");
    startActivity(i);
}

private long getSum17(String text) {
    // TODO Auto-generated method stub
    long sum17 = 0;
    EditText et7 = (EditText) findViewById (R.id.editText7);        
    EditText et8 = (EditText) findViewById (R.id.editText8);
    EditText et9 = (EditText) findViewById (R.id.editText9);
    int day = Integer.parseInt(et7.getText().toString());
    int month = Integer.parseInt(et8.getText().toString());
    int year = Integer.parseInt(et9.getText().toString());

    String s = String.valueOf(FullExplanationEntry.this.getAge(year, month, day));

    long t = Integer.parseInt(s);
    char[] name17 = new char[text.length()];

    name17 = text.toCharArray();

    for(int i=0; i<text.length(); i++)
    {
        if(sum17 < t) {
            sum17 += value17( name17[i] );
        } else if (sum17 == t) {
            return sum17;
        } else {
            return sum17;
        }
    }
    return sum17;
}

private long value17(char a) {
    // TODO Auto-generated method stub

    switch(a)
    {
       case 'A': 
       return 1;    
       case 'B':
       return 2;
       case 'C':
       return 3;
       case 'D':
       return 4;
       case 'E':
       return 5;
       case 'F':
       return 6;
       case 'G':
       return 7;
       case 'H':
       return 8;
       case 'I':
       return 9;
       case 'J':
       return 1;
       case 'K':
       return 2;
       case 'L':
       return 3;
       case 'M':
       return 4;
       case 'N':
       return 5;
       case 'O':
       return 6;
       case 'P':
       return 7;
       case 'Q':
       return 8;
       case 'R':
       return 9;
       case 'S':
       return 1;          
       case 'T':
       return 2;
       case 'U':
       return 3;
       case 'V':
       return 4;
       case 'W':
       return 5;
       case 'X':
       return 6;
       case 'Y':
       return 7;
       case 'Z':
       return 8;
       default:         
       return 0;

    }

}

public int getAge (int _year, int _month, int _day) {

        GregorianCalendar cal = new GregorianCalendar();
        int y, m, d, a;         

        y = cal.get(Calendar.YEAR);
        m = cal.get(Calendar.MONTH) + 1;
        d = cal.get(Calendar.DAY_OF_MONTH);
        cal.set(_year, _month, _day);
        a = y - cal.get(Calendar.YEAR);
        if ((m < cal.get(Calendar.MONTH) + 1)
                        || ((m == cal.get(Calendar.MONTH) + 1) && (d < cal
                                        .get(Calendar.DAY_OF_MONTH)))) {
                --a;
        }
        if(a < 0)
                throw new IllegalArgumentException("Age < 0");
        return a;
}

1 个答案:

答案 0 :(得分:2)

好吧,我的朋友,你的代码几乎难以理解。但是,今晚我手上有很多时间,所以我顺便通过它。为了理解你想要完成什么,我已经重命名了你的一些变量,但是这个代码应该可以工作而不必改变你的MainActivity之外的任何东西。

public class MainActivity extends Activity
{
    EditText edtFirst, edtMiddle, edtLast, edtDay, edtMonth, edtYear;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        edtFirst = (EditText) findViewById(R.id.editText1);
        edtMiddle = (EditText) findViewById(R.id.editText2);
        edtLast = (EditText) findViewById(R.id.editText3);
        edtDay = (EditText) findViewById(R.id.editText7);        
        edtMonth = (EditText) findViewById(R.id.editText8);
        edtYear = (EditText) findViewById(R.id.editText9);
    }

    public void gReport(View v)
    {
        String first = edtFirst.getText().toString();

        if (first.length() == 0)
        {
            Toast.makeText(this, "Please enter a First Name.", Toast.LENGTH_SHORT).show();
            return;
        }

        try
        {
            int day = Integer.parseInt(edtDay.getText().toString());
            int month = Integer.parseInt(edtMonth.getText().toString());
            int year = Integer.parseInt(edtYear.getText().toString());

            int age = getAge(year, month, day);
            int sum17 = getSum17(first, age);

            Intent i = new Intent(this, FullExplanation.class);
            i.putExtra("name17", sum17);
            i.putExtra("age", age);
            startActivity(i);
        }
        catch (NumberFormatException e)
        {
            Toast.makeText(this, "Please enter Day, Month, and Year", Toast.LENGTH_SHORT).show();
        }
        catch (IllegalArgumentException e)
        {           
            Toast.makeText(this, "Invalid Birthdate", Toast.LENGTH_SHORT).show();
        }
    }

    private int getSum17(String name, int age)
    {
        int sum17 = 0;
        int ct = 0;
        boolean done = false;

        char[] name17 = name.toUpperCase().toCharArray();

        if (name17.length == 0 || value17(name17[0]) > age)
        {
            return 0;
        }

        do
        {
            sum17 += value17(name17[ct]);
            ct++;

            if (ct > name.length() - 1)
            {
                ct = 0;
            }

            if (sum17 + value17(name17[ct]) > age)
            {
                done = true;
            }
        }
        while (!done);

        return sum17;       
    }

    private int value17(char c)
    {
        if (c < 65 || c > 90)
        {
            return 0;
        }

        int v = (c - 64) % 9;
        return v == 0 ? 9 : v;      
    }

    public int getAge(int _year, int _month, int _day)
    {
        GregorianCalendar cal = new GregorianCalendar();
        int y, m, d, a;         

        y = cal.get(Calendar.YEAR);
        m = cal.get(Calendar.MONTH) + 1;
        d = cal.get(Calendar.DAY_OF_MONTH);
        cal.set(_year, _month, _day);
        a = y - cal.get(Calendar.YEAR);
        if ((m < cal.get(Calendar.MONTH) + 1)
            || ((m == cal.get(Calendar.MONTH) + 1) && (d < cal
            .get(Calendar.DAY_OF_MONTH))))
        {
            --a;
        }
        if (a < 0)
            throw new IllegalArgumentException("Age < 0");
        return a;
    }
}

现在,只更新您的MainActivity可以让您运行。但是,如果您愿意,还可以进行以下更改以防止用户导致运行时错误。

在你的布局xml中,在名称的每个EditTexts中,添加以下行,保留字母表之间的空格:

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

在生日的每个EditTexts中,添加:

android:inputType="number"

如果您有任何问题,请告诉我。