用Java写了一个小猪效果图:

回答|共 65 个

legs+ 发表于 2022-12-16 22:24:05| 字数 1,950 | 显示全部楼层

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6.     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  7.     <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
  8.     <style>
  9.         #app{
  10.             text-align: center;
  11.         }
  12.         .container {
  13.             background-color: #73ffd6;
  14.             margin-top: 20px;
  15.             height: 300px;
  16.         }
  17.         .son{
  18.             margin-top: 30px;
  19.         }
  20.     </style>

  21. </head>
  22. <body>
  23. <div id="app">
  24.     <!-- 通过 router-link 标签来生成导航链接 -->
  25.     <router-link to="/home" tag="button">首页</router-link>
  26.     <router-link to="/list">列表</router-link>
  27.     <div class="container">
  28.         <!-- 将选中的路由渲染到 router-view 下-->
  29.         <router-view></router-view>
  30.     </div>
  31. </div>
  32. <template id="tmpl">
  33.     <div>
  34.         <h3>列表内容</h3>
  35.         <!-- 生成嵌套子路由地址 -->
  36.         <router-link to="/list/login">登录</router-link>
  37.         <router-link to="/list/register">注册</router-link>
  38.         <div class="son">
  39.             <!-- 生成嵌套子路由渲染节点 -->
  40.             <router-view></router-view>
  41.         </div>
  42.     </div>
  43. </template>
  44. <script>
  45.     // 1.定义路由跳转的组件模板
  46.     const home = {
  47.         template: '<div><h3>首页内容</h3></div>'
  48.     }
  49.     const list = {
  50.         template: '#tmpl'
  51.     }
  52.     const login = {
  53.         template: '<div> 登录页面内容</div>'
  54.     }

  55.     const register = {
  56.         template: '<div>注册页面内容</div>'
  57.     }
  58.     // 2.定义路由信息
  59.     const routes = [
  60.         // 路由重定向:当路径为/时,重定向到/home路由
  61.         {
  62.             path: '/',
  63.             redirect: '/home'
  64.         },
  65.         {
  66.             path: '/home',
  67.             component: home
  68.         },
  69.         {
  70.             path: '/list',
  71.             component: list,
  72.             //嵌套路由
  73.             children: [
  74.                 {
  75.                     path: 'login',
  76.                     component: login
  77.                 },
  78.                 {
  79.                     path: 'register',
  80.                     component: register
  81.                 },
  82.                 // 当路径为/list时,重定向到/list/login路径
  83.                 {
  84.                     path: '/list',
  85.                     redirect: '/list/login'
  86.                 }
  87.             ]
  88.         }
  89.     ]
  90.     const router = new VueRouter({
  91.         //mode: 'history', //使用 history 模式还是hash路由模式
  92.         routes
  93.     })
  94.     // 3.挂载到当前 Vue 实例上
  95.     const app = new Vue({
  96.         el: '#app',
  97.         data:{},
  98.         methods: {},
  99.         router: router
  100.     });
  101. </script>

  102. </body>
  103. </html>

复制代码
写了一个vue的路由,不知道网上能否找到在线运行的方法

legs+ 发表于 2022-12-17 17:18:28| 字数 6,114 | 显示全部楼层

360截图20221217171653941.jpg



代码:
  1. package com.example.junior;

  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.text.method.ScrollingMovementMethod;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import android.widget.Toast;

  9. import com.example.junior.util.Arith;

  10. /**
  11. * Created by ouyangshen on 2017/9/15.
  12. */
  13. public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
  14.     private final static String TAG = "CalculatorActivity";
  15.     private TextView tv_result; // 声明一个文本视图对象

  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_calculator);
  20.         // 从布局文件中获取名叫tv_result的文本视图
  21.         tv_result = findViewById(R.id.tv_result);
  22.         // 设置tv_result内部文本的移动方式为滚动形式
  23.         tv_result.setMovementMethod(new ScrollingMovementMethod());
  24.         // 下面给每个按钮控件都注册了点击监听器
  25.         findViewById(R.id.btn_cancel).setOnClickListener(this); // “取消”按钮
  26.         findViewById(R.id.btn_divide).setOnClickListener(this); // “除法”按钮
  27.         findViewById(R.id.btn_multiply).setOnClickListener(this); // “乘法”按钮
  28.         findViewById(R.id.btn_clear).setOnClickListener(this); // “清除”按钮
  29.         findViewById(R.id.btn_seven).setOnClickListener(this); // 数字7
  30.         findViewById(R.id.btn_eight).setOnClickListener(this); // 数字8
  31.         findViewById(R.id.btn_nine).setOnClickListener(this); // 数字9
  32.         findViewById(R.id.btn_plus).setOnClickListener(this); // “加法”按钮
  33.         findViewById(R.id.btn_four).setOnClickListener(this); // 数字4
  34.         findViewById(R.id.btn_five).setOnClickListener(this); // 数字5
  35.         findViewById(R.id.btn_six).setOnClickListener(this); // 数字6
  36.         findViewById(R.id.btn_minus).setOnClickListener(this); // “减法”按钮
  37.         findViewById(R.id.btn_one).setOnClickListener(this); // 数字1
  38.         findViewById(R.id.btn_two).setOnClickListener(this); // 数字2
  39.         findViewById(R.id.btn_three).setOnClickListener(this); // 数字3
  40.         findViewById(R.id.btn_zero).setOnClickListener(this); // 数字0
  41.         findViewById(R.id.btn_dot).setOnClickListener(this); // “小数点”按钮
  42.         findViewById(R.id.btn_equal).setOnClickListener(this); // “等号”按钮
  43.         findViewById(R.id.ib_sqrt).setOnClickListener(this); // “开平方”按钮
  44.     }

  45.     @Override
  46.     public void onClick(View v) {
  47.         int resid = v.getId(); // 获得当前按钮的编号
  48.         String inputText;
  49.         if (resid == R.id.ib_sqrt) { // 如果是开根号按钮
  50.             inputText = "√";
  51.         } else { // 除了开根号按钮之外的其它按钮
  52.             inputText = ((TextView) v).getText().toString();
  53.         }
  54.         Log.d(TAG, "resid=" + resid + ",inputText=" + inputText);
  55.         if (resid == R.id.btn_clear) { // 点击了清除按钮
  56.             clear("");
  57.         } else if (resid == R.id.btn_cancel) { // 点击了取消按钮
  58.             if (operator.equals("")) { // 无操作符,则表示逐位取消前一个操作数
  59.                 if (firstNum.length() == 1) {
  60.                     firstNum = "0";
  61.                 } else if (firstNum.length() > 0) {
  62.                     firstNum = firstNum.substring(0, firstNum.length() - 1);
  63.                 } else {
  64.                     Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show();
  65.                     return;
  66.                 }
  67.                 showText = firstNum;
  68.                 tv_result.setText(showText);
  69.             } else { // 有操作符,则表示逐位取消后一个操作数
  70.                 if (nextNum.length() == 1) {
  71.                     nextNum = "";
  72.                 } else if (nextNum.length() > 0) {
  73.                     nextNum = nextNum.substring(0, nextNum.length() - 1);
  74.                 } else {
  75.                     Toast.makeText(this, "没有可取消的数字了", Toast.LENGTH_SHORT).show();
  76.                     return;
  77.                 }
  78.                 showText = showText.substring(0, showText.length() - 1);
  79.                 tv_result.setText(showText);
  80.             }
  81.         } else if (resid == R.id.btn_equal) { // 点击了等号按钮
  82.             if (operator.length() == 0 || operator.equals("=")) {
  83.                 Toast.makeText(this, "请输入运算符", Toast.LENGTH_SHORT).show();
  84.                 return;
  85.             } else if (nextNum.length() <= 0) {
  86.                 Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
  87.                 return;
  88.             }
  89.             if (caculate()) { // 计算成功,则显示计算结果
  90.                 operator = inputText;
  91.                 showText = showText + "=" + result;
  92.                 tv_result.setText(showText);
  93.             } else { // 计算失败,则直接返回
  94.                 return;
  95.             }
  96.         } else if (resid == R.id.btn_plus || resid == R.id.btn_minus // 点击了加、减、乘、除按钮
  97.                 || resid == R.id.btn_multiply || resid == R.id.btn_divide) {
  98.             if (firstNum.length() <= 0) {
  99.                 Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
  100.                 return;
  101.             }
  102.             if (operator.length() == 0 || operator.equals("=") || operator.equals("√")) {
  103.                 operator = inputText; // 操作符
  104.                 showText = showText + operator;
  105.                 tv_result.setText(showText);
  106.             } else {
  107.                 Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
  108.                 return;
  109.             }
  110.         } else if (resid == R.id.ib_sqrt) { // 点击了开根号按钮
  111.             if (firstNum.length() <= 0) {
  112.                 Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
  113.                 return;
  114.             }
  115.             if (Double.parseDouble(firstNum) < 0) {
  116.                 Toast.makeText(this, "开根号的数值不能小于0", Toast.LENGTH_SHORT).show();
  117.                 return;
  118.             }
  119.             // 进行开根号运算
  120.             result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));
  121.             firstNum = result;
  122.             nextNum = "";
  123.             operator = inputText;
  124.             showText = showText + "√=" + result;
  125.             tv_result.setText(showText);
  126.             Log.d(TAG, "result=" + result + ",firstNum=" + firstNum + ",operator=" + operator);
  127.         } else { // 点击了其它按钮,包括数字和小数点
  128.             if (operator.equals("=")) { // 上一次点击了等号按钮,则清空操作符
  129.                 operator = "";
  130.                 firstNum = "";
  131.                 showText = "";
  132.             }
  133.             if (resid == R.id.btn_dot) { // 点击了小数点
  134.                 inputText = ".";
  135.             }
  136.             if (operator.equals("")) { // 无操作符,则继续拼接前一个操作数
  137.                 if (firstNum.contains(".") && inputText.equals(".")) {
  138.                     return; // 一个数字不能有两个小数点
  139.                 }
  140.                 firstNum = firstNum + inputText;
  141.             } else { // 有操作符,则继续拼接后一个操作数
  142.                 if (nextNum.contains(".") && inputText.equals(".")) {
  143.                     return; // 一个数字不能有两个小数点
  144.                 }
  145.                 nextNum = nextNum + inputText;
  146.             }
  147.             showText = showText + inputText;
  148.             tv_result.setText(showText);
  149.         }
  150.         return;
  151.     }

  152.     private String operator = ""; // 操作符
  153.     private String firstNum = ""; // 前一个操作数
  154.     private String nextNum = ""; // 后一个操作数
  155.     private String result = ""; // 当前的计算结果
  156.     private String showText = ""; // 显示的文本内容

  157.     // 开始加减乘除四则运算,计算成功则返回true,计算失败则返回false
  158.     private boolean caculate() {
  159.         if (operator.equals("+")) { // 当前是相加运算
  160.             result = String.valueOf(Arith.add(firstNum, nextNum));
  161.         } else if (operator.equals("-")) { // 当前是相减运算
  162.             result = String.valueOf(Arith.sub(firstNum, nextNum));
  163.         } else if (operator.equals("×")) { // 当前是相乘运算
  164.             result = String.valueOf(Arith.mul(firstNum, nextNum));
  165.         } else if (operator.equals("÷")) { // 当前是相除运算
  166.             if (Double.parseDouble(nextNum) == 0) { // 发现除数是0
  167.                 // 除数为0,要弹窗提示用户
  168.                 Toast.makeText(this, "除数不能为零", Toast.LENGTH_SHORT).show();
  169.                 // 返回false表示运算失败
  170.                 return false;
  171.             } else { // 除数非0,则进行正常的除法运算
  172.                 result = String.valueOf(Arith.div(firstNum, nextNum));
  173.             }
  174.         }
  175.         // 把运算结果打印到日志中
  176.         Log.d(TAG, "result=" + result);
  177.         firstNum = result;
  178.         nextNum = "";
  179.         // 返回true表示运算成功
  180.         return true;
  181.     }

  182.     // 清空并初始化
  183.     private void clear(String text) {
  184.         showText = text;
  185.         tv_result.setText(showText);
  186.         operator = "";
  187.         firstNum = "";
  188.         nextNum = "";
  189.         result = "";
  190.     }

  191. }
复制代码



孤星11 发表于 2022-12-17 17:41:12| 字数 46 来自手机 | 显示全部楼层

legs+ 发表于 2022-12-17 17:18
代码:

厉害,看到了。有时候我不敢相信是你写的。

legs+ 发表于 2022-12-17 17:48:39| 字数 116 | 显示全部楼层

孤星11 发表于 2022-12-17 17:41
厉害,看到了。有时候我不敢相信是你写的。

这是我是在GitHub下了一个代码改的,但是你知道告诉作假是不留痕迹的,就像discuz的代码你看不懂一样,GitHub那个很难懂,我改为通俗易懂了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则