「java数组程序」Java的数组
今天给各位分享java数组程序的知识,其中也会对Java的数组进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java 程序编写 数组
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int rows, cols; // 行数与列数
System.out.print("Enter number of rows and columns"
+ " for the matrix: ");
rows = stdIn.nextInt();
cols = stdIn.nextInt();
int[][] arrA = new int[rows][cols];
arrA = readArray(stdIn, rows, cols);
System.out.println("the matrix:");
printArray(arrA);
getMax(arrA);
getMin(arrA);
}
/*******************************************************/
/* 读入矩阵数据 */
public static int[][] readArray(Scanner scan,
int numRows, int numCols) {
int[][] arr = new int[numRows][numCols];
System.out.println("Enter values for " + numRows + "x" + numCols + " matrix:");
for (int row = 0; row arr.length; row++) {
for (int col = 0; col arr[0].length; col++) {
arr[row][col] = scan.nextInt();
}
}
return arr;
}
/*******************************************************/
/* 打印矩阵 */
public static void printArray(int[][] arr) {
for (int row=0; rowarr.length; row++) {
for (int col=0; colarr[0].length; col++) {
System.out.printf("%5d", arr[row][col]);
}
System.out.println();
}
}
public static void getMax(int[][] arr) {
int max = arr[0][0];
for (int row=0; rowarr.length; row++) {
for (int col=0; colarr[0].length; col++) {
if(arr[row][col]max){
max = arr[row][col];
}
}
}
System.out.println("最大值为:"+max);
for (int row=0; rowarr.length; row++) {
for (int col=0; colarr[0].length; col++) {
if(arr[row][col]==max)
System.out.println("最大值坐标为:("+(row+1)+","+(col+1)+")");
}
}
}
public static void getMin(int[][] arr) {
int min = arr[0][0];
for (int row=0; rowarr.length; row++) {
for (int col=0; colarr[0].length; col++) {
if(arr[row][col]min){
min = arr[row][col];
}
}
}
System.out.println("最小值为:"+min);
for (int row=0; rowarr.length; row++) {
for (int col=0; colarr[0].length; col++) {
if(arr[row][col]==min)
System.out.println("最小值坐标为:("+(row+1)+","+(col+1)+")");
}
}
}
}
JAVA编写程序使用数组
public static void main(String[] args) {
int[] arr = new int[20];
//初始化
arr[0] = 1;
arr[1] = 1;
//遍历计算
for (int i = 2; i 20; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
//打印
for (int i = 1; i 21; i++) {
System.out.println("Fibonacci "+i+" = "+arr[i-1]);
}
}
数组——JAVA程序设计
程序代码如下:(一维数组)import java.util.*;
public class Array
{
public static void main(String args[])
{
int i,j;
int a[]={1,2,3,4,5,6,7,8,9,0};//10
//一维数组Buuble_sort
for(i=0;i10;i++)
{
for(j=0;j10-i-1;j++)
{
if(a[j]a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("冒泡排序结果:");
for(i=0;i10;i++)
System.out.print(a[i]+" ");
System.out.println();}}程序运行结果:
关于Java中数组的程序
public
static
void
main(String[]
args)
{
int[]
score
=
{
18,
25,
7,
36,
13,
2,
89,
63
};
int
min
=
score[0];
int
t=0;
for
(int
i
=
1;
i
score.length;
i++)
{
if
(score[i]
min)
{
min
=
score[i];
t=i;
}
}
System.out.println("这组数中最少的积分数是:"
+
min);
System.out.println("它在数组中的原始位置是:"
+
(t+1));
这么写
关于java数组程序和Java的数组的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-14,除非注明,否则均为
原创文章,转载请注明出处。