Android计算器简单实现及代码分析

article/2025/11/3 2:26:20

一、UI布局及代码

  • 页面效果
    在这里插入图片描述
  • 布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:weightSum="8"><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:id="@+id/text_view"android:layout_weight="2"android:textSize="70dp"android:gravity="right|bottom"android:maxLines="2" /><TextViewandroid:id="@+id/result_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="right|bottom"android:maxLines="1"android:textSize="65dp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="5"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_ac"android:layout_weight="1"android:text="AC"android:background="#ff0000"android:textColor="#000"android:textSize="20dp"android:layout_margin="5dp" /><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_left"android:layout_weight="1"android:text="("android:background="#fff"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_right"android:layout_weight="1"android:text=")"android:background="#fff"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_divide"android:layout_weight="1"android:text="/"android:background="#fffaf0"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_del"android:layout_weight="1"android:text="DEL"android:background="#808a87"android:textColor="#000"android:textSize="15dp"android:layout_margin="5dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="4"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_7"android:layout_weight="1"android:text="7"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_8"android:layout_weight="1"android:text="8"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_9"android:layout_weight="1"android:text="9"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_mult"android:layout_weight="1"android:text="*"android:background="#fffaf0"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="4"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_4"android:layout_weight="1"android:text="4"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_5"android:layout_weight="1"android:text="5"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_6"android:layout_weight="1"android:text="6"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_add"android:layout_weight="1"android:text="+"android:background="#fffaf0"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="4"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_1"android:layout_weight="1"android:text="1"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_2"android:layout_weight="1"android:text="2"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_3"android:layout_weight="1"android:text="3"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_minus"android:layout_weight="1"android:text="-"android:background="#fffaf0"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="horizontal"android:weightSum="4"><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_0"android:layout_weight="2"android:text="0"android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_dot"android:layout_weight="1"android:text="."android:textColor="#000"android:textSize="30dp"android:layout_margin="5dp"/><Buttonandroid:layout_width="0dp"android:layout_height="match_parent"android:id="@+id/button_result"android:layout_weight="1"android:text="="android:background="#ff6347"android:textColor="#fff"android:textSize="30dp"android:layout_margin="5dp"/></LinearLayout></LinearLayout>
  • 解析
  1. 布局使用LinearLayout布局,android:orientation="vertical"指定垂直布局,从上至下依次是表达式显示框,结果显示框以及五行按钮,并指定总权重方便控制显示。
  2. 文本框TextView使用android:gravity="right|bottom"指定其从右下开始显示内容。
  3. 在按钮上,使用android:background="#ff0000"指定按钮背景颜色,使用android:layout_margin="5dp"控制偏移,使其不会过于紧凑。(关于按钮背景颜色无法设置,可在res//values//themes.xml文件下将代码<style name="Theme.Calculation" parent="Theme.MaterialComponents.DayNight.DarkActionBar.">改为<style name="Theme.Calculation" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

二、计算器逻辑设计

  • 代码部分
package com.example.calculation;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private final StringBuilder sb = new StringBuilder();//用于表达式的输入private String expression = "";//最终表达式的确定private String result = "";//计算结果private boolean isHasDot = false;//是否有小数点private final Stack<Boolean> dotStack = new Stack<>();//用栈存储每一个数字是否有小数点private boolean isZero = false;//是否是数字零private final Stack<Boolean> zeroStack = new Stack<>();private int leftNum = 0;//左括号数量private int rightNum = 0;//右括号数量private TextView textView;private TextView result_view;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView) findViewById(R.id.text_view);textView.setAutoSizeTextTypeUniformWithConfiguration(4, 70, 10, TypedValue.COMPLEX_UNIT_DIP);//将显示框设置为根据行数自动缩小result_view = (TextView) findViewById(R.id.result_view);result_view.setAutoSizeTextTypeUniformWithConfiguration(4, 70, 10, TypedValue.COMPLEX_UNIT_DIP);Button button1 = (Button) findViewById(R.id.button_1);button1.setOnClickListener(this);Button button2 = (Button) findViewById(R.id.button_2);button2.setOnClickListener(this);Button button3 = (Button) findViewById(R.id.button_3);button3.setOnClickListener(this);Button button4 = (Button) findViewById(R.id.button_4);button4.setOnClickListener(this);Button button5 = (Button) findViewById(R.id.button_5);button5.setOnClickListener(this);Button button6 = (Button) findViewById(R.id.button_6);button6.setOnClickListener(this);Button button7 = (Button) findViewById(R.id.button_7);button7.setOnClickListener(this);Button button8 = (Button) findViewById(R.id.button_8);button8.setOnClickListener(this);Button button9 = (Button) findViewById(R.id.button_9);button9.setOnClickListener(this);Button button0 = (Button) findViewById(R.id.button_0);button0.setOnClickListener(this);Button buttondot = (Button) findViewById(R.id.button_dot);buttondot.setOnClickListener(this);Button buttonadd = (Button) findViewById(R.id.button_add);buttonadd.setOnClickListener(this);Button buttonminus = (Button) findViewById(R.id.button_minus);buttonminus.setOnClickListener(this);Button buttonmult = (Button) findViewById(R.id.button_mult);buttonmult.setOnClickListener(this);Button buttondivide = (Button) findViewById(R.id.button_divide);buttondivide.setOnClickListener(this);Button buttonac = (Button) findViewById(R.id.button_ac);buttonac.setOnClickListener(this);Button buttondel = (Button) findViewById(R.id.button_del);buttondel.setOnClickListener(this);Button buttonleft = (Button) findViewById(R.id.button_left);buttonleft.setOnClickListener(this);Button buttonright = (Button) findViewById(R.id.button_right);buttonright.setOnClickListener(this);Button buttonresult = (Button) findViewById(R.id.button_result);buttonresult.setOnClickListener(this);}@Overridepublic void onClick(View v) {result_view.setText("");switch (v.getId()) {case R.id.button_1:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('1');textView.setText(sb.toString());break;case R.id.button_2:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('2');textView.setText(sb.toString());break;case R.id.button_3:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('3');textView.setText(sb.toString());break;case R.id.button_4:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('4');textView.setText(sb.toString());break;case R.id.button_5:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('5');textView.setText(sb.toString());break;case R.id.button_6:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('6');textView.setText(sb.toString());break;case R.id.button_7:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('7');textView.setText(sb.toString());break;case R.id.button_8:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('8');textView.setText(sb.toString());break;case R.id.button_9:if (isZero == true) {sb.deleteCharAt(sb.length() - 1);isZero = false;}sb.append('9');textView.setText(sb.toString());break;case R.id.button_0:if (isZero == false) {sb.append('0');if (sb.length() == 1 ||!('0' <= sb.charAt(sb.length() - 2) && sb.charAt(sb.length() - 2) <= '9')&& sb.charAt(sb.length() - 2) != '.') {isZero = true;}}textView.setText(sb.toString());break;case R.id.button_dot:if (isHasDot == false) {if (sb.length() == 0 ||!('0' <= sb.charAt(sb.length() - 1) && sb.charAt(sb.length() - 1) <= '9')) {sb.append("0.");} else {sb.append('.');}isHasDot = true;isZero = false;}textView.setText(sb.toString());break;case R.id.button_add:if (sb.length() != 0 &&(sb.charAt(sb.length() - 1) == '+' || sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '*' || sb.charAt(sb.length() - 1) == '/')) {sb.setCharAt(sb.length() - 1, '+');} else if (sb.length() != 0) {sb.append('+');zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}if (result.matches("[-]?\\d+[.]?\\d*") && sb.length() == 0) {sb.append(result + "+");isHasDot = result.matches("[-]?\\d*[.]\\d*");isZero = "0".equals(result);zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}textView.setText(sb.toString());break;case R.id.button_minus:if (sb.length() != 0 &&(sb.charAt(sb.length() - 1) == '+' || sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '*' || sb.charAt(sb.length() - 1) == '/')) {sb.append('(');zeroStack.push(isZero);dotStack.push(isHasDot);leftNum++;}if (result.matches("[-]?\\d+[.]?\\d*") && sb.length() == 0) {sb.append(result + "-");isHasDot = result.matches("[-]?\\d*[.]\\d*");isZero = "0".equals(result);zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}sb.append('-');zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;textView.setText(sb.toString());break;case R.id.button_mult:if (sb.length() != 0 &&(sb.charAt(sb.length() - 1) == '+' || sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '*' || sb.charAt(sb.length() - 1) == '/')) {sb.setCharAt(sb.length() - 1, '*');} else if (sb.length() != 0) {sb.append('*');zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}if (result.matches("[-]?\\d+[.]?\\d*") && sb.length() == 0) {sb.append(result + "*");isHasDot = result.matches("[-]?\\d*[.]\\d*");isZero = "0".equals(result);zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}textView.setText(sb.toString());break;case R.id.button_divide:if (sb.length() != 0 &&(sb.charAt(sb.length() - 1) == '+' || sb.charAt(sb.length() - 1) == '-' || sb.charAt(sb.length() - 1) == '*' || sb.charAt(sb.length() - 1) == '/')) {sb.setCharAt(sb.length() - 1, '/');} else if (sb.length() != 0) {sb.append('/');zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}if (result.matches("[-]?\\d+[.]?\\d*") && sb.length() == 0) {sb.append(result + "/");isHasDot = result.matches("[-]?\\d*[.]\\d*");isZero = "0".equals(result);zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;}textView.setText(sb.toString());break;case R.id.button_left:sb.append('(');leftNum++;zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;textView.setText(sb.toString());break;case R.id.button_right:sb.append(')');rightNum++;zeroStack.push(isZero);dotStack.push(isHasDot);isZero = false;isHasDot = false;textView.setText(sb.toString());break;case R.id.button_del:if (sb.length() != 0) {if (sb.charAt(sb.length() - 1) == '.') {isHasDot = false;if (sb.charAt(sb.length() - 2) == '0') {sb.deleteCharAt(sb.length() - 1);}}if (sb.charAt(sb.length() - 1) == '(') {leftNum--;}if (sb.charAt(sb.length() - 1) == ')') {rightNum--;}if (sb.charAt(sb.length() - 1) == '0' && isZero == true) {isZero = false;}if (!(sb.charAt(sb.length() - 1) == '.' || '0' <= sb.charAt(sb.length() - 1) && sb.charAt(sb.length() - 1) <= '9')) {if (!dotStack.isEmpty()) {isHasDot = dotStack.pop();}if (!zeroStack.isEmpty()) {isZero = zeroStack.pop();}}sb.deleteCharAt(sb.length() - 1);}result_view.setText("");textView.setText(sb.toString());break;case R.id.button_ac:if (sb.length() != 0) {sb.delete(0, sb.length());}isHasDot = false;isZero = false;zeroStack.clear();dotStack.clear();expression = "";result = "";leftNum = 0;rightNum = 0;result_view.setText("");textView.setText("");break;case R.id.button_result:if (sb.length() != 0) {textView.setText(sb.toString());isHasDot = false;isZero = false;zeroStack.clear();dotStack.clear();if (leftNum < rightNum) {result = "出错";sb.delete(0, sb.length());result_view.setText(result);leftNum = 0;rightNum = 0;break;} else if (leftNum > rightNum) {while (leftNum > rightNum) {sb.append(')');rightNum++;}}if (sb.charAt(0) == '-') {sb.insert(0, '0');}if (sb.charAt(sb.length() - 1) == '+' || sb.charAt(sb.length() - 1) == '-') {sb.append('0');}if (sb.charAt(sb.length() - 1) == '*' || sb.charAt(sb.length() - 1) == '/') {sb.append('1');}for (int i = 1; i < sb.length() - 1; i++) {char ch = sb.charAt(i);if (ch == '(' && (sb.charAt(i - 1) == '.' || '0' <= sb.charAt(i - 1) && sb.charAt(i - 1) <= '9')) {sb.insert(i, '*');}if (ch == ')' && '0' <= sb.charAt(i + 1) && sb.charAt(i + 1) <= '9') {sb.insert(i + 1, '*');}}expression = sb.toString();sb.delete(0, sb.length());exchange(expression);expression = "";leftNum = 0;rightNum = 0;}result_view.setText(result);break;}}public void exchange(String expression) {Queue<Object> queue = new LinkedList<>();Stack<Character> stack = new Stack<>();int figureNum = 0;int operatorNum = 0;StringBuilder numSb = new StringBuilder();for (int i = 0; i < expression.length(); i++) {char ch = expression.charAt(i);if (ch == '.' || ('0' <= ch && ch <= '9')) {numSb.append(ch);} else {if (numSb.length() != 0) {queue.offer(new BigDecimal(numSb.toString()));figureNum++;numSb.delete(0, numSb.length());}if (ch == '-' && expression.charAt(i - 1) == '(' &&('0' <= expression.charAt(i + 1) && expression.charAt(i + 1) <= '9')) {numSb.append(ch);continue;}if (ch == '(') {stack.push(ch);} else if ((ch == '+' || ch == '-') && !stack.isEmpty() && stack.peek() == '(') {stack.push(ch);} else if ((ch == '*' || ch == '/') && !stack.isEmpty() && !(stack.peek() == '*' || stack.peek() == '/')) {stack.push(ch);} else {while (!stack.empty() && stack.peek() != '(') {queue.offer(stack.pop());operatorNum++;}if (!stack.isEmpty()) {stack.pop();}if (ch != ')') {stack.push(ch);}}}}if (numSb.length() != 0) {queue.offer(new BigDecimal(numSb.toString()));figureNum++;}while (!stack.empty()) {queue.offer(stack.pop());operatorNum++;}if (figureNum == operatorNum + 1) {calculate(queue);} else {result = "出错";}}public void calculate(Queue<Object> queue) {Stack<BigDecimal> stack = new Stack<>();while (!queue.isEmpty()) {Object obj = queue.poll();if (obj.getClass() == Character.class) {char operator = (Character) obj;BigDecimal num1 = (BigDecimal) stack.pop();BigDecimal num2 = (BigDecimal) stack.pop();BigDecimal subResult;if (operator == '+') {subResult = num2.add(num1);stack.push(subResult);} else if (operator == '-') {subResult = num2.subtract(num1);stack.push(subResult);} else if (operator == '*') {subResult = num2.multiply(num1);stack.push(subResult);} else {if ("0".equals(num1.toString())) {result = "不能除以零";return;} else if ("0".equals(num2.toString())) {stack.push(new BigDecimal("0"));} else {subResult = num2.divide(num1, 20, BigDecimal.ROUND_HALF_UP);stack.push(subResult);}}} else {stack.push((BigDecimal) obj);}}result = stack.pop().toString();if (result.matches("[-]?\\d*[.]\\d*")) {int i = result.length() - 1;while (result.charAt(i) == '0') {i--;}if (result.charAt(i) == '.') {i--;}result = result.substring(0, i + 1);}}
}
  • 分析
  1. expression,result,textview等会多次使用,因此将其设置为成员变量。
  2. 显示框根据行数自动放大或缩小。使用textView.setAutoSizeTextTypeUniformWithConfiguration(4, 70, 10, TypedValue.COMPLEX_UNIT_DIP);方法控制。第一个参数表示最小大小,第二个参数表示最大大小,第三个参数表示每次缩小步长,第四个参数表示之前参数的单位(如:dp,sp等)。
  • 按钮点击详解
  1. 除了按下等号键显示计算结果,其余按键都不需要显示,因此将结果显示框设为空。
  2. 输入数字时,如果前一个是数字“0”,按下其他数字应显示该数字,即显示“1”而不是“01”。
  3. 按下加减运算符时,如果之前有计算结果,应直接使用该结果运算。
  4. 表达式优化:以“-”号开头的表达式,可以在前加上零方便计算;如果左括号少于右括号,此表达式无法计算;左括号少于右括号,可以在末尾补全右括号;表达式以“+”“-”号结尾,应补上0,以“*”“/”结尾,应补上1;对于a(b),(a)b应自动补上乘号
  • 中缀表达式转后缀表达式
  1. 遇到数字以及小数点直接保存,遇到符号按照优先级顺序出栈入栈。
  2. 使用计数器分别记录运算符和数字的个数,如果不匹配,直接显示出错
  • 计算
  1. 在除法运算中,“0/a”应直接显示0、“a/0”应不计算,直接显示不能除以零
  2. 在最后结果上,“2.000000…”的类似有末尾0的结果应去除末尾0以及小数点

http://chatgpt.dhexx.cn/article/EP6IG3tj.shtml

相关文章

Android计算器——入门

Android计算器—入门 作者&#xff1a;黑衣侠客 一.前言 这是我写的第一个App&#xff0c;利用的是《安卓第一行代码》第三章&#xff0c;UI控件的一些知识&#xff0c;然后整体结构综合了一些CSDN博客和简书上的一些著作&#xff0c;同样&#xff0c;在写Android计算器&…

基础复习——项目练习——计算器

布局&#xff1a; <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:background"#eeeeee"android:orientation"vert…

安卓-AndroidStudio-计算器

安卓--Android Studio--简易计算器 结果截图关键代码讲解完整代码 本人是一名在校学生&#xff0c;由于上个学期的课程内容包括移动开发&#xff0c;其中的实验也算是大部分是自己完成的&#xff0c;所以算是写博客记录一下吧。 第一次写&#xff0c;有啥问题及时交流哈&#x…

Django数据库操作

目录 一、Django/ORM框架介绍及配置 1.1、ORM框架介绍 1.2、Django数据库配置 二、定义模型类数 2.1、定义模型类 2.2、迁移 2.3、插入数据 三、单表据库操作(增、删、改、查) 3.1、增 3.2、删 3.3、改 3.4查&#xff08;重点&#xff09; 四、两表联查 4.1、一对…

Qt入门------数据库操作

文章目录 一、数据库1.数据库驱动2.查询驱动3.连接数据库4.执行sql语句5.插入数据6.事务 二 &#xff0c;sql模型类1.QSqlQueryModel模型2.QSqlTableModel模型3.QSqlRelationalTableModel模型 一、数据库 Qt中的Qt SQL模块提供了对数据库的支持&#xff0c;模块中类可分为三层…

Java的数据库操作

Java的数据库操作&#xff0c;上学期末的课设是用到的&#xff0c;然而老师把JDBC连接mysql数据库的代码和配置文件已经给好了&#xff0c;那时对它可以说是只有一点点印象。 今天跟着书走一边敲一边&#xff0c;总算是有所进步。 数据库表的创建和初始数据的操作&#xff0c…

一文详解python中的数据库操作

python中的数据库操作 一、数据库编程接口1. 连接对象 二、使用内置的SQLite1.创建数据库文件2.操作SQLite 三、MySql数据库的使用3.1 安装MySql3.2 设置环境变量3.3 启动MySql3.4 使用navicat for mysql 管理软件3.5 安装PyMysql模块3.6 连接数据库 四、创建数据表4.1 创建boo…

数据库操作练习题

如何查看员工表中的所有字段&#xff1f; 如何查看员工表中的姓名和性别&#xff1f; 如何知道每个员工一年的总收入&#xff1f; 怎么查看女员工的信息&#xff1f; 如何查看月薪范围位于 8000 到 12000 之间的员工&#xff1f; 查询员工中叫做“张三”、“李四” 或“张飞”的…

MySQL(一)数据库操作

1.关系型数据库与非关系型数据库区别与联系 1.关系型数据库 ( SQL ) MySQL , Oracle , SQL Server , SQLite , DB2 , … 关系型数据库通过外键关联来建立表与表之间的关系 2.非关系型数据库 ( NOSQL ) Redis , MongoDB , … 非关系型数据库通常指数据以对象的形式存储在数据…

使用ORACLE 进行数据库操作

上一期讲了 如何创建表&#xff0c;例如我们创建了一个 名为 t_customer 的表&#xff0c;格式为下&#xff1a; create table t_customer(cust_id int primary key,cust_name varchar(20),cust_gender char(3),cust_age int,cust_birthday date,cust_height number(3,2),cust…

C#数据库操作

功能需求 1&#xff0c;利用随机数模拟产生每次考试成绩 2&#xff0c;将每次考试成绩存入到数据库 3&#xff0c;将每次考试成绩划分优、良、中、差、不及格五类&#xff0c;并作为查询条件&#xff0c;查询符合每种水平的成绩 技术知识点 1.random类的使用 2.数据库的链…

云数据库操作

1 云数据库创建 每一个list就相当于一个表&#xff0c;每个记录就是一条内容 云数据库存储形式是JSON2 读取云数据库值 数据库初始化 const db wx.cloud.database() 连接数据库 db.collection(需要连接的数据列表名)可以通过.doc(‘具体id号’)获取具体的信息内容获取年、月…

Android数据库操作

Android内置了一个名为SQLite的关系型数据库&#xff0c;这是一款轻量型的数据库&#xff0c;操作十分简便。SQLite与别的数据库不同的是&#xff0c;它没有数据类型。可以保存任何类型的数据到你所想要保存的任何表的任何列中。但它又支持常见的类型比如: NULL, VARCHAR, TEXT…

数据库基本操作

一、数据库基本操作 1.数据库的基本操作 -- 1.数据库的基本操作 使用test数据库 USE test; -- 查看当前test数据库中所有表 MySQL命令 SHOW TABLES; -- 查表的基本信息 SHOW CREATE TABLE student; -- -- 查看表的字段信息 desc student; 2.数据表的基本操作 -- 2.数据表的…

数据库基础操作

一、数据库的操作 数据库与客户端是通过网络进行交互的。 1、显示当前数据库 sql语句必须以 ; 结尾 show databases; 2、创建数据库 create database 数据库名; ->如果数据库sql敲错了&#xff0c;会有提示。 ->如果sql敲错了&#xff0c;可以按 ctrlc来终止sql。…

数据库的基本操作

目录 一、数据库的基本操作 1、数据库的登录及退出 2、查看所有数据库 3、显示数据库版本 4、显示时间 5、创建数据库 6、查看创建数据库的语句 7、查看当前使用的数据库 8、查看当前用户 9、使用某个数据库 10、删除数据库 二、数据表的基本操作 1、查看当前数据…

如何在IDEA上创建一个JSP项目【亲测有效】

idea上创建一个jsp项目 1、创建一个Java项目 2、创建成功后右击项目名称&#xff0c;点击Add Framework support 3、选择web application 选择适合版本&#xff0c;创建 4、创建成功后项目中会出现web文件夹 5、点击属性&#xff0c;进行配置 6、在project中配置jdk(你自己电脑…

Eclipse中安装配置Tomcat和创建JSP项目

这里写自定义目录标题 安装配置Tomcat创建JSP项目 安装配置Tomcat 下载并解压Tomcat到指定目录Eclipse菜单栏中找到Window–>Preperences->Server->Runtime Environments->Add 3.选择Tomcat文件夹 4.在Eclipse中找到Servers视图窗口&#xff0c;创建Tomcat服务…

springBoot+JSP搭建项目

1.springBoot对JSP的支持 springBoot虽然支持JSP&#xff0c;但是官方不建议使用&#xff0c;下面是官方文档解释 When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations…

IntelliJ IDEA中创建jsp项目

创建java项目 首先需要创建一个普通的java项目 这一步很简单就不用多说了&#xff0c;按照正常操作来就行了 创建好的目录结果如下 创建Moudle 在IDEA中jsp项目是Project中的一个Moudle&#xff0c;因此我们就需要在jsp中创建一个moudle 然后Next 输入moudle名称 finishe…