二重循环
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i=1; i<=3; i++) {
System.out.println("请输入第" + i + "班级的成绩");
int sum = 0;
for (int j=1; j<=4; j++) {
System.out.print("第" + j + "个学生的成绩:");
sum += scanner.nextInt();
}
System.out.println("第" + i + "班级的平均成绩:" + sum / 4.0);
}
}
}
正方形
直角三角形
public class Demo02 {
public static void main(String[] args) {
int number = 3;
for (int i=1; i<=number; i++) {
for (int j=1; j<=number; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
倒直角三角形
public class Demo03 {
public static void main(String[] args) {
int number = 5;
for (int i=1; i<=number; i++) {
for (int j=1; j<=number-i+1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
等腰三角形
public class Demo04 {
public static void main(String[] args) {
int number = 5;
for (int i=1; i<=number; i++) {
for (int j=1; j<=number-i; j++) {
System.out.print(" ");
}
for (int j=1; j<=2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
九九乘法表
public static void main(Stringl] args) {
for (int 1=1;1c=9;i++) {
for (int j=1; j<=1; i++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
冒泡
public class Demo10 {
public static void main(String[] args) {
int[] arr = {10,8,4,5,2,-1,56,33};
for (int i=0; i<arr.length-1; i++) {
for (int j=0; i<arr.length-1-i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
System.out.printin(Arrays.toString(arr));
}
}
二维数组
public class Demo11 {
public static void main(string[] args) {
int[][] arr = {
{10,8,4,5,2,-1,56,33,1},
{110,18,14,15,12,-11, 156,133,11},
{1110,118,114,115,112,-111,1156,1133,113}
};
for (int i=0; i<arr.length; i++) {
for (int i=0; j<arr[il.length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.printin();
}
}
}