练习9—数据计算

本文涉及的产品
云原生大数据计算服务MaxCompute,500CU*H 100GB 3个月
简介: 练习9—数据计算

题目

写一个简单的函数实现下面的功能:具有三个参数,完成对两个整型数据的加、减、乘、除四种操作,前两个为操作数,第三个参数为字符型的参数。

解题步骤

(1)定义变量;
(2)接收用户输入;
(3)函数计算;
(4)输出结果;

Java

import java.util.Scanner;

public class E20210814 {
    public static void main(String[] args) {
        int a1, b;
        char c;
        Scanner input = new Scanner(System.in);
        System.out.print("please enter two whole numbers:");
        a1 = input.nextInt();
        b = input.nextInt();
        System.out.print("please enter your computing type:[a-addition s-subtraction m-multiplication d-division]:");
        c = input.next().charAt(0);
        switch (c) {
            case 'a' -> System.out.format("%d+%d=%d", a1, b, a1 + b);
            case 's' -> System.out.format("%d-%d=%d", a1, b, a1 - b);
            case 'm' -> System.out.format("%d*%d=%d", a1, b, a1 * b);
            case 'd' -> {
                if (b == 0) {
                    System.out.println("the divisor is not 0 error!!!");
                } else {
                    System.out.format("%d/%d=%d", a1, b, a1 / b);
                }
            }
            default -> System.out.println("input error please try again!!!");
        }
    }
}

enhanced switch

        switch (c) {
            case 'a' -> System.out.format("%d+%d=%d", a1, b, a1 + b);
            case 's' -> System.out.format("%d-%d=%d", a1, b, a1 - b);
            case 'm' -> System.out.format("%d*%d=%d", a1, b, a1 * b);
            case 'd' -> {
                if (b == 0) {
                    System.out.println("the divisor is not 0 error!!!");
                } else {
                    System.out.format("%d/%d=%d", a1, b, a1 / b);
                }
            }
            default -> System.out.println("input error please try again!!!");
        }

说明

  1. 注意switch-case语句中case处的数据类型,因为设定了变量cchar类型,所以需要使用 c = input.next().charAt(0) 语句接收用户键盘上的单个字符输入,charAt() 方法用于返回指定索引处的字符,索引范围为从 0 到 length() - 1
  2. Java 中引入增强型switch结构,给出参考代码。主要特点如下:需要返回值、无需 break、使用箭头->、可进行 case 间的合并,以逗号分隔。

C语言

#include <stdio.h>

int calculate(int operand1, int operand2, char type)
{
    char a, s, m, d;
    switch (type)
    {
    case 'a':
        printf("%d+%d=%d", operand1, operand2, operand1 + operand2);
        break;
    case 's':
        printf("%d-%d=%d", operand1, operand2, operand1 - operand2);
        break;
    case 'm':
        printf("%d*%d=%d", operand1, operand2, operand1 * operand2);
        break;
    case 'd':
    {
        if (operand2 == 0)
        {
            printf("the divisor is not 0 the error please try again");
            break;
        }

        else
        {
            printf("%d/%d=%d", operand1, operand2, operand1 / operand2);
            break;
        }
    }
    default:
        printf("if the type of operation is not specified please re enter!!!");
    }
}

int main()
{
    int a, b;
    char c;
    printf("please enter two integers:");
    scanf("%d%d", &a, &b);
    printf("please enter your computing type:[a-addition s-subtraction m-multiplication d-division]:");
    getchar();
    scanf("%c", &c);
    calculate(a, b, c);
    return 0;
}

说明

因为有四种计算类型,所以我们使用 switch-case语句解决,注意除法计算中除数不为 0 的条件判断,且 case后需为常量,这里使用字符做判断条件,加上单引号 ‘’变为字符常量。
相关实践学习
基于MaxCompute的热门话题分析
Apsara Clouder大数据专项技能认证配套课程:基于MaxCompute的热门话题分析
相关文章
|
17天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
11天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1035 35
|
11天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
800 55
|
9天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
690 11
下一篇
开通oss服务