锦州市网站建设_网站建设公司_在线商城_seo优化
2025/12/17 9:43:02 网站建设 项目流程

石头迷阵游戏

package com.itheima; public class ArrayTest6 { public static void main(String[] args) { start(5); //完成数字华容道的初始化和随机顺序 } public static void start(int n){ //定义一个二维数组存储字到表 int[][] arr = new int[n][n]; //遍历二维数组,给二位数组赋值 int count = 1; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j] = count++; } } for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { int m = (int) (Math.random()*arr.length); int p = (int) (Math.random()*arr.length); int temp = arr[m][p]; arr[m][p] = arr[i][j]; arr[i][j] = temp; } } printArray(arr); } public static void printArray(int[][] arr) { for(int i =0;i<arr.length;i++){ for(int j =0;j<arr[i].length;j++){ System.out.print(arr[i][j]+"\t"); } System.out.println(); } } }

对象(类)

案例

ps:封装:把数据和对数据的处理放到同一个类中去

Student.java
public class Student { String name; double chinese; double math; public void printAllScore(){ System.out.println(name + "的总成绩是:" + (chinese + math)); } public void printAverageScore(){ System.out.println(name + "的平均成绩是:" + (chinese + math) / 2); } }
Test2.java
public class Test2 { public static void main(String[] args) { Student s1 = new Student(); s1.name = "播妞"; s1.chinese = 100; s1.math = 100; s1.printAllScore ();//s1调用则自动寻找s1中的值 s1.printAverageScore(); Student s2 = new Student(); s2.name = "播仔"; s2.chinese = 59; s2.math = 100; s2.printAllScore (); s2.printAverageScore(); } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询