首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android添加首名、中名和姓首字符的值

Android添加首名、中名和姓首字符的值
EN

Stack Overflow用户
提问于 2013-11-30 11:58:18
回答 1查看 1K关注 0票数 1

我正在开发一个安卓app..in,它以用户的FirstName、middlename和LastName作为输入。而...the应用程序是基于数字的。所以每个字母都有自己的value...like下面。A,J,S-1B,K,T-2C,L,U-3D,M,V-4E,N,W-5F,O,X-6G,P,Y-7H,Q,Z-8I,R-9。FirstName的第一个字母,然后是中间名的第一个字母和姓氏的第一个字母。相应的值必须加到一位数中.这个值必须显示在另一个页面上:我的名字叫罗山·彼得。所以名字:罗山和LAst名字:彼得..。所以拿‘’ROSHAN‘和'PETER’的第一个字母..。所以我们会得到两个字母‘r’和'P‘。我没有中间名..。所以这个值将为零。所以R-9的值和P-7的值相加9+7= 16,所以我们需要用个位数来显示,所以,我们把16个字母相加,比如1+6 =7。所以我们的答案是7,我们需要在另一页上显示.我做了这样的代码,但结果没有显示..

MainActvity

代码语言:javascript
运行
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void gReport(View V) 
    {
        EditText et1 = (EditText) findViewById (R.id.editText1);
        EditText et2 = (EditText) findViewById (R.id.editText2);
        EditText et3 = (EditText) findViewById (R.id.editText3);
        TextView tv1 = (TextView) findViewById (R.id.textView1);
        ArrayList<Integer> sum1;
        long sum2 =0;
        sum1 = getMissingNo(et1.getText().toString() + et2.getText().toString());

        String firstName = et1.getText().toString();
        String middleName = et2.getText().toString();
        String lastName = et3.getText().toString();
        char blno = firstName.charAt(0);
        char blno1 = middleName.charAt(0);
        char blno2 = lastName.charAt(0);
        sum2 = getSum(String.valueOf(blno) + String.valueOf(blno1) + String.valueOf(blno2));

        int itemCount =sum1.size();
        tv1.setText(String.valueOf(blno));
        Intent in = new Intent(this, FirstActivity.class);
        in.putIntegerArrayListExtra("sum1", (ArrayList<Integer>) sum1);
        in.putExtra("itemCount", itemCount);
        in.putExtra("name2", sum2);
        startActivity(in);

        //int itemCount = sum1.size();






    }

    private long getSum(String text) {
        // TODO Auto-generated method stub
         long sum2 = 0;


            char[] name2 = new char[text.length()];

                   name2 = text.toCharArray();

                   for(int i=0; i<text.length(); i++)
                   {
                       sum2 += value2( name2[i] );
                    }
                     while (sum2>9)


                   {                  


                       sum2 = findDigitSum2(sum2);
                   }
                  return sum2;
    }

    private long findDigitSum2(long n) {
        // TODO Auto-generated method stub
        int sum2=0;
        while (n != 0) 
        {
         sum2 += n % 10;
         n = n / 10;
        }
        return sum2;
    }

    private long value2(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;

            }
    }

    private ArrayList<Integer> getMissingNo(String text) {
         ArrayList<Integer> sum1 = new ArrayList<Integer>();
        // TextView tv1 = (TextView) findViewById (R.id.textView1);
            boolean[] usedNos = new boolean[9];



            for(int i=0; i<text.length(); i++){
                usedNos [(int) (value1(text.charAt(i))-1)] = true;
            }




            for(int i=0; i<9; i++){
                if(!usedNos[i]){
                   sum1.add(i+1);
                    //System.out.println((i+1) + " is missing");
                    //tv1.setText(String.valueOf((i+1)));


                }
            }

            return sum1;
        // TODO Auto-generated method stub

    }



    private long value1(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;

            }
    }

FirstActivity

代码语言:javascript
运行
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstactivity_xm);

        TextView tv1 = (TextView) findViewById (R.id.textView1);



        ArrayList<Integer> list = getIntent().getIntegerArrayListExtra("sum1");
        tv1.setText("");
        for (int j = 0; j < list.size(); j++){
           tv1.append("KarmicLesson " + list.get(j) + "\n");


        }

        TextView tv3 = (TextView) findViewById (R.id.textView3);
        tv3.setText(getIntent().getStringExtra("name2"));


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-28 12:13:29

在Java中,可以使用以下代码选择字符串的第一个字母:-

代码语言:javascript
运行
复制
String s = "Roshan";
char c = s.charAt(0);

会做好这份工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20299951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档