Java日志第53天 2020.8.29
例5.6 用函数处理例5.5
public class Demo5_6 {
public static void main(String[] args) {
int row=0, colum=0, max;
int[][] a = new int[][]{{5,12,23,56}, {19,28,37,46}, {-12,-34,6,8}};
max = a[0][0];
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 3; j++) {
max = max_value(a[i][j],max);
if (max == a[i][j]){
row = i+1;
colum = j+1;
}
}
}
System.out.println("max = "+max+", row = "+row+", colum = "+colum);
}
private static int max_value(int x, int max) {
if (x>max) {
return x;
} else
return max;
}
}
例5.7 用选择法对数组中10个整数按由小到大排序
import java.util.Scanner;
public class Demo5_7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[10];
System.out.print("Enter the origial array:");
for (int i = 0; i < 10; i++) {
a[i] = sc.nextInt();
}
select_sort(a, 10);
System.out.println("The sorted array:");
for (int i = 0; i < 10; i++) {
System.out.print(a[i]+" ");
}
}
private static void select_sort(int array[], int n) {
int k, temp;
for (int i = 0; i < n-1; i++) {
k = i;
for (int j = i+1; j < n; j++)
if (array[j] < array[k])
k = j;
temp = array[k];
array[k] = array[i];
array[i] = temp;
}
}
}
例5.8 有一个3×4的矩阵,求矩阵中所有元素中的最大值。要求用函数处理。
public class Demo5_8 {
public static void main(String[] args) {
int[][] a = new int[][] {{11,32,45,67},{22,44,66,88},{15,72,43,37}};
System.out.println("Max value is "+max_value(a));
}
private static int max_value(int array[][]){
int max;
max = array[0][0];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (array[i][j]>max)
max = array[i][j];
}
}
return max;
}
}
例5.9 设计和输出一个钻石图形。
public class Demo5_9 {
public static void main(String[] args) {
char[][] diamond = new char[][] {{' ',' ','*',' ',' '}, {' ','*',' ','*',' '},{'*',' ',' ',' ','*'},
{' ','*',' ','*',' '}, {' ',' ','*',' ',' '}};
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(diamond[i][j]);
}
System.out.println();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南