Java Android计算字母的值
我正在开发一个Android应用程序。。。这是一个基于命理学的应用程序,其中。。。A-I有一个1-9的值,然后是J-R和S-Z,所以每个字母都有值,然后计算它。。。我需要显示一个计算用户名字的结果,例如我的名字是ROSHAN PETER。。。我的名字是ROSHAN,姓PETER,所以值是9+6+1+8+1+5 ie,总值是30。。。下一步,我的年龄是27岁,所以我需要计算ROSHA,因为它的总值是25,当我计算N的值时,它将变成30。所以我需要显示,直到30年,我的当前值是N。假设我的名字是RAM,我的总值是9+1+4,所以值是14,我的年龄是27,所以我需要再计算一次RAM的总值,它将得到28,所以我当前的字母是M。。 我做过这样的代码,但我的问题是。。。我的名字值正在计算一次。。但我需要计算到我的年龄。。。以RAM的名称为例 我正在更新我的代码 主要活动Java Android计算字母的值,java,android,Java,Android,我正在开发一个Android应用程序。。。这是一个基于命理学的应用程序,其中。。。A-I有一个1-9的值,然后是J-R和S-Z,所以每个字母都有值,然后计算它。。。我需要显示一个计算用户名字的结果,例如我的名字是ROSHAN PETER。。。我的名字是ROSHAN,姓PETER,所以值是9+6+1+8+1+5 ie,总值是30。。。下一步,我的年龄是27岁,所以我需要计算ROSHA,因为它的总值是25,当我计算N的值时,它将变成30。所以我需要显示,直到30年,我的当前值是N。假设我的名字是RA
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;
}
public void gReport(视图五)
{
长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());
意图i=新意图(这是fullexplainion.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(年、月、日));
i、 putExtra(“名称17”,sum17+”);
i、 putExtra(“a”,s+);
星触觉(i);
}
私有长getSum17(字符串文本){
//TODO自动生成的方法存根
长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(年、月、日));
长t=整数.parseInt(s);
char[]name17=新字符[text.length()];
name17=text.toCharArray();
对于(int i=0;i好吧,我的朋友,你的代码几乎是不可理解的。然而,今晚我手头有很多时间,所以我费力地读了一遍。为了理解你试图完成的任务,我已经重命名了你的一些变量,但这段代码应该可以工作,而不必修改你的主要活动以外的任何内容
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;
}
}
在每个生日的编辑文本中,添加:
android:inputType="number"
如果您有任何问题,请告诉我。不要对变量使用无意义的名称,特别是当其他人试图阅读您的代码时。。节省我们的时间并编写清晰的代码。注释也很有用,不要遵循糟糕的编程实践。对不起,Jox…我一定会在下次编码时这样做…对不起
android:inputType="number"