英文:
How to change Text view to int and use it in mathematical operations?
问题
以下是翻译好的内容:
你好,我正在尝试制作一个应用程序,它会给你两个随机数,用户必须将这两个随机数相加。但是我的问题是随机数是小部件,而不是整数。
我已经尝试了很多解释,但没有任何效果。
public void action(View v){
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
if (num1 + num2){
}
}
这是错误信息:
二元运算符 '+' 的操作数类型不匹配,第一个类型:TextView,第二个类型:TextView
英文:
Hello I'm trying to make an app that gives you 2 random numbers and the user has to add the 2 random numbers, so my error is that that the random numbers are widgets not integers,
I have tried hundreds of explinations but nothing is working.
public void action(View v){
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
if (num1 + num2){
}
}
and this is the error:
> bad operand types for binary operator '+' first type: TextView second
> type: TextView
答案1
得分: 1
你所遇到的问题可以从你收到的错误消息中清楚地描述出来。
根据你提供的代码片段,我猜想num1和num2是指向TextView对象的引用。
你不能直接在TextView对象本身上执行加法操作,而是要在它们的内容上进行操作。
因此,你想要实现的是这样的:
public void action(View v) {
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
String num1String = num1.getText().toString();
String num2String = num2.getText().toString();
int firstNumber = Integer.parseInt(num1String);
int secondNumber = Integer.parseInt(num2String);
if (firstNumber + secondNumber) {
// YOUR LOGIC
}
}
或者更好的做法是,你可以将生成的随机数保存到变量中,以后再使用,像这样:
public void action(View v) {
int randomNumberOne = randNum1.nextInt(10);
int randomNumberTwo = randNum2.nextInt(10);
num1.setText(String.valueOf(randomNumberOne));
num2.setText(String.valueOf(randomNumberTwo));
userAnswer.getText();
if (randomNumberOne + randomNumberTwo) {
// YOUR LOGIC
}
}
英文:
The problem you have is well described by the error you are receiving.
From the code snippet you have provided, I am guessing num1 and num2 are references to textview objects.
You CANNOT perform addition on the textview objects themselves, but rather on their content.
So, what you are trying to achieve is this:
public void action(View v){
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
String num1String = num1.getText().toString();
String num2String = num2.getText().toString();
int firstNumber = Integer.parseInt(num1String);
int secondNumber = Integer.parseInt(num2String);
if (firstNumber + secondNumber){
// YOUR LOGIC
}
}
Or better yet, you could save the generated random numbers to variables and use them later, like so:
public void action(View v){
int randomNumberOne = randNum1.nextInt(10);
int randomNumberTwo = randNum2.nextInt(10);
num1.setText(String.valueOf(randomNumberOne));
num2.setText(String.valueOf(randomNumberTwo));
userAnswer.getText();
if (randomNumberOne + randomNumberTwo){
// YOUR LOGIC
}
}
答案2
得分: 0
你可以按照以下步骤进行操作。我假设randNum1和randNum2是随机数,num1和num2是TextView。
public void action(View v) {
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
int randNumber1 = Integer.parseInt(num1.getText().toString());
int randNumber2 = Integer.parseInt(num2.getText().toString());
if (randNumber1 + randNumber2) {
// 在这里编写你的逻辑
} else {
// 在这里编写其他情况的逻辑
}
}
英文:
You can follow the below steps. i assume randNum1 and randNum2 is random number and num1 and num2 is TextView.
public void action(View v){
num1.setText(String.valueOf(randNum1.nextInt(10)));
num2.setText(String.valueOf(randNum2.nextInt(10)));
userAnswer.getText();
int randNumber1 = Integer.parseInt(num1.getText().toString());
int randNumber2 = Integer.parseInt(num2.getText().toString());
if (randNumber1 + randNumber2 ){
//your logic write here
}else{}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论