Java 課堂隨筆(2),java課堂隨筆

來源:互聯網
上載者:User

Java 課堂隨筆(2),java課堂隨筆

迴圈

           for( 聲明; 迴圈條件 ; 迴圈變數的變化 ) { }有時這三個都不必填寫

           while(1)--死迴圈 在迴圈裡

                          可用break 來跳出當前迴圈 ;continue 結束本次迴圈,繼續下一次; return 結束當前方法

           do..while()

九九乘法表

package Day2;public class hello_word {public static void main(String[] args) {for(int i=1;i<=9;i++){   for(int j=1;j<=i;j++)   {   /*   System.out.print(i);   System.out.print("*");   System.out.print(j);   System.out.print("=");   System.out.print(i*j);   System.out.print("\t");   */   System.out.print(j+"*"+i+"="+(i*j)+"\t");}   System.out.println();}}}
老師講了一下程式猿暖手寶,百度了一個代碼,測試了一下,確實cpu的使用率100%

#include <process.h>#include <windows.h>double pi = 3.14;void handwarmer(void *p){while (1){//非線程同步。pi *= pi;if (pi > 1024*1024*1024)pi = 3.14;}}int main(){SYSTEM_INFO siSysInfo;GetSystemInfo(&siSysInfo);//n核 2n線程一樣hold住for (int i=0; i< (int)siSysInfo.dwNumberOfProcessors*2; ++i)_beginthread(handwarmer, 0, 0);system("pause");return 0;}
程式 = 演算法 + 資料結構

下面講 數組

     數組,一組資料類型相同的資料集合

        int[ ] arr = new int [10] ; //數組的聲明

         數組的初始化方式

              在聲明的時候初始化     int [ ]  arr1 = new int [10];    用這種方式,自動初始化為0 //arr1[0,0,0,0,0,0,0,0,0,0];

                                                       int[ ] arr2 = {1,2,3,4,5,6}; 

                                                       int[ ] arr3;

                                                       arr3 = new int [ ]{1,2,3,4}; 先聲明 , 後賦值

              數組不可變長的

             數組在初始化後,數組可存數的資料

              個數就固定了,不再可變。     ①  數組中資料的查詢      通過數組名【下標】的形式查詢資料   註:下標從0開始

                                                                   ②  數組中資料的修改      直接給數組名【下標】賦值。

                                                                   ③  數組的長度的擷取       通過 數組名.lenth 擷取   System.out.println(arr.lenth);

                                                                   ④  數組的遍曆 for()迴圈       for(int  i=0;i<arr3;i++ )  System.out.println( arr3[i] );     

                                                                   ⑤  數組的賦值   api  application process interface

                                                                                               System.arracopy(a,b,c,d,e)

                                                                                               a : 源數組

                                                                                               b : 原數組的起始位置

                                                                                               c: 目標數組

                                                                                               d: 目標數組的起始位置

                                                                                               e: 複製長度

                                                                                               int[ ] src = {1,2,3,4,5,6,7,8};

                                                                                               int[ ] aim = new int[10];

                                                                     ⑥數組的輸出   Arrays.Tostring(arrayName);

                                                                      ⑦ 數組下標越界異常

                                                                          Array Index Out Of Buounds Exception 

                                                                          ansychronized

                                                                      ⑧ 數組的擴容  

                                                                          Arrays.copyOf( arrName , lenth );

                                                                      ⑨數組的排序

                                                                         int[ ] unsorteArr = { 5,3,1,2,4};

                                                                         System.out.print( )

Copyint[] src = {1,2,3,4,5,6,7,8};        int[] aim = new int[10];        System.out.println(Arrays.toString(src));        System.out.println(Arrays.toString(aim));System.arraycopy(src,3,aim,4,4);        System.out.println(Arrays.toString(src));        System.out.println(Arrays.toString(aim));
sortint[ ] a = { 5,3,1,2,4};        System.out.println(Arrays.toString(a));        Arrays.sort(a);        System.out.println(Arrays.toString(a));

java裡的“方法” 與C語言裡的被調函數 相似,但是在JAVA裡不能叫做函數

 方法是用來封裝一段有特殊功能的代碼塊,在JAVA裡方法是可以被反覆調用的

 提高了代碼的重用率,代碼效率大大提升,便於維護

    1 方法的修飾(辭)

    2 方法的返回值類型

    3方法名——需滿足駝峰命名法 方法名後寫一對小括弧(參數列表)   註:紅色是一個方法必須有的

       throws 異常列表

       {方法體}                                                                    

      修飾辭   publc static

      返回值類型   int  || char ||  void(僅僅是處理,沒有返回值)

      ㈠方法名  ①駝峰 ②見名知意

      ㈡參數列表: 例如C語言裡的sort(int a[ ] )    形式參數  方法聲明時參數列表中所聲明的參數,無具體值,只起佔位作用

                                                                                        實際參數  方法調用時,傳進方法的有具體值,具體意義的參數                     註: Java的方法中無法修改實參的值  亦可為空白

      ㈢ 異常列表 (略)

      ㈣ 方法體 花括弧引起來的東西  即C 裡的函數內容

package Day02;public class hello_word {public static void main(String[] args) {System.out.println(c());}public static int  c(){return (1+100)*50; //方法體}}
/* *猜字母遊戲package ddddd;import java.util.Arrays;import java.util.Random;import java.util.Scanner;public class asdfasdf {public static void main(String[] args) {System.out.println("您一共有10次機會來猜這五個都不相同的大寫英文字母");int[] ans= new int[5];int[] gauss= new int[5];int count = 10;Random rdn = new Random();int cnt = 0,j = 0;for(int i=0;;i++){   int x = (int)rdn.nextInt(26);   for(j = 0;j<cnt;j++)   {   if(x==ans[j]) break;   }   if(j!=cnt){i--;continue;}   else {ans[cnt++] = x;System.out.print((char)(x+65)+" ");}   if(cnt==5) break;}while(true){System.out.println("現在您還有"+(count--)+"次機會");Scanner cin = new Scanner(System.in);char[] str = new char[5];String sstr = cin.nextLine();    for(int i=0;i<5;i++)    {    gauss[i] = (int)(sstr.charAt(i)-65);         }    int ans1 = 0,ans2 = 0;    for(int i=0;i<5;i++)    {    if(ans[i]==gauss[i])    ans1++;    }    for(int i=0;i<5;i++)    {    for(j=0;j<5;j++)    {    if(ans[i]==gauss[j])    ans2++;    }    }    System.out.println("位置和字母都猜對了有"+ans1+"個數字");    System.out.println("位置不對,但字母對了有"+ans2+"個數字");    if(ans1==5) {System.out.println("恭喜您猜對了");break;}if(count==0) {System.out.println("很遺憾答案是");for(int i= 0 ;i<5;i++)                                    System.out.print((char)('A'+ ans[i]));break;}}}} 






 

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.