java数组简单逻辑代码
2024-08-30 07:38:38
package cuteSnow; public class HelloWorld {
// 遍历数组里面的每个数字
public static void print(int[] array){
String tip = "数组中每个元素分别为:";
for(int i = 0; i<array.length; i++) {
int temNum = array[i];
tip = tip + temNum +" ";
}
System.out.print(tip);
}
// 判断给定数据是否为6的倍数
public static void sixMultiple(int[] array) {
String tip = "6的倍数有:";
for(int i = 0; i<array.length; i++) {
int temNum = array[i];
if( temNum % 6 == 0) {
tip = tip + temNum +" ";
}
}
System.out.print(tip);
}
// 将数组第一个数字和最后一个数字对调
public static void exchange(int[] array) {
String tip = "数组第一个数字和最后一个数字对调之后的数组为:[";
int tem = array[array.length - 1];//最后一个的值存起来
array[array.length - 1] = array[0];
array[0] = tem;
for(int i = 0; i<array.length; i++) {
int temNum = array[i];
tip = tip + temNum;
if(i < array.length-1) {
tip = tip + ",";
}
}
System.out.print(tip+"]");
}
// 将数组的元素倒序排列
public static void desc(int[] array) {
String tip = "数组的元素倒序排列为:[";
for(int i = 0; i<array.length/2; i++) {
int tem = array[i];
array[i] = array[array.length - i - 1];
array[array.length - 1 - i] = tem;
}
for(int i = 0; i<array.length; i++) {
int temNum = array[i];
tip = tip + temNum;
if(i < array.length-1) {
tip = tip + ",";
}
}
System.out.print(tip+"]");
}
//求1000以内的质数,存放到数组中
public static void prime() {
String tip = "1000以内的质数组合成的数组为:[";
int[] prime = new int[50]; // 最多存放50个
int count = 0;
for(int i = 2;i <= 1000;i++) {
boolean isPrime = true;
for(int k = 2;k < i;k++) {
if(i % k == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
prime[count] = i;
count++;
if(count >= prime.length) {
break;
}
}
}
for(int i = 0; i < count; i++) {
int temNum = prime[i];
tip = tip + temNum;
if(i < count-1) {
tip = tip + ",";
}
}
System.out.print(tip+"]");
} // 主函数
public static void main(String[] args) {
// System.out.println("helloworld");
int[] array = { 29, 90, 48, 92};
// 调用
print(array);
System.out.print("\n");
sixMultiple(array);
System.out.print("\n");
exchange(array);
System.out.print("\n");
desc(array); // 由于数组时引用类型,所以此处是数组值受exchange(array)方法的影响值为[92,90,48,29]
System.out.print("\n");
prime();
}
// 上述运行结果
数组中每个元素分别为:29 90 48 92
6的倍数有:90 48
数组第一个数字和最后一个数字对调之后的数组为:[92,90,48,29]
数组的元素倒序排列为:[29,48,90,92]
1000以内的质数组合成的数组为:[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229]
}
最新文章
- 查看数据库表的数据量和SIZE大小的脚本修正
- 常用的java类型转json的转换类
- Eclipse仿Visual AssistX 编辑着色插件
- http://highscalability.com/blog/2015/5/18/how-mysql-is-able-to-scale-to-200-million-qps-mysql-cluster.html
- C#.NET序列化XML、JSON、二进制微软自带DLL与newtonsoft(json.net)
- LNMP编译安装教程
- Python自动化 【第十篇】:Python进阶-多进程/协程/事件驱动与Select\Poll\Epoll异步IO
- hdu_1875_畅通工程再续 prim和kruskal
- Javascript Promise 学习笔记
- FreeBsdb FAMP Lamp环境
- JavaScript表单验证年龄
- JavaEE(16) - JPA生命周期及监听器
- 第一个Android crackme(2016-05)
- SourceTree 无法查看组织仓库
- 【Spring】面向切面之AOP
- echarts.js--前端可视化数据图形
- Git:git diff 命令详解
- Linux下设置python脚本文件为服务
- webbench的安装
- js 读写 cookie 简单实现