题目汇总一

Question One

编写一个应用程序,输出100以内的全部偶数,并以10个一行的形式输出。

public class Main{public static void main(String[] args){int cnt = 0;for(int i = 2; i <= 100; i += 2){cnt ++;System.out.print(i);if(cnt % 10 != 0) System.out.print(' ');else System.out.println();}}
}

Question Two

声明一些变量类型,用作练习

public class Main{public static void main(String[] args){byte a1 = 0x55;byte a2 = 95;System.out.println(a1 + " " + a2);short b = 0x55ff;System.out.println(b);int i = 1000;long l1 = 0xffff;long l2 = 2147483648L;System.out.println(l1 + " " + l2);char c = 'a';float f = 0.23F;System.out.println(f);double d1 = 0.007;double d2 = 0.7E-3;System.out.println(d1 + " " + d2);boolean fg = false;String s = "123";}
}

Question Three

练习 switch 语句语法

import java.util.Scanner;public class Main{public static void main(String[] args){Scanner sc = new Scanner(System.in);int x = sc.nextInt();switch (x * 2) {case 2 :System.out.println("Case One");break;case 3 :System.out.println("case Two");break;case 4 :System.out.println("case Three");break;default :System.out.println("case Four");break;}}
}

Question Four

随机数生成练习

public class Main{public static void main(String[] args){for(int i = 1; i <= 10; i ++){int num = (int)(100 * Math.random());System.out.print(num + " ");}System.out.println();for(int i = 1; i <= 10; i ++){// Random 生成 [0, 1) 的浮点数double num = Math.random();System.out.print(num + " ");}}
}

Question Five

输入 5 个整数求平均值

import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);double[] d = new double[5];double avg = 0.0d;for(int i = 0; i < 5; i ++){System.out.println("请输入第" + (i + 1) + "个整数: ");d[i] = sc.nextDouble();avg += d[i];}System.out.println("平均分数是: " + avg / 5.0);}
}

注意 : (同 C++ 的除法运算法则)

System.out.println(5 / 3); // 1
System.out.println(5.0 / 3); // 1.6666666666666667

Question Six

保留指定小数位数

import java.util.Scanner;public class Main{public static void main(String[] args) {System.out.printf("Two decimal places : %.2f", 5.0 / 3); // 1.6666666666666667System.out.printf("%n");System.out.printf("Three decimal places : %.3f", 5 / 3.0);}
}

Question Seven

GUI 实现 a + b,加入异常抛出

import javax.swing.*;public class Main {public static void main(String[] args) {try {// 用InputDialog获取用户输入的第一个数字int num1 = Integer.parseInt(JOptionPane.showInputDialog("Input first number:"));// 用InputDialog获取用户输入的第二个数字int num2 = Integer.parseInt(JOptionPane.showInputDialog("Input second number:"));// 计算两个数字的和int sum = num1 + num2;// 显示结果,使用INFORMATION_MESSAGE以显示信息图标JOptionPane.showMessageDialog(null, "Sum is " + sum,"Result", JOptionPane.INFORMATION_MESSAGE);}catch (NumberFormatException e) {// 捕获NumberFormatException并显示错误消息JOptionPane.showMessageDialog(null, "Invalid input. " +"Please enter a valid integer.", "Error", JOptionPane.ERROR_MESSAGE);}}
}

Question Eight

编写一个函数fun,它的功能是:求出1000以内能被7或11整除,但不能同时被7和11整除的数,存放到数组myarray中,fun函数返回该数组。在main()函数中输出该数组中的所有元素。

public class Main{public static void main(String[] args) {for(var x : fun()){System.out.print(x + " ");}}public static int[] fun(){int cnt = 0;int[] arr = new int[100];for(int i = 1; i <= 100; i ++){if((i % 7 == 0 || i % 11 == 0) && (i % 77 != 0)){arr[cnt ++] = i;}}return java.util.Arrays.copyOf(arr, cnt);}
}

需要注意 :

1. 如果想直接返回数组,这样写 : return new int[] {1, 2, 3}; 不需要指定长度
2. 返回子数组这样写,return java.util.Arrays.copyOf(arr, cnt)

Question Nine

区间复制部分数组 + 顺便实现 9*9 Multiplication

import java.util.Arrays;
public class Main{public static void main(String[] args) {int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7, 8};for(int x : arr){System.out.print(x + " ");}System.out.println();int[] part = Arrays.copyOfRange(arr, 1, 4);System.out.println(Arrays.toString(part));Multiplication();}public static void Multiplication(){for(int i = 1; i <= 9; i ++){for(int j = 1; j <= i; j ++){System.out.print(i + "*" + j + "=" + (i * j));if(j == i) System.out.println();else System.out.print(" ");}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/864434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于内存认证的 Spring Security

在Spring Security中&#xff0c;基于内存的认证是一种简单直接的方式&#xff0c;适合于开发环境或者小型应用&#xff0c;它允许我们直接在安全配置中定义用户。在这篇文章中&#xff0c;我们将详细介绍如何使用Spring Security进行基于内存的认证配置。 开始之前 首先&…

以太网基础知识

文章目录 一、以太网&#xff08;Ethernet&#xff09;介绍二、协议介绍三、什么是PHY&#xff1f;1.标准接口协议&#xff1a;2.寄存器配置&#xff1a;3.自动协商&#xff1a; 四、时序4.1RGMII接口时序4.1.1 对其模式4.1.2 延时模式&#xff08;常用&#xff09; 4.2MDIO接口…

小学生学习成绩不理想,背后重要的因素,在于一些错误学习方法

小学生学习成绩不理想&#xff0c;背后重要的因素&#xff0c;在于一些错误学习方法 学习方法不当的学生&#xff0c;抓不住学习的重点和难点&#xff0c;找不到学习上的突破口而浪费了时间与精力。小学生学习成绩不理想&#xff0c;以下这些错误学习方法需要注意。 一…

WEB01MySQL安装和数据库

第一天、WEB课程 web课程主要讲三部分内容 数据库 数据库介绍 什么是数据库 数据存储的仓库&#xff0c;其本质也是一个文件系统 数据库会按照特定的格式对数据进行存储&#xff0c;用户可以对数据库中的数据进行增加&#xff0c;修改&#xff0c;删除及查询操作。 数据库…

6-24 数据结构考题 - 顺序查找

6-24 数据结构考题 - 顺序查找 - &#xff08;32&#xff09;专科段数据结构专项练习&#xff08;2024版&#xff09; (pintia.cn) // 顺序表中第一个数据元素存储在 T.R[1] // 由题可知&#xff0c;下标从1开始int Search_Seq (SSTable T,ElemType k) { int i;T.R[0] k;…

RBAC介绍

一、前言 Kubernetes 中的角色基于访问控制&#xff08;Role-Based Access Control&#xff0c;RBAC&#xff09;是一种权限管理机制&#xff0c;用于控制用户和应用程序对 Kubernetes 资源的访问。RBAC 通过定义一组角色&#xff08;Roles&#xff09;和角色绑定&#xff08;R…

python爬虫之数据解析操作

python爬虫之scrapy数据解析操作 scrapy数据解析操作&#xff1a;利用scrapy爬取段子标题和内容 终端输入&#xff1a; 1、scrapy startproject qiushiPro创建爬虫文件夹 2、cd qiushiPro进入qiushiPro文件夹 3、scrapy genspider qiushi www.xxx.com创建爬虫代码qiushi.py 4、…

2024/6/30 英语每日一段

Years of economic and political turbulence have brought stagnation.“In a world where there is more risk and uncertainty, people become reluctant to voluntarily move jobs and find better jobs,” says Manning. At the same time, businesses have cut back on i…

企业互联网建站源码系统 附带完整的安装代码包以及搭建部署教程

系统概述 企业互联网建站源码吸系统是一款集众多先进功能于一身的建站工具。它提供了丰富的模板和组件&#xff0c;允许企业根据自身需求和品牌形象进行个性化定制&#xff0c;快速搭建出具有独特风格的网站。 代码示例 系统特色功能一览 1.用户友好界面&#xff1a;系统采用…

Java GUI编程:跨平台应用的设计与开发

Java GUI编程&#xff1a;跨平台应用的设计与开发 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 引言 图形用户界面&#xff08;GUI&#xff09;是现代软件应…

大数据生态体系中各组件的区别面试题(更新)

一、MapReduce与Spark有什么区别&#xff1f; 1、处理方式: MapReduce基于磁盘处理数据&#xff0c;将中间结果保存到磁盘中,减少了内存占用&#xff0c;计算速度慢。 基于内存处理数据&#xff0c;将计算的中间结果保存到内存中&#xff0c;计算速度快。2、资源申请方式&…

你还搞不懂串口的格式转换问题吗?

相信大多数人在使用串口传输不同单片机之间的数据时都会运到数据格式怎么对应起来的问题&#xff0c;今天我们就来聊聊&#xff01; 在开始之前我插一个内容&#xff0c;就是不同的单片机之间的电平可能不相同&#xff0c;是不能直接使用杜邦线连接通信的&#xff0c;需要进行电…

Lfu缓存在Rust中的实现及源码解析

一个 lfu(least frequently used/最不经常使用页置换算法 ) 缓存的实现&#xff0c;其核心思想是淘汰一段时间内被访问次数最少的数据项。与LRU&#xff08;最近最少使用&#xff09;算法不同&#xff0c;LFU更侧重于数据的访问频率而非访问的新鲜度。 LFU的原理与实现机制 普通…

带安全启动—Ubuntu系统—手动安装Nvidia驱动

教程1&#xff1a;在启用安全启动的 Fedora 中安装英伟达驱动 教程2&#xff1a;UEFI安全启动模式下安装Ubuntu的NVIDIA显卡驱动 1. 搜索合适的驱动 Nvidia驱动官网 选择这个 驱动(.run)链接 2. 安装必要的软件依赖 CUDA底层用C写的&#xff0c;因此导入编译器 sudo apt i…

大模型压缩:基于贝叶斯优化的自适应低秩分解

1.方法 1.1 基于特征的高维空间低秩分解 PCA已经是老朋友了&#xff0c;每次一说主成分都会出现PCA。这篇文章1利用预训练数据的子集作为校准数据集 D c a l { x i } i 1 n \mathcal{D}_{cal}\{x_{i}\}_{i1}^{n} Dcal​{xi​}i1n​&#xff0c;首先用校准数据集的样本协方差…

ts语法---数据类型,interface和type的用法

ts的数据类型 ts的数据类型自上而下的分级有 第一层 any-任意类型和unknow类型&#xff0c; 第二层 原型链的Object类型&#xff0c; 第三层 js类的类型 第四层 标准的typescript类型 第五层 对应的实例数据类型 第六层 never类型&#xff0c;never表示不合理&#xff0c…

SHELL脚本学习——自动备份

1、 tar 命令 tar {operation} [options…] [file]… &#xff1a;压缩文件 operation&#xff1a; -c 创建压缩包 -x 提取文件 -t 列出文件 -f 指定文件名 -z 通过gzip指令处理备份文件 tar命令详细介绍见&#xff1a;https://www.runoob.com/linux/linux-comm-tar.html) 例&…

整除分块的题目

链接 思路&#xff1a; 求1到n中的因数个数和等价于求,设x为因子&#xff0c;就是求x在1到n里出现了几次&#xff0c;求1到n里是x的倍数的数有几个&#xff0c;即n/x。需要用整除分块&#xff0c;n/i的值是分块分部的&#xff0c;右端点是n/&#xff08;n/i&#xff09;。 代…

Application Studio 学习笔记(3)

一、工具栏按钮 1、panel控件添加工具栏按钮 展开panel控件的Advanced属性并点击Action Data&#xff0c;进入Action Data编辑界面 新增Action Data数据&#xff0c;Sequence设定工具按钮的显示顺序 默认工具按钮会显示在弹出工具栏中 勾选Add to Primary ToolBar后&#xff…

deepin基于apt-mirror同步软件源及构建本地内网源

1.安装apt-mirror sudo apt install -y apt-mirror2.配置apt-mirror(/etc/apt/mirror.list) sudo cp /etc/apt/mirror.list /etc/apt/mirror.list.deepin.bak #备份配置文件 sudo gedit /etc/apt/mirror.list修改如下&#xff1a; deb [trustedyes] https://mirrors.bfsu.ed…