我有一些代码,用户在EditText中输入数字。
根据输入内容的值,我想设置一个由对话框片段拉出的字符串(我现在用吐司取代,直到我可以修改字符串位。)
我得到了更改为int的pushups输入。 该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();
}
};});
}}
我知道我最近一直在问很多,但是所提供的任何帮助都将一如既往地受到极大的赞赏!
答案 0 :(得分:2)
您的变量仅在声明它们的范围内是已知的。例如:
if(pushn >=20 || pushn <=30){
String pushs = "Good";
}
如果这个pushs
, if
将无法在范围之外被识别,您将收到编译错误:
pushs cannot be resolved to a variable
答案 1 :(得分:1)
你正在重新声明你已经在onCreate之前声明的pushs
,它是全局的,如果/ else块是本地的,其范围在{}
内,则会向内推送。因此你需要删除 String 在推送之前使用if和else block.Also你的Toast也在最后一个if if block因此你没有得到Toast msg。
像这样改变你的代码
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();
}
在此之后一切都会正常工作