日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习、简单代码编译

發布時間:2023/12/2 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习、简单代码编译 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【2.1】求明天是星期幾

public class study {
public static void main(String args[])
{
short i=7;
short tomorrow,yesterday;
tomorrow = (short) ((i+1) % 7);
yesterday = (short)((i-1+7) % 7);
System.out.print(“tomorrow:”+tomorrow+" “+”\n");
System.out.print(“yesterday:”+yesterday);
}
}
【2.2】判斷一個年份是否為閏年
public class study {
public static void main(String args[])
{
int year = 2020;
boolean leap = year%4000||(year%100!=0&&year%40); //先計算表達式,再賦值
System.out.println(year+" is a leap year? "+leap);
}
}

【2.3】Fibonacci數列

public class Fibonacci {
public static void main(String args[])
{
short i=0,j=1;
do
{
System.out.print(" “+i+” "+j);
i = (short)(i+j); //兩個short整數運算后結果使int類型,類型強制轉換后才能賦值
j = (short)(i+j);
}while(i>0); //short整數溢出時循環停止,不是死循環
System.out.println();
}
}
【2.4】求一個日期是星期幾

public class study {
public static void main(String args[])
{
int year = 2020, month = 9, day = 1;
boolean leap = year%4000||year%40&&year%100!=0; //判斷閏年
int total = year-1980+(year-1980+3)/4; //求平(閏)年累計的總天數
for (int i = month-1;i>0;i–) //計算當年前n-1個月累計的天數
switch(i)
{
case 1:case 3:case 5:case 7:case 8:case 10:total+=31;break;
case 4:case 6:case 9:case 11:total+=30;break;
case 2:total+=leap?29:28;
}
total+=day; //當月的天數
int week = 1; //起始日1979-12-31是星期一
week = (week+total)%7; //求得星期幾
System.out.print(year+“年”+month+“月”+day+“日 星期”);
switch(week)
{
case 0:System.out.print(“日”);break;
case 1:System.out.print(“一”);break;
case 2:System.out.print(“二”);break;
case 3:System.out.print(“三”);break;
case 4:System.out.print(“四”);break;
case 5:System.out.print(“五”);break;
case 6:System.out.print(“六”);break;
}
}
}

【2.5】用一維數組表示Fibonacci數列

public class study {
public static void main(String args[])
{
int n = 25,i, fib[]= new int[n];
fib[0]=0;
fib[1]=1;
for(i=2;i<n;i++)
fib[i]=fib[i-1]+fib[i-2];
for(i=0;i<fib.length;i++)
System.out.print(" "+fib[i]);
System.out.println();
}
}

【2.6】幻方

public class study {
public static void main(String args[])
{
int n = 3, mat[][]=new int[n][n]; // n是階數
int i=0,j=n/2; //i和j作為下標,首個數放在第一行中間位置
for(int k=1;k<=n*n;k++) //k是自然數
{
mat[i][j]=k; //當前位置取值
if(k%n==0) //對角線已滿
i=(i+1)%n; //下一位置像下一行
else
{
i=(i-1+n)%n; //下一位置向右上方
j=(j+1)%n;
}
}
for(i=0;i<mat.length;i++) //輸出二維數組
{
for(j=0;j<mat[i].length;j++)
System.out.print(mat[i][j]+"\t");
System.out.println();
}
}
}

【2.7】一維整數數組排序

public class study {
public static int[] random(int n,int max) //產生n個0~max之間的隨機數,返回一維數組
{
int value[] =new int[n];
for(int i=0;i<value.length;i++)
value[i] = (int)(Math.random()*max); //random()返回一個0~1的double隨機數
return value;
}

public static void print(int value[]) //輸出一維數組元素,數組作為參數 {for(int i=0;i<value.length;i++)System.out.print(" "+value[i]);System.out.println(); }//選擇直接排序。數組作為引用類型參數,實際參數的元素值將被改變 public static void selectsort(int value[]) {for(int i=0;i<value.length-1;i++) //n-1趟排序{int min=i;for(int j=i;j<value.length;j++) //在從value[i]開始的部分數組元素中if(value[j]<value[min]) //尋找最小值min = j; //min記錄下本趟最小值的下標if(i!=min){int temp=value[i]; //本趟最小值交換到左邊value[i]=value[min];value[min]= temp;}} } //設X.Y數組已按升序排序,merge()方法將兩者合并成一個排序數組返回,一次歸序算法 public static int[] merge(int X[],int Y[]) {int Z[]=new int[X.length+Y.length], i=0,j=0,k=0;while (i<X.length&&j<Y.length) //將X中兩個相鄰子序列歸并到Y中{if(X[i]<Y[i]) //較小值復制到Y中Z[k++]=X[i++];else Z[k++]=Y[j++];}while(i<X.length) //將前一個子序列剩余元素復制到X中Z[k++]=X[i++]; while(j<Y.length) //將后一個子序列剩余元素復制到Y中Z[k++]=Y[j++];return Z; } public static void main(String args[]) {int n1=7,max1=100;int table1[] = random(n1,max1);System.out.print("table1:");print(table1);selectsort(table1);System.out.print("sorted table1:");print(table1);int table2[] = random(n1,max1);System.out.print("table2:");print(table2);selectsort(table2);System.out.print("sorted table2:");print(table2);System.out.print("merge:");print(merge(table1,table2)); }

}

【2.8】楊輝三角

public class study {
public static int[][] yanghui(int n) //求n行楊輝三角,返回二維數組
{
int mat[][] = new int[n][]; //申請第一維的存儲空間
for (int i=0;i<n;i++)
{
mat[i] = new int[i+1]; //申請第二維的存儲空間
mat[i][0]=mat[i][i]=1;
for(int j=1;j<i;j++)
mat[i][j]=mat[i-1][j-1]+mat[i-1][j];
}
return mat; //返回二維數組引用
}
public static void print(int mat[][]) //輸出二維數組
{
for(int i=0;i<mat.length;i++)
{
for(int j=0;j<mat[i].length;j++)
System.out.print(" "+mat[i][j]);
System.out.println();
}
}
public static void main(String args[])
{
print(yanghui(10));
}
}

【2.9】求Fibonacci數列第n項的遞歸方法。

public class study {
public static int fibonacci(int n) //求Fibonacci數列的第n項,遞歸方法
{
if(n<0)
return -1;
if(n0||n1)
return n;
return fibonacci(n-2)+fibonacci(n-1);
}
public static void main(String args[])
{
System.out.print(fibonacci(10));
}
}

【2.10】從標準輸入流中讀取一行字符串,再轉換成整數。(兩個類)

public class Input{
//從標準輸入流中讀取一行字符串返回,以回車換行符結束
public static String readLine() throws java.io.IOException //拋出IO異常
{
System.out.print(“輸入一行字符串,以回車換行符結束:”);
byte buffer[] = new byte[512]; //以字節數組作為緩沖區
int count = System.in.read(buffer); //從標準輸入流讀字節到緩沖區buffer,返回讀取字節數
System.in.close(); //關閉標準輸入流
return(count==-1)?null:new String(buffer,0,count-2); //若只輸入Ctrl+Z,
//關閉標準輸入流,則count==-1,返回null;若只輸入回車換行符,則count==2,返回空串“”;
//否則,返回由buffer數組種從0開始count-2字節構造的串,不計回車換行符
}
public static void main(String args[])throws java.io.IOException //拋出IO異常交java虛擬機處理
{
String s=readLine(); //從標準輸入流中讀取一行字符串
int value = MyInteger.parseInt(s); //將字符串s轉換成整數,自動識別進制
System.out.println(“MyInteger.toString(”+value+",2)="+MyInteger.toString(value,2));
System.out.println(“MyInteger.toString(”+value+",16)="+MyInteger.toString(value,16));
}
}

public class MyInteger {
//返回將字符串s轉換的整數,自動識別進制,十、八、十六進制分別以正負號及1~9/0、0x開頭。。若s不能轉換成整數,則拋出數值格式日常
public static int parseInt(String s)throws NumberFormatException
{
if(snull)
throw new NumberFormatException(“null”); //拋出數值格式異常
char ch=s.charAt(0); //獲得首字母,識別進制
int value=0,i=0,sign=1,radix;
if(ch>=‘1’&&ch<=‘9’||ch’+’||ch==’-’) //十進制以正負號及1~9開頭
{
radix=10;
if(ch==’+’||ch==’-’) //跳過正負號,只有十進制可輸入±
i++; //i記住當前字符序號
sign=ch==’-’?-1:1; //識別正負號,記住正負數標記
}
else if(ch==‘0’&&s.charAt(1)!=‘x’) //八進制以0開頭
{
radix=8;
i++;
}
else if(ch==‘0’&&s.charAt(1)‘x’) //十六進制以0x開頭
{
radix=16;
i+=2;
}
else throw new NumberFormatException(“整數不能識別’”+ch+"'字符");
while(i<s.length()) //獲得無符號整數絕對值
{
ch=s.charAt(i++);
if(ch>=‘0’&&ch-‘0’<radix) //當radix<=0時,radix只需識別數字0~radix-1
value=value*radix+ch-‘0’; //value記住當前獲得的整數值
else if(radix16&&ch>=‘a’&&ch<=‘f’)
value=valueradix+ch-‘a’+10; //十六進制還需轉換’a’到’f’表示的整數值
else if(radix==16&&ch>=‘A’&&ch<=‘F’)
value=valueradix+ch-‘A’+10;
else throw new NumberFormatException(radix+“進制整數不能識別’”+ch+"'字符");
}
return value*sign; //返回有符號整數值
}

//返回整數value的radix進制字符串,radix取值為2,4,8,16,采用位運算 public static String toString(int value,int radix) {if (radix==10)return value+""; //返回將value值轉換成十進制的字符串if(radix==2||radix==4||radix==8||radix==16){int mask,n=0; //mask獲得radix進制的最大數字for(mask=radix-1;mask>0;mask>>>=1)n++; //n獲得mask的二進制位數,即2"=radixmask=radix-1;char buffer[]=new char[(int)(32.0/n+0.5)]; //存儲一個int表示為radix的各位for(int i=buffer.length-1;i>=0;i--){ //除radix取余法,余數存入buffer字符數組(逆序),高位補0int bit=value&mask; //獲得radix進制的個位數字buffer[i]=(char)(bit<=9?bit+'0':bit+'a'-10); //將0~9、10~15轉換為‘0’~‘9’、‘a'~'f'value>>>=n; // 右移n位,高位補0,即value除以radix }return new String(buffer); //返回由字符數組構造的字符串}throw new IllegalArgumentException("radix參數值"+radix+"表示的進制無效。"); //無效參數異常 }

}

總結

以上是生活随笔為你收集整理的Java学习、简单代码编译的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。