Java编程入门程序实践

java初学者,练习几个入门程序!

乘法表

public class Print99 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("99 form");
        System.out.print(" ");
        for(int i=1;i<9;i++)
            System.out.print(i+" ");
        System.out.println();
        for(int i=1;i<=9;i++){
            System.out.print(i+" ");
            for(int j=1;j<=9;j++)
                if(j<=i)
                    System.out.print(i*j+" ");
            System.out.println();
        }
    }

}

执行结果:

99 form
 1 2 3 4 5 6 7 8 
1 1 
2 2 4 
3 3 6 9 
4 4 8 12 16 
5 5 10 15 20 25 
6 6 12 18 24 30 36 
7 7 14 21 28 35 42 49 
8 8 16 24 32 40 48 56 64 
9 9 18 27 36 45 54 63 72 81 

螺旋矩阵

import java.io.*;
public class RingDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String strln="";
        System.out.print("now input your row number: ");
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader buff = new BufferedReader(input);
        try
        {
            strln = buff.readLine();
        }catch(IOException e)
        {
            System.out.println(e.toString());
        }
        int int1 = Integer.parseInt(strln);
        int n = int1;
        System.out.println("this row "+n+" is spiral array:");
        int intA = 1;
        int[][] array = new int[n][n];
        int intB;
        if(n%2 != 0)
        {
            intB = n/2 + 1;
        }else
            intB = n/2;
        for(int i=0;i<intB;i++)
        {
            for(int j=i;j<n-i;j++)
            {
                array[i][j] = intA;
                intA++;
            }
            for(int k=i+1;k<n-i;k++)
            {
                array[k][n-i-1] = intA;
                intA++;
            }
            for(int l=n-i-2;l>=i;l--)
            {
                array[n-i-1][l] = intA;
                intA++;
            }
            for(int m=n-i-2;m>i;m--)
            {
                array[m][i] = intA;
                intA++;
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.println(array[i][j]+" ");
            }
            System.out.println();
        }
    }

}

执行结果:

now input your row number: 7
this row 7 is spiral array:
1 
2 
3 
4 
5 
6 
7 

24 
25 
26 
27 
28 
29 
8 

23 
40 
41 
42 
43 
30 
9 

22 
39 
48 
49 
44 
31 
10 

21 
38 
47 
46 
45 
32 
11 

20 
37 
36 
35 
34 
33 
12 

19 
18 
17 
16 
15 
14 
13 

打印结果为什么出现换行呢?

数组排序


public class SelectionSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] intArray = {12,11,45,6,8,40,33,3,60};
        int keyValue;
        int index;
        int temp;
        System.out.println("排序前的数组:");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
        for(int i=0;i<intArray.length;i++)
        {
            index = i;
            keyValue = intArray[i];
            for(int j=i;j<intArray.length;j++)
                if(intArray[j]<keyValue)
                {
                    index = j;
                    keyValue =  intArray[j];
                }
            temp = intArray[i];
            intArray[i] = intArray[index];
            intArray[index] = temp;
        }
        System.out.println("\n排序后的数组:");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
    }

}

执行结果:

排序前的数组:
12 11 45 6 8 40 33 3 60 
排序后的数组:
3 6 8 11 12 33 40 45 60 

又有换行问题?

冒泡排序

public class BubbleSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] intArray = {12,33,44,5,67,13,56,20};
        System.out.println("排序前的数组:");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
        System.out.println();
        int temp;
        for(int i=0;i<intArray.length;i++)
        {
            for(int j=i;j<intArray.length;j++)
            {
                if(intArray[j]<intArray[i])
                {
                    temp = intArray[i];
                    intArray[i]=intArray[j];
                    intArray[j]=temp;

                }
            }
        }
        System.out.println("排序后的数组");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
    }

}

执行结果:

排序前的数组:
12 33 44 5 67 13 56 20 
排序后的数组
5 12 13 20 33 44 56 67 

快速排序

public class QuickSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] intArray = {23,14,67,3,42,16,67,88,37,56};
        int keyValue;
        int index;
        int temp;
        System.out.println("排序前的数组:");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
        System.out.println();
        for(int i=0;i<intArray.length;i++)
        {
            index = i;
            keyValue = intArray[i];
            for(int j=i;j<intArray.length;j++)
                if(intArray[j]<keyValue)
                {
                    index = j;
                    keyValue = intArray[j];

                }
            temp = intArray[i];
            intArray[i] = intArray[index];
            intArray[index] = temp;
        }
        System.out.println("排序后的数组:");
        for(int i=0;i<intArray.length;i++)
            System.out.print(intArray[i]+" ");
    }

}

执行结果:

排序前的数组:
23 14 67 3 42 16 67 88 37 56 
排序后的数组:
3 14 16 23 37 42 56 67 67 88 

For-Each循环

public class ForEach {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum = 0;
        int[] nums = {1,2,3,4,5,6,7,8,9,0};
        for(int i:nums)
        {
            System.out.println("数组元素:"+i);
            sum+=i;
        }
        System.out.println("数组元素和:"+sum);
    }

}

执行结果:

数组元素:1
数组元素:2
数组元素:3
数组元素:4
数组元素:5
数组元素:6
数组元素:7
数组元素:8
数组元素:9
数组元素:0
数组元素和:45

For-Each访问多维数组

public class ForEach2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum =0;
        int nums[][] = new int[4][4];
        int k=0;
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                nums[i][j]=k++;
        for(int x[]:nums)
            for(int y:x)
            {
                System.out.println("数组元素:"+y);
                sum+=y;
            }
        System.out.println("数组元素:"+sum);
    }

}

执行结果:

数组元素:0
数组元素:1
数组元素:2
数组元素:3
数组元素:4
数组元素:5
数组元素:6
数组元素:7
数组元素:8
数组元素:9
数组元素:10
数组元素:11
数组元素:12
数组元素:13
数组元素:14
数组元素:15
数组元素:120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值