本文目錄一覽:
JAVA 練習題
public class JavaExos {
public static void charInt(String chaine){ //1044
String[] charInt = new String[2];
int count = -1;
char maxChar = ‘A’;
int[] letterCount = new int[26];
String word = chaine.toLowerCase();
for (int i=0;iword.length();i++) {
int indexOfChar = (byte)word.charAt(i)-97;
letterCount[indexOfChar]++;
if (letterCount[indexOfChar]count || (letterCount[indexOfChar]==count word.charAt(i)maxChar)){
count = letterCount[indexOfChar];
maxChar = word.charAt(i);
}
}
charInt[0] = String.valueOf(maxChar);
charInt[1] = “”+count;
System.out.println(charInt[0]+” “+charInt[1]);
}
public static void getDate(int n){ //1047 這題如果給1,其實是指2000年1月2號.
n++;
int[] getYear = getYear(n);
int year = getYear[0];
int[] getMonth = getMonth(year,getYear[1]);
int month = getMonth[0];
String monthString ;
if(month10) monthString = “0”+String.valueOf(month);
else monthString = String.valueOf(month);
int day = getMonth[1];
System.out.println(year+”-“+monthString+”-“+day+” “+getDayOfWeek(n));
}
private static boolean isBissextile(int n){
if (n%4==0 !(n%100==0n%400!=0))
return true;
else
return false;
}
private static int[] getYear(int n){
int[] getYear = new int[2];
int year = 2000;
while(n0){
if(isBissextile(year)) n -= 366;
else n -= 365;
if (n0) year++;
}
if(isBissextile(year)) n+=366;
else n += 365;
getYear[0] = year;
getYear[1] = n;
return getYear;
}
private static int[] getMonth(int year, int n){
int[] getMonth = new int[2];
int month = 1;
while(n0){
if(month=7 month%2 != 0) n -= 31;
else if (month==2 isBissextile(year) ) n -= 29;
else if (month==2 !isBissextile(year)) n -= 28;
else if(month=7 month%2==0) n -= 30;
else if(month%2==0) n-=31;
else n -= 30;
if (n0) month++;
}
if(month=7 month%2 != 0) n += 31;
else if (isBissextile(year) month==2) n += 29;
else if (!isBissextile(year) month==2) n += 28;
else if(month=7 month%2==0) n += 30;
else if(month%2==0) n+=31;
else n += 30;
getMonth[0] = month;
getMonth[1] = n;
return getMonth;
}
private static String getDayOfWeek(int n){
int quotient = n/7;
int remainder = n -= quotient*7;
switch(remainder){
case 0 : return “Sunday”;
case 1 : return “Monday”;
case 2 : return “Tuesday”;
case 3 : return “Wednesday”;
case 4 : return “Thursday”;
case 5 : return “Friday”;
case 6 : return “Saturday”;
default : return “Never arrive”;
}
}
public static void getCode(String chaine){ //1048
chaine = chaine.toUpperCase();
System.out.println(“START”);
System.out.println(chaine);
System.out.println(“END”);
System.out.println();
for(int i=0;ichaine.length();i++){
System.out.print((changChar(chaine.charAt(i))));
}
System.out.println();
}
private static char changChar(char c){
if(c=65 c=90 c-565) return (char)(c+26-5);
else if(c=65 c=90) return (char)(c-5);
else return c;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaExos.charInt(“adfadffasdfda”);
JavaExos.getDate(1751);
JavaExos.getCode(“NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX”);
java練習題
public class Vehicle {
private String name;
private String gender;
private int age;
private String driverLicense;
private String plateNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDriverLicense() {
return driverLicense;
}
public void setDriverLicense(String driverLicense) {
this.driverLicense = driverLicense;
}
public String getPlateNumber() {
return plateNumber;
}
public void setPlateNumber(String plateNumber) {
this.plateNumber = plateNumber;
}
@Override
public String toString() {
return “Vehicle [name=” + name + “, gender=” + gender + “, age=” + age + “, driverLicense=” + driverLicense
+ “, plateNumber=” + plateNumber + “]”;
}
public void drive() {
System.out.println(name + “司機正在開” + plateNumber + “牌號的車。”);
}
public void stop() {
System.out.println(name + “司機把” + plateNumber + “牌號的車停了下來。”);
}
public boolean checkName(String check) {
if (this.name != null this.name != “”) {
return this.name.equals(check);
}
return false;
}
public boolean checkPlateNumber(String check) {
if (this.plateNumber != null this.plateNumber != “”) {
return this.plateNumber.equals(check);
}
return false;
}
}
懶得寫注釋了
java的經典例題
【程序1】 題目:古典問題:有一對兔子,從出生後第2個月起每個月都生一對兔子,小兔子長到第2個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?
【程序2】 題目:判斷101-200之間有多少個素數,並輸出所有素數。
【程序3】 題目:列印出所有的 “水仙花數 “,所謂 “水仙花數 “是指一個三位數,其各位數字立方和等於該數本身。
【程序4】 題目:將一個正整數分解質因數。例如:輸入90,列印出90=2*3*3*5。
【程序5】 題目:利用條件運算符的嵌套來完成此題:學習成績 =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。
【程序6】 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。
【程序7】 題目:輸入一行字元,分別統計出其中英文字母、空格、數字和其它字元的個數。
【程序8】 題目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加由鍵盤控制。
【程序9】 題目:一個數如果恰好等於它的因子之和,這個數就稱為 “完數 “。例如6=1+2+3.編程 找出1000以內的所有完數。
【程序10】 題目:一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?
【程序11】 題目:有1、2、3、4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?
【程序12】 題目:企業發放的獎金根據利潤提成。
利潤(I)低於或等於10萬元時,獎金可提10%;
利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;
20萬到40萬之間時,高於20萬元的部分,可提成5%;
40萬到60萬之間時高於40萬元的部分,可提成3%;
60萬到100萬之間時,高於60萬元的部分,可提成1.5%,
高於100萬元時,超過100萬元的部分按1%提成,
從鍵盤輸入當月利潤I,求應發放獎金總數?
【程序13】 題目:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
【程序14】 題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
【程序15】 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。
【程序16】 題目:輸出9*9口訣。
【程序17】 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以後每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。
【程序18】 題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽籤決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。
【程序19】 題目:列印出如下圖案(菱形)
【程序20】 題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數列的前20項之和。 1.程序分析:請抓住分子與分母的變化規律。
【程序21】 題目:求1+2!+3!+…+20!的和
【程序22】 題目:利用遞歸方法求5!。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/240192.html