java三行三列代码 三行三列的html代码

用java写程序,输入3行3列,求最大值、最小值、平均值、每一行排序、对角线的和

import java.util.Arrays;

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网页空间、营销软件、网站建设、碌曲网站维护、网站推广。

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int nums[][] = scanf(3, 3);

System.out.println("最大值:" + max(nums));

System.out.println("最小值:" + min(nums));

System.out.println("平均值:" + avg(nums));

int sum = diagonalSum(nums);

sort(nums);

System.out.println("对角线之和:" + sum);

}

// 输入

public static int[][] scanf(int row,int col){

int[][] nums = new int[row][col];

System.out.println("请输入三行三列数阵");

Scanner scanner = new Scanner(System.in);

for (int i = 0; i row; i++) {

for (int j = 0; j col; j++) {

int num = scanner.nextInt();

nums[i][j] = num;

}

}

return nums;

}

// 最大值

public static int max(int[][] nums){

int max = nums[0][0];

for (int i = 0; i nums.length; i++) {

for (int j = 0; j nums[i].length; j++) {

if(max nums[i][j]){

max = nums[i][j];

}

}

}

return max;

}

// 最小值

public static int min(int[][] nums){

int min = nums[0][0];

for (int i = 0; i nums.length; i++) {

for (int j = 0; j nums[i].length; j++) {

if(min nums[i][j]){

min = nums[i][j];

}

}

}

return min;

}

// 平均值

public static float avg(int[][] nums){

int sum = 0;

int count = 0;

for (int i = 0; i nums.length; i++) {

for (int j = 0; j nums[i].length; j++) {

sum += nums[i][j];

count++;

}

}

return sum / (count * 1.0f);

}

// 排序

public static void sort(int[][] nums) {

for (int i = 0; i nums.length; i++) {

Arrays.sort(nums[i]);

System.out.print("第" +(i+1) + "行排序:");

for (int j = 0; j nums[i].length; j++) {

System.out.print(nums[i][j] + " ");

}

System.out.println();

}

}

// 对角线的和

public static int diagonalSum(int[][] nums){

int sum = 0;

for (int i = 0; i nums.length; i++) {

for (int j = 0; j nums[i].length; j++) {

if(i==j || i+j == nums.length-1){

sum += nums[i][j];

}

}

}

return sum;

}

}

使用Java语言编写一段程序,实现从控制台输入并输出一个3行3列的数组。

import java.util.Scanner;

public class P

{

public static void main(String[] args)

{

int[][] a=new int[3][3];

Scanner sc = new Scanner(System.in);

System.out.println("请输入9个整数:");

for(int i=0;i3;i++)

{

for(int j=0;j3;j++)

{

a[i][j]=sc.nextInt();

}

}

System.out.println("你输入的3x3数组的数据是:");

for(int i=0;i3;i++)

{

for(int j=0;j3;j++)

{

System.out.print(a[i][j]+" ");

}

System.out.println();

}

}

}

java中定义一个二维数组,3行3列,但是只给第一行赋值,其他两行之后按输入再赋值,代码应该怎么写?

Object array[][] = new Object[3][3];

array[0] = new Object[3];

array[0][0] = new Object();

array[0][1] = new Object();

array[0][2] = new Object();

这个就是实现了只给第一行赋值,而另两行都是空的,以后随时想赋值都可以按照上面的代码继续。当然你也可以写个循环比如

Object array[][] = new Object[3][3];

array[0] = new Object[3];

for(int i = 0; iarray.length; i++)

array[0][i] = new Object();

用Java定义一个三行三列的二维数组,要求每行之和等于每列之和

其实就是魔术方阵。。。

给你写了个比较通用的哈。。

public class MagicSquare {

/**

* @param args

*/

//注意只能产生奇数的魔术方阵 偶数的规律不一样

public static void main(String[] args) {

// TODO Auto-generated method stub

int[][] square = generateSquare(3);

for(int[] nums : square) {

for(int num : nums) {

System.out.printf("%-4d", num);

}

System.out.println();

}

}

//产生魔术方阵的方法 注意只能是奇数方阵哈 参数count就是你想要产生几阶的

public static int[][] generateSquare(int count) {

int[][] square = new int[count][count];

int row = 0;

int col = count / 2;

square[row][col] = 1;

for(int i = 2; i = count * count; i++) {

row--;

col--;

if(row 0) {

row = count - 1;

}

if(col 0) {

col = count - 1;

}

if(square[row][col] != 0) {

if(row == count - 1) {

row = 0;

} else {

row++;

}

if(col == count - 1) {

col = 0;

} else {

col++;

}

row++;

if(row count - 1) {

row = 0;

}

}

square[row][col] = i;

}

return square;

}

}


当前文章:java三行三列代码 三行三列的html代码
网页URL:http://myzitong.com/article/hjocjj.html