Android Studio中的CGPA计算器
创始人
2024-08-18 09:30:13
0次

以下是一个基于Android Studio的CGPA计算器的代码示例:

在activity_main.xml文件中,添加以下布局代码:



    

    

    

    

    

在MainActivity.java文件中,添加以下代码:

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText etCourse1, etCourse2, etCredit1, etCredit2;
    private Button btnCalculate;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etCourse1 = findViewById(R.id.et_course1);
        etCourse2 = findViewById(R.id.et_course2);
        etCredit1 = findViewById(R.id.et_credit1);
        etCredit2 = findViewById(R.id.et_credit2);
        btnCalculate = findViewById(R.id.btn_calculate);
        tvResult = findViewById(R.id.tv_result);

        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateCGPA();
            }
        });
    }

    private void calculateCGPA() {
        double course1Grade = Double.parseDouble(etCourse1.getText().toString());
        double course2Grade = Double.parseDouble(etCourse2.getText().toString());
        int course1Credit = Integer.parseInt(etCredit1.getText().toString());
        int course2Credit = Integer.parseInt(etCredit2.getText().toString());

        double totalGradePoints = (course1Grade * course1Credit) + (course2Grade * course2Credit);
        int totalCredits = course1Credit + course2Credit;

        double cgpa = totalGradePoints / totalCredits;

        tvResult.setText("CGPA: " + cgpa);
    }
}

这个示例中,我们创建了一个基本的布局,其中有四个EditText用于输入每门课程的成绩和学分,一个Button用于计算CGPA,一个TextView用于显示结果。在MainActivity中,我们引用了布局中的各个视图,并将按钮的点击事件设置为调用calculateCGPA()方法。在calculateCGPA()方法中,我们从EditText中获取输入的成绩和学分,计算总绩点和总学分,然后计算CGPA,并将结果显示在TextView中。

注意:这只是一个基本的示例,你可以根据自己的需求进行扩展和修改。

相关内容

热门资讯

分辨真假!wepoker有没有... 分辨真假!wepoker有没有挂,WPK开挂技巧下载(确实有透视挂)AI 智能体黑科技再上新,自动化...
技术分享!wejoker私人辅... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
玩家交流!WPK下载,微扑克透... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟秒懂!德扑之星透视辅助软... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
盘点几款!wpk辅助哪里买,德... 您好,盘点几款!wpk辅助哪里买,德扑之星有挂透视吗平台(确实真的有辅助)这款游戏可以开挂的,确实是...
细节揭秘!德普之星怎么开辅助,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
实测发现!wepoker私人局... 您好:wepoker私人局有透视吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
分享一款!wepoker透视脚... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker透视脚本免费下载pc解...
分享给玩家!wepoker智能... 分享给玩家!wepoker智能辅助插件,wepoker透视脚本(先前是有辅助)AI 是一款可以让一直...
最新技巧!wepokerplu... 最新技巧!wepokerplus万能挂,德扑之星能不能开挂神器(竟然有辅助器)主打本地运行的 AI ...