1

我开发了一个应用程序..它是一个命理应用程序..收集和计算用户的名字、第二名和第三个名字......为每个值分配 1 -9 的值,并且在计算所有字母的这些值时必须是加在一起并变成一个数字 1-9。之后,在另一个活动中将此值提供给 textview 并显示结果....我的问题是我进行了编程..但是添加时..正确的值不是显示..我使用 switch case 为每个字母提供值...我给 0 作为默认值..当结果显示时,值显示为 0 。如果我将其更改为 1,则将 1 添加到值中并且该值正在播放.. 请检查我的代码,如果有任何错误,请指出 4 我...谢谢...

MainActivity.java

public void gReport(View V)
{
long sum1 = 0;
long sum2 = 0;
long sum3 = 0;
long sum7 = 0;
long sum8 = 0;
long sum9 = 0;
long sum10 = 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);

sum1 = getSum1(et1.getText().toString());
sum2 = getSum2(et2.getText().toString());
sum3 = getSum3(et3.getText().toString());

/*sum7 = getSum7(et7.getText().toString());
sum8 = getSum8(et8.getText().toString());
sum9 = getSum9(et9.getText().toString());*/



sum10 = getSum10 (et1.getText().toString() + et2.getText().toString() + et3.getText().toString());  
Intent i = new Intent(this, FirstResult.class);
i.putExtra("name10", sum10 + "");
startActivity(i);
}

private long getSum10(String text)  
{
long sum10 = 0;
char[] name10 = new char[text.length()];
name10 = text.toCharArray();

for(int i=0; i<text.length(); i++)
{
     sum10 += value10( name10[i] );
}
while (sum10>9)
{                    
      sum10 = findDigitSum10(sum10);
}
  return sum10;         
}
private long value10(char a) 
{
        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 long findDigitSum10(long n)        
 {

  int sum10=0;
        while (n != 0)
            {
             sum10 += n % 10;
             n = n / 10;
            }

           return sum10;

}
}

结果活动.java

@Override
    protected void onCreate(Bundle savedInstanceState) 

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstresult_xm);


        TextView txt1 = (TextView) findViewById (R.id.textView2);
        txt1.setText(getIntent().getStringExtra("name10"));


    }
4

2 回答 2

5

sum10你里面没有钥匙intent,只有name10. 您应该首先看到 String intTextView而在第二个中什么也没有

于 2013-11-05T09:20:27.733 回答
0

Eclipse中的编码可能与Android不同?尝试在项目的属性中将其更改为 UTF-8。

我试过这样的concole代码。您的 value10 功能正常工作。

public class HelloWorld {
  public static void main(String[] args) {

    HelloWorld hw = new HelloWorld();
   long a = hw.value10('F');
   System.out.println("val: " + a);
  }
  private long value10(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;


      }


  }
}
于 2013-11-05T09:29:43.250 回答