在我的加减计算游戏程序中出现了错误。

huangapple go评论66阅读模式
英文:

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+&quot;+&quot;+n2+&quot;=&quot;);
else
tv.setText(n1+&quot;-&quot;+n2+&quot;=&quot;);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = eText.getText().toString();
if (Integer.parseInt(str) == answer){
Toast.makeText(getApplicationContext(), &quot;Correct.&quot;, Toast.LENGTH_LONG).show();
acnt++;
}
else if (str==null)
Toast.makeText(getApplicationContext(), &quot;Write the anser.&quot;, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), &quot;Wrong. The answer is &quot;+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+&quot;+&quot;+n2+&quot;=&quot;);
qcnt++;
}
else{
answer=n1-n2;
tv.setText(n1+&quot;-&quot;+n2+&quot;=&quot;);
qcnt++;
}
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText(acnt+&quot;/&quot;+qcnt+&quot;, percentage: &quot;+(float)(acnt/qcnt)+&quot;%&quot;);
}
});
}
}

答案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)也在onCreateonClick方法中调用),其中原来的if条件被替换为一个三元运算符
3)在n1n2变量赋值中,将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:

  1. creating a method for question creation (i.e. makeNewQuestion) called in the onCreate method (to make the initial question) and onClick method (to make a new question after button click)
  2. creating a method for displaying the question (displayQuestion(TextView tv) also called in onCreate and onClick methods), where the original 'if' condition was replaced by a 'ternary operator'
  3. within the n1 and n2 variable assignments, replacing &nbsp;ran.nextInt(98) + 1&nbsp; by &nbsp;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(), &quot;Correct.&quot;, Toast.LENGTH_LONG).show();
acnt++;
} else if (str == null)
Toast.makeText(getApplicationContext(), &quot;Write the answer.&quot;, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), &quot;Wrong. The answer is &quot; + 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 + &quot;/&quot; + qcnt + &quot;, percentage: &quot; + (float) (acnt / qcnt) + &quot;%&quot;);
}
});
}
private void displayQuestion(TextView tv) {
String operator = (n3 == 0) ? &quot;+&quot; : &quot;-&quot;;
tv.setText(n1 + operator + n2 + &quot;=&quot;);
}
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;
}
}
}

huangapple
  • 本文由 发表于 2020年10月1日 03:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64144649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定