《步骤化》 做个简单的分数识别器(方法2)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/HEZEQUAN/article/details/101766841

之前写了一片同样的文章 ,题目是已知有考试分数若干,需用程序进行判断为‘优秀(>80分)’,‘良好(>70分)’,及格(>60分)’,‘不及格(<60分)’ ,并显示出结果。
之前文章的链接,点击进入,用的是if…else…方法,这次换一种方式,用的是if…switch…,程序结果还是这样,唯有写法不同。

编译工具:android studio 3.5
步骤起:
1.新建一个项目
2.activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="159dp"
        android:ems="10"
        android:hint="在此输入分数"
        android:inputType="textPersonName"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:text="查询"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="171dp"
        android:hint="查询结果在此显示"
        android:text=""
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

3.MainActivity代码

package com.example.a201909291x;
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;
public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    private EditText editText;
    int num=0;
    int score=0;
    //先将两个数初始化
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button) findViewById(R.id.button);
        textView=(TextView) findViewById(R.id.textView);
        editText=(EditText) findViewById(R.id.editText);
        button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                num=Integer.parseInt(editText.getText().toString());
                if(num>100||num<0){
                    textView.setText("不合理的数字");
                }switch (num){
                    case 100:
                    textView.setText("优秀");
                    break;
                }score=num/10;
                //除法之后,将值赋予score进行判断
                switch (score){
                    case 8:
                    case 9:
                        textView.setText("优秀");
                        break;
                    case 7:
                        textView.setText("良好");
                        break;
                    case 6:
                        textView.setText("及格");
                        break;
                    case 5:
                    case 4:
                    case 3:
                    case 2:
                    case 1:
                    case 0:
                        textView.setText("不及格");
                        break;
                }
            }
        });
    }
}

结语:程序结果与之前的一模一样,只是方法不同

猜你喜欢

转载自blog.csdn.net/HEZEQUAN/article/details/101766841