观看一些视频后,我正在构建我的第一个android应用程序。我只是在构建一个基本的应用程序,该应用程序接收用户的等级输入,并使用Toast通知他们其字母等级是什么。我已经正确实现了Java和Xml文件,并且模拟器运行完美,但是Java的逻辑是错误的。输入的每个数字都会导致返回if语句“请输入有效成绩”,而不是实际成绩。
这是我的Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GradeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to SJU Grading App"
android:paddingTop="30sp"
android:paddingBottom="20sp"
android:textSize="25sp"
android:layout_gravity="center" />
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Numeric Grade Received out of 100"
android:layout_gravity="center"
android:paddingBottom="20sp"/>
<EditText
android:id="@+id/directions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:background="#CCCCCC"
android:ems="8"
android:inputType="number" />
<Button
android:id="@+id/sumbitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:text="Sumbit"
android:background="#0099FF"/>
</LinearLayout >
这是我的java文件:
package com.example.yberh703.gradecalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class GradeActivity extends AppCompatActivity {
Button myButton;
EditText input;
public void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
}
if(grade>80&& grade<90){
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
}
if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
}
if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
}
if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
myButton =(Button)findViewById(R.id.sumbitButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getGrade((EditText)findViewById(R.id.directions));
}
});
}
}
我很高兴能获得所有帮助。我觉得这很愚蠢,但是解决了,但是我已经尝试了一分钟了。
谢谢
答案 0 :(得分:3)
如果
,则使用elsepublic void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
} else if (grade>80&& grade<90) {
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
} else if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
} else if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
} else if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:3)
之所以发生这种情况,是因为总是检查您的最后一个If Condition(if(grade> 0 && grade <60)),而不管上述If条件是否为真,并且由于您可能输入的成绩输入失败如果条件使它进入ELSE部分,则最后一个,因此,您每次都会获得“请输入有效成绩”。
将IF构造更改为以下格式。
public void getGrade(EditText input){
input = (EditText)findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if(grade>90){
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
}
else if(grade>80&& grade<90){
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
}
else if(grade>70&& grade<80){
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
}
else if(grade>60&&grade<70){
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
}
else if(grade>0&&grade<60){
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}
现在,在任何时间点,如果发现为真,则仅对单个IF / ELSE条件进行评估。
答案 2 :(得分:0)
public void getGrade(EditText input) {
input = (EditText) findViewById(R.id.directions);
String A_grade = input.getText().toString();
int grade = Integer.parseInt(A_grade);
if (grade > 90) {
Toast.makeText(this, "Your Grade: A", Toast.LENGTH_SHORT).show();
} else if (grade >= 80 && grade < 90) {
Toast.makeText(this, "Your Grade: B", Toast.LENGTH_SHORT).show();
} else if (grade >= 70 && grade < 80) {
Toast.makeText(this, "Your Grade: C", Toast.LENGTH_SHORT).show();
} else if (grade >= 60 && grade < 70) {
Toast.makeText(this, "Your Grade: D", Toast.LENGTH_SHORT).show();
} else if (grade > 0 && grade < 60) {
Toast.makeText(this, "You Failed :( ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please Enter A Valid Grade", Toast.LENGTH_SHORT).show();
}
}