import java.util.Calendar;
公司主營(yíng)業(yè)務(wù):成都網(wǎng)站建設(shè)、做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出宜都免費(fèi)做網(wǎng)站回饋大家。
import java.util.GregorianCalendar;
public class birthday {
public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
}
else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while ( gc1.before(gc2) ) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}
public int getMonths(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
}
else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc1.clear(Calendar.DATE);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.DATE);
while ( gc1.before(gc2) ) {
gc1.add(Calendar.MONTH, 1);
elapsed++;
}
return elapsed;
}
public static void main(String[] args) {
GregorianCalendar gc1 = new GregorianCalendar(2007,3,4);
GregorianCalendar gc2 = new GregorianCalendar(2008,2,5);
birthday et = new birthday();
int days = et.getDays(gc1, gc2);
int months = et.getMonths(gc1, gc2);
System.out.println("年齡為"+months/12+"歲"+months%12+"個(gè)月");
}
}
我寫的這個(gè)更為精確,不用考慮每個(gè)月的平均多少天的問題
public?class?Test?{
public?static?void?main(String[]?args)?{
Test?t?=new?Test();
File?file?=?new?File("E:\\桌面\\words.txt");
try?{
ListString?list=?t.getWords(file,?true,"h");
for?(String?string?:?list)?{
System.out.print(string+"???");
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
/**
?*?java實(shí)現(xiàn)按詞頭、詞尾提取英文文檔中的單詞
?*?@param?file?原文件
?*?@param?isHead?按詞頭true?按詞尾false
?*?@param?fix?關(guān)鍵詞
?*?@return
?*?@throws?Exception?
?*/
public?ListString?getWords(File?file?,?boolean?isHead,String?fix)?throws?Exception{
//讀取文件中的內(nèi)容到字符串str
FileInputStream?fis?=?new?FileInputStream(file);
BufferedInputStream?bis?=?new?BufferedInputStream(fis);
int?i=0;
String?str?=?"";
while?((i=bis.read())!=-1)?{
str+=(char)i;
}
System.out.println(str);
bis.close();
fis.close();
//將str分割為單詞數(shù)組
String[]?words?=?str.split("?");
ListString?list?=?new?ArrayListString();
if?(isHead)?{
for?(String?word?:?words)?{
if?(word.startsWith(fix))?{
list.add(word);
}
}
}else?{
for?(String?word?:?words)?{
if?(word.endsWith(fix))?{
list.add(word);
}
}
}
return?list;
}
}
編碼思路
首先考慮到,英文字母的ASCII碼是按字母順序連續(xù)的整數(shù),所以可以采用起始字母A遞增的方式進(jìn)行打印
英文字母有大小寫之分,大小寫字母的ASCII碼是不連續(xù)的,所以需要分別打印
示例代碼
public?static?void?main(String[]?args)?{
char?uc?=?'A';
char?lc?=?'a';
//保存全部大寫字母
StringBuilder?upperCaseLetters?=?new?StringBuilder("UpperCaseLetters:");
//保存全部小寫字母
StringBuilder?lowerCaseLetters?=?new?StringBuilder("LowerCaseLetters:");
for?(int?i?=?0;?i??26;?i++)?{
upperCaseLetters.append((char)?(uc?+?i));
lowerCaseLetters.append((char)?(lc?+?i));
}
System.out.println(upperCaseLetters);
System.out.println(lowerCaseLetters);
}
注意事項(xiàng)
不建議將上述變量uc硬編碼賦值為65。反之,則可以提高程序的可讀性,省去了不必要的注釋,也體現(xiàn)出了可移植性的思想。
public?String?getXqEn(int?xqNum){
MapInteger,String?xqMap?=?new?HashMapInteger,String();
xqMap.put(1,"星期一");?
xqMap.put(2,"星期二");?
xqMap.put(3,"星期三");?
xqMap.put(4,"星期四");?
xqMap.put(5,"星期五");?
xqMap.put(6,"星期六");?
xqMap.put(7,"星期七");?
String?xqEn?=?xqMap.get(xqNum);
System.out.print(xqEn?);
return?xqEn;
}
把星期翻譯成英文就可以了!
源代碼
輸出結(jié)果
public class Demo1 {
public static void main(String[] args) {
int start= Character.valueOf('A');?? ?//獲取'A'對(duì)應(yīng)的ASCII碼編號(hào)
int j=0;?? ??? ??? ??? ??? ??? ??? ?//對(duì)每行打印的個(gè)數(shù)進(jìn)行計(jì)數(shù)
for(int i=start;i=Character.valueOf('Z');i++){ //從'A'循環(huán)到'Z'
System.out.print((char)i);? //輸出當(dāng)前ASCII碼對(duì)應(yīng)的字母
j++;?? ??? ??? ??? ??? ??? ?//打印出一個(gè)字母后加1
if (j==10) {?? ??? ??? ??? ?//如果打印了十個(gè)數(shù)了
j=0;?? ??? ??? ??? ??? ?//把j重新設(shè)置為0;
System.out.println();?? ?//換行
}
}
}
}
/**
* 程序入口類
* Assume that the starting time for the baby-sitter is somewhere between 6 AM and 11 PM;
* and that the ending time is somewhere between 9 PM and 4 AM.
* 工作時(shí)間從一天的6:00到23:00開始,在21:00到4:00結(jié)束,最長(zhǎng)是6:00到4:00或者23:00到21:00,
* 22小時(shí)(不考慮孩子個(gè)數(shù)、中場(chǎng)休息、最長(zhǎng)天數(shù)累加:N天 * 24 + 22)
* 其實(shí)用Calendar對(duì)象可能還簡(jiǎn)單點(diǎn)
*/
public class Babysitter {
public static void main(String[] args) {
//這里設(shè)置開始時(shí)間和結(jié)束時(shí)間
Time time = new Time(6,15,3,45);
Calculate calculate = new Calculate(time);
System.out.println(calculate.toPay());
}
}
/**
* 時(shí)間類,記錄開始時(shí)間和結(jié)束時(shí)間
*
*/
class Time {
int startHour;
int startMinute;
int endHour;
int endMinute;
public Time() {
super();
}
public Time(int startHour, int startMinute, int endHour, int endMinute) {
super();
this.startHour = startHour;
this.startMinute = startMinute;
this.endHour = endHour;
this.endMinute = endMinute;
}
public int getStartHour() {
return startHour;
}
public void setStartHour(int startHour) {
this.startHour = startHour;
}
public int getStartMinute() {
return startMinute;
}
public void setStartMinute(int startMinute) {
this.startMinute = startMinute;
}
public int getEndHour() {
return endHour;
}
public void setEndHour(int endHour) {
this.endHour = endHour;
}
public int getEndMinute() {
return endMinute;
}
public void setEndMinute(int endMinute) {
this.endMinute = endMinute;
}
}
/**
* 計(jì)算報(bào)酬
*
*/
class Calculate {
private Time interval;
public Calculate(Time interval) {
super();
this.interval = interval;
}
public Time getInterval() {
return interval;
}
public void setInterval(Time interval) {
this.interval = interval;
}
/**
* 實(shí)際計(jì)算報(bào)酬的方法
*/
public float toPay() {
float resultPay;
//分結(jié)束時(shí)間在午夜前還是有過午夜
if (interval.getStartHour() interval.getEndHour()) {
resultPay = payForOneDay(interval);//結(jié)束時(shí)間無過午夜
} else {
resultPay = payForTwoDay(interval);//結(jié)束時(shí)間過了午夜
}
return resultPay;
}
/**
* 結(jié)束時(shí)間在午夜前
* @param time
* @return
*/
private float payForOneDay(Time time) {
int startHour = time.getStartHour();
int endHour = time.getEndHour();
int startMinute = time.getStartMinute();
int endMinute = time.getEndMinute();
float payment = 0.00f;
if (startMinute endMinute) {
float ecllapsTime = (endHour - startHour - 1.00f)
+ (60.00f + endMinute - startMinute) / 60.00f;
payment = ecllapsTime * Rule.BEFORE_MIDNIGHT_PAY;
} else {
float ecllapsTime = (endHour - startHour)
+ (endMinute - startMinute) / 60.00f;
payment = ecllapsTime * Rule.BEFORE_MIDNIGHT_PAY;
}
return payment;
}
/**
* 結(jié)束時(shí)間在午夜后
* @param time
* @return
*/
private float payForTwoDay(Time time) {
int startHour = time.getStartHour();
int endHour = time.getEndHour();
int startMinute = time.getStartMinute();
int endMinute = time.getEndMinute();
float paymentBeforeMidnight = 0.00f;
float paymentAfterMidnight = 0.00f;
if (startMinute != 0.00) {
float ecllapsTime = (23.00f - startHour)
+ (60.00f - startMinute) / 60.00f;
paymentBeforeMidnight = ecllapsTime * Rule.BEFORE_MIDNIGHT_PAY;
} else {
float ecllapsTime = (24.00f - startHour);
paymentBeforeMidnight = ecllapsTime * Rule.BEFORE_MIDNIGHT_PAY;
}
paymentAfterMidnight = (endHour + (endMinute / 60.00f)) * Rule.AFTER_MIDNIGHT_PAY;
return (paymentBeforeMidnight + paymentAfterMidnight);
}
}
/**
* 計(jì)算規(guī)則
*
*/
class Rule {
public static float BEFORE_MIDNIGHT_PAY = 3.40f;
public static float AFTER_MIDNIGHT_PAY = 3.90f;
}