Java 我只是想问一下我的声明在android应用程序中是否正确

Java 我只是想问一下我的声明在android应用程序中是否正确,java,android,Java,Android,我只是想问一下我是否在int结果中设置了正确的值,因为每当我运行应用程序时,我的textview都不会输出ans*ans2的乘积 TextView tv2 = (TextView) findViewById(R.id.tv2); EditText et1 = (EditText) findViewById(R.id.first); EditText et2 = (EditText) findViewById(R.id.second); String temp = et1.getText().to

我只是想问一下我是否在
int
结果中设置了正确的值,因为每当我运行应用程序时,我的
textview
都不会输出
ans*ans2
的乘积

TextView tv2 = (TextView) findViewById(R.id.tv2);
EditText et1 = (EditText) findViewById(R.id.first);
EditText et2 = (EditText) findViewById(R.id.second);
String temp = et1.getText().toString();
String temp2 = et2.getText().toString();
int ans = Integer.parseInt(temp);
int ans2 = Integer.parseInt(temp);
int result = ans * ans2;

tv2.setText("" + result);

我猜这个问题与线路有关

int ans2 = Integer.parseInt(temp);
您正在解析来自
temp
而不是
temp2
的值,因此请尝试将其更改为

int ans2 = Integer.parseInt(temp2);

这可能对你有用

tv1.setText(String.format("%d", result));
setText(int)指的是一个类似R.string.myText的资源id

setText(String)是您希望调用的,Java自动将int装箱为字符串

尝试:


tv1.setText(“+result)

您在logcat中是否遇到异常?它是否输出任何内容?此外,ans2是否应该用temp2?或
tv1.setText(Integer.toString(result))初始化你能告诉我你的答案和我的有什么不同吗?你正在转换并分配相同的变量tempint ans2=Integer.parseInt(temp2);而不是int ans2=Integer.parseInt(temp);再次检查,尤其是最后一行。顺便说一句,如果你同意一些已经发布的答案,你可以将其更新,而不是复制:/temp2的用法在哪里?非常感谢你,我没有注意到另一个temp2不是temp2:D@user2699948如果我的答案解决了你的问题,你可以用它来标记你的问题已经解决。
TextView tv2 = (TextView) findViewById(R.id.tv2);
EditText et1 = (EditText) findViewById(R.id.first);
EditText et2 = (EditText) findViewById(R.id.second);
String temp = et1.getText().toString();
String temp2 = et2.getText().toString();
int ans = Integer.parseInt(temp);
int ans2 = Integer.parseInt(temp2 );
int result = ans * ans2;

tv2.setText("" + result);