1

我有一些代码,用户在 EditText 中输入数字。

根据他们输入的值,我想设置一个由对话框片段拉出的字符串(我现在已经用 toast 替换了,直到我可以修复字符串位。)

我得到了更改为 int 的俯卧撑输入。该 int 在其下方的逻辑中与某些标准进行比较。根据它落在哪里,我希望它更改一个字符串,一旦所有逻辑完成,该字符串将在稍后显示。

package com.mynavy;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PRTC extends Activity {
    String pushs;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prtc);
        Button submit = (Button) findViewById(R.id.submit);



    submit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            EditText age = (EditText) findViewById(R.id.agetxt);
            EditText pushups = (EditText) findViewById(R.id.pushups);
            EditText situps = (EditText) findViewById(R.id.situps);
            EditText runtime = (EditText) findViewById(R.id.runtime);
            String agetxt = String.valueOf(age.getText().toString());
            int agen = Integer.valueOf(agetxt);
            String pushtxt = String.valueOf(pushups.getText().toString());
            int pushn = Integer.valueOf(pushtxt);
            String sittxt = String.valueOf(situps.getText().toString());
            int sitn = Integer.valueOf(sittxt);
            String runtxt = String.valueOf(runtime.getText().toString());
            int runn = Integer.valueOf(runtxt);

//CHANGE THESE WHEN NEW INSTRUCTIONS COME OUT!!         
            if (agen >= 17 || agen <=20) {

                if (pushn >=20 || pushn <=30){
                    String pushs = "Good";
                }else if (pushn >=31 || pushn <=40){
                    String pushs = "Excellent";
                }else if (pushn >=41 || pushn <=50){
                    String pushs = "Outstanding";}}

                if (sitn >= 20 || sitn <= 30){
                    String sits = "Good";
                }else if (sitn >= 31 || sitn <=40){
                    String sits = "Excellent";
                }else if (sitn >= 41 || sitn <=50){
                    String sits = "Outstanding";

                    Toast.makeText(getBaseContext(), pushs + "Score.", Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getBaseContext(), "TEST FAILED!!", Toast.LENGTH_SHORT).show();
            }


        };});
}}

我知道我最近问了很多,但是提供的任何帮助都将一如既往地感激不尽!!

4

2 回答 2

2

您的变量仅在它们被声明的范围内是已知的。例如:

if(pushn >=20 || pushn <=30){
     String pushs = "Good";
}

pushs如果 this 超出范围将无法识别if,则会出现编译错误:

pushs cannot be resolved to a variable

于 2013-11-06T08:29:38.247 回答
1

您正在重新声明pushs您在 onCreate 之前已经声明的哪个是全局的,并且在 if/else 块内推送是本地的,其范围在. 因此{}您需要从 if 和 else 块中删除推送之前使用的字符串。此外,您的 Toast 在最后一个 else if 块内因此,您没有收到 Toast 味精。像这样更改您的代码

           if (agen >= 17 || agen <=20) {

                if (pushn >=20 || pushn <=30){
                    pushs = "Good";
                }else if (pushn >=31 || pushn <=40){
                    pushs = "Excellent";
                }else if (pushn >=41 || pushn <=50){
                   pushs = "Outstanding";
                 }


                if (sitn >= 20 || sitn <= 30){
                   String sits = "Good";
                }else if (sitn >= 31 || sitn <=40){
                   String sits = "Excellent";
                }else if (sitn >= 41 || sitn <=50){
                   String sits = "Outstanding";
                }

            Toast.makeText(getBaseContext(), pushs + "Score.",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getBaseContext(), "TEST FAILED!!", Toast.LENGTH_SHORT).show();
            }

在此之后一切都会正常

于 2013-11-06T08:42:21.110 回答