英文:
errors in my +,- calculating game program
问题
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random ran = new Random();
int n1 = ran.nextInt(98) + 1;
int n2 = ran.nextInt(98) + 1;
int n3 = ran.nextInt(2);
int answer = 0;
int acnt = 0;
int qcnt = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.Question);
final TextView tv2 = (TextView) findViewById(R.id.result);
final EditText eText = (EditText) findViewById(R.id.Answer);
Button bt1 = (Button) findViewById(R.id.bt1);
Button bt2 = (Button) findViewById(R.id.bt2);
Button bt3 = (Button) findViewById(R.id.endbt);
if (n3 == 0)
tv.setText(n1 + "+" + n2 + "=");
else
tv.setText(n1 + "-" + n2 + "=");
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
if (Integer.parseInt(str) == answer) {
Toast.makeText(getApplicationContext(), "Correct.", Toast.LENGTH_LONG).show();
acnt++;
} else if (str == null)
Toast.makeText(getApplicationContext(), "Write the answer.", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Wrong. The answer is " + answer, Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n1 = ran.nextInt(98) + 1;
int n2 = ran.nextInt(98) + 1;
int n3 = ran.nextInt(98) + 1;
if (n3 == 0) {
answer = n1 + n2;
tv.setText(n1 + "+" + n2 + "=");
qcnt++;
} else {
answer = n1 - n2;
tv.setText(n1 + "-" + n2 + "=");
qcnt++;
}
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText(acnt + "/" + qcnt + ", percentage: " + (float) (acnt / qcnt) + "%");
}
});
}
}
英文:
I'm a beginner of android studio and I want to make a simple calculating game program.(1~99 / +, -)
But there's so much errors in this program and I don't know what should be fixed.
Errors: illegal start of type / <identifier> expected / ';' expected / class, interface, or enum expected
(Especially 'class, interface, or enum expected' errors are so much)
Here's the code of program
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random ran=new Random();
int n1=ran.nextInt(98)+1;
int n2=ran.nextInt(98)+1;
int n3=ran.nextInt(2);
int answer=0;
int acnt=0;
int qcnt=0;
if(n3==0){
answer = n1 + n2;
qcnt++;
}
else{
answer=n1-n2;
qcnt++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.Question);
final TextView tv2=(TextView)findViewById(R.id.result);
final EditText eText=(EditText)findViewById(R.id.Answer);
Button bt1=(Button)findViewById(R.id.bt1);
Button bt2=(Button)findViewById(R.id.bt2);
Button bt3=(Button)findViewById(R.id.endbt);
if (n3==0)
tv.setText(n1+"+"+n2+"=");
else
tv.setText(n1+"-"+n2+"=");
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
if (Integer.parseInt(str) == answer){
Toast.makeText(getApplicationContext(), "Correct.", Toast.LENGTH_LONG).show();
acnt++;
}
else if (str==null)
Toast.makeText(getApplicationContext(), "Write the anser.", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Wrong. The answer is "+answer, Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n1=ran.nextInt(98)+1;
int n2=ran.nextInt(98)+1;
int n3=ran.nextInt(98)+1;
if (n3==0){
answer = n1 + n2;
tv.setText(n1+"+"+n2+"=");
qcnt++;
}
else{
answer=n1-n2;
tv.setText(n1+"-"+n2+"=");
qcnt++;
}
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText(acnt+"/"+qcnt+", percentage: "+(float)(acnt/qcnt)+"%");
}
});
}
}
答案1
得分: 1
主要问题是不允许代码编译的原因是将 if else
语句直接放在类中:
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
需要将其放在以下位置之一:A) 初始化块,或者 B) 方法中:
A)
{
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
}
B)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
}
英文:
The main issue not allowing for the code to compile is the if else
statement being placed directly in the class:
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
It needs to be placed within A) an initializer block or B) a method:
A)
{
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
}
B)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (n3 == 0) {
answer = n1 + n2;
qcnt++;
} else {
answer = n1 - n2;
qcnt++;
}
答案2
得分: 0
除了我之前的答案中关注编译错误的部分,以下是您的代码的改进版本。
3个主要修改:
1)创建一个用于创建问题的方法(即makeNewQuestion
),在onCreate
方法中调用(用于创建初始问题)和onClick
方法中调用(在按钮点击后创建新问题)
2)创建一个用于显示问题的方法(displayQuestion(TextView tv)
也在onCreate
和onClick
方法中调用),其中原来的if
条件被替换为一个三元运算符
3)在n1
和n2
变量赋值中,将ran.nextInt(98) + 1
替换为ran.nextInt(99) + 1
,因为@param bound
中的上界不包括边界值
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random ran = new Random();
int n1;
int n2;
int n3;
int answer;
int acnt;
int qcnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView)findViewById(R.id.Question);
final TextView tv2 = (TextView)findViewById(R.id.result);
final EditText eText = (EditText)findViewById(R.id.Answer);
Button bt1 = (Button)findViewById(R.id.bt1);
Button bt2 = (Button)findViewById(R.id.bt2);
Button bt3 = (Button)findViewById(R.id.endbt);
makeNewQuestion();
displayQuestion(tv);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
if (Integer.parseInt(str) == answer) {
Toast.makeText(getApplicationContext(), "Correct.", Toast.LENGTH_LONG).show();
acnt++;
} else if (str == null)
Toast.makeText(getApplicationContext(), "Write the answer.", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Wrong. The answer is " + answer, Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeNewQuestion();
displayQuestion(tv);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText(acnt + "/" + qcnt + ", percentage: " + (float) (acnt / qcnt) + "%");
}
});
}
private void displayQuestion(TextView tv) {
String operator = (n3 == 0) ? "+" : "-";
tv.setText(n1 + operator + n2 + "=");
}
private void makeNewQuestion() {
n1 = ran.nextInt(99) + 1;
n2 = ran.nextInt(99) + 1;
n3 = ran.nextInt(2);
qcnt++;
if (n3 == 0) {
answer = n1 + n2;
} else {
answer = n1 - n2;
}
}
}
英文:
In addition to my previous answer focusing on the compilation error, here below is an improved version of your code.
The 3 main modifications:
- creating a method for question creation (i.e.
makeNewQuestion
) called in theonCreate
method (to make the initial question) andonClick
method (to make a new question after button click) - creating a method for displaying the question (
displayQuestion(TextView tv)
also called inonCreate
andonClick
methods), where the original 'if' condition was replaced by a 'ternary operator' - within the
n1
andn2
variable assignments, replacing ran.nextInt(98) + 1
by ran.nextInt(99) + 1
, as the upper bound in@param bound
is excluding the bound value
<br>
package com.example.hello;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random ran = new Random();
int n1;
int n2;
int n3;
int answer;
int acnt;
int qcnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView)findViewById(R.id.Question);
final TextView tv2 = (TextView)findViewById(R.id.result);
final EditText eText = (EditText)findViewById(R.id.Answer);
Button bt1 = (Button)findViewById(R.id.bt1);
Button bt2 = (Button)findViewById(R.id.bt2);
Button bt3 = (Button)findViewById(R.id.endbt);
makeNewQuestion();
displayQuestion(tv);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
if (Integer.parseInt(str) == answer) {
Toast.makeText(getApplicationContext(), "Correct.", Toast.LENGTH_LONG).show();
acnt++;
} else if (str == null)
Toast.makeText(getApplicationContext(), "Write the answer.", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Wrong. The answer is " + answer, Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeNewQuestion();
displayQuestion(tv);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText(acnt + "/" + qcnt + ", percentage: " + (float) (acnt / qcnt) + "%");
}
});
}
private void displayQuestion(TextView tv) {
String operator = (n3 == 0) ? "+" : "-";
tv.setText(n1 + operator + n2 + "=");
}
private void makeNewQuestion() {
n1 = ran.nextInt(99) + 1;
n2 = ran.nextInt(99) + 1;
n3 = ran.nextInt(2);
qcnt++;
if (n3 == 0) {
answer = n1 + n2;
} else {
answer = n1 - n2;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论