[英]Android adding values of first characters of firstname,middlename and last name
我正在开发一个Android应用程序..其中用户的名字,中间名和姓氏被用作输入.....该应用程序是基于命理学的..因此每个字母都有其自己的值...如下。 J,S – 1 B,K,T – 2 C,L,U – 3 D,M,V – 4 E,N,W – 5 F,O,X – 6 G,P,Y – 7 H,Q ,Z – 8 I,R – 9因此,当用户输入自己的名字时,..名字的第一个字母,然后必须取中间名的第一个字母和姓氏的第一个字母..并将相应的值添加到一位...并且该值必须在另一页上显示例如:我的名字ROSHAN PETER ..所以名字:ROSHAN和最新名字:PETER ..所以取'ROSHAN'和'PETER'的首字母。 。所以我们将得到两个字母“ R”和“ P” ..我没有中间名..所以该值将为零。 所以R-9的值和P-7的值加起来9 + 7 = 16,所以我们需要用一位数字显示,所以我们将两个字母都加起来就像1 + 6 = 7 ..所以我们的答案是7我们需要在另一页上显示它...我做了这样的代码,但结果没有显示..
MainActvity
@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
@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"));
}
在Java中,可以使用以下代码选择字符串的首字母:-
String s = "Roshan";
char c = s.charAt(0);
会做的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.