java sixth week homework

Define integer arrays of length 5, enter their values, and use bubble sort to output

package dome6_2sixweek_sunday;

import java.util.Scanner;

public class a {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] whether = new int [5];
		Scanner input = new Scanner(System.in);
		for (int i = 0; i < num.length; i++) {
			System.out.print ("Please enter the number" + (i + 1) + "Number:");
			num[i] = input.nextInt();
		}
		for (int i = 0; i < num.length - 1; i++) {
			for (int j = 0; j < num.length - 1 - i; j++) {
				if (num[j] > num[j + 1]) {
					int temp = num[j];
					num [j] = num [j + 1];
					num[j + 1] = temp;
				}
			}
		}
		for (int i = 0; i < num.length; i++) {
			System.out.print(num[i] + " ");
		}

	}

}

  

Define the array {34,22,35,67,45,66,12,33}, enter a number a, find if it exists in the array, if there is an output subscript, there is no output "not found"

package dome6_2sixweek_sunday;

import java.util.Scanner;

public class b {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 34, 22, 35, 67, 45, 66, 12, 33 };
		Scanner input = new Scanner(System.in);

		System.out.print ("Please enter a number:");

		int n = input.nextInt();
		for (int i = 0; i < arr.length; i++) {
			if (n == arr[i]) {
				System.out.println(i);
				break;
			} else {
				System.out.println("not found");
				break;
			}
		}

	}

}

  

Use the matrix to output the value of a double-type two-dimensional array (the length is 5, 4 and the value is set by yourself)

package dome6_2sixweek_sunday;

public class c {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 8, 7, 6 }, { 5, 4, 3, 2 }, { 1, 2, 3, 4 } };
		for (int i = 0; i < arr.length; i++) {
			System.out.println();
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "	");
			}
		}
	}

}

  

Define a two-dimensional array (lengths are 3, 4, and the value is set by yourself), find the maximum value of the two-dimensional array

package dome6_2sixweek_sunday;

public class d {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 8, 7, 6 } };
		int max = arr[0][0];

		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
			}
		}
		System.out.println ("Maximum value is" + max);

	}

}

  

 

Guess you like

Origin www.cnblogs.com/a000/p/12684672.html