JAVA基礎的output和Sort

來源:互聯網
上載者:User
今天上了JAVA的第2講課,上到一半感覺那些data type在C++都學過的,所以就先開溜了~出來居然看到了大雨和大太陽在同一時間進行^_^
回來把作業做完了,但第2題搞了半天老是越界問題,結果出在自己沒仔細.看來programming的時候千萬不要粗心啊^_^大家繼續努力吧~

/*
輸出前10個的自然數的平方
Shawn
CityPW@copyright
*/
class square
{
    static int i,v;
    square()
    {
        System.out.println("歡迎計算平方!");
        show();
    }

void show()
    {
        for(i=1;i<=10;i++)
        {
            System.out.println(i*i);        //System.out.println就好比C++裡的cout輸出資料流
        }
    }
    public static void main(String[] args)    //由於main只能有一個所以要聲明為public static
    {
        square s=new square();
    }
}

/*
排序的
Shawn
CityPW@copyright
*/
class test
{
    static int i,j,temp;
    test(){System.out.println("開始排序");}
void bubblesort(int[] a)
{
    //用一個冒泡泡
        for(i=0;i<a.length/3;i++)
        {
            for(j=0;j<a.length-1;j++)
            {
                if(a[j]>a[j+1])
                {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                }
            }
        }
}
   
public static void main(String[] args)
{
        int[] arrayOfInts={32,87,3,589,12,1076,2000,8,622,127};
        test t=new test();
        t.bubblesort(arrayOfInts);
        for(int i=0;i<arrayOfInts.length;i++)        //列印出結果
            System.out.print(" "+arrayOfInts[i]);
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.