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;
}
}
【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; //返回有符號整數值
}
}
總結
以上是生活随笔為你收集整理的Java学习、简单代码编译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020-08-21 光纤通信第四章知识
- 下一篇: Java学习:类的封装、继承和多态