新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Calendar;
公司主营业务:成都网站建设、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出宜都免费做网站回馈大家。
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+"个月");
}
}
我写的这个更为精确,不用考虑每个月的平均多少天的问题
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实现按词头、词尾提取英文文档中的单词
* @param file 原文件
* @param isHead 按词头true 按词尾false
* @param fix 关键词
* @return
* @throws Exception
*/
public ListString getWords(File file , boolean isHead,String fix) throws Exception{
//读取文件中的内容到字符串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分割为单词数组
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码是按字母顺序连续的整数,所以可以采用起始字母A递增的方式进行打印
英文字母有大小写之分,大小写字母的ASCII码是不连续的,所以需要分别打印
示例代码
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);
}
注意事项
不建议将上述变量uc硬编码赋值为65。反之,则可以提高程序的可读性,省去了不必要的注释,也体现出了可移植性的思想。
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;
}
把星期翻译成英文就可以了!
源代码
输出结果
public class Demo1 {
public static void main(String[] args) {
int start= Character.valueOf('A'); //获取'A'对应的ASCII码编号
int j=0; //对每行打印的个数进行计数
for(int i=start;i=Character.valueOf('Z');i++){ //从'A'循环到'Z'
System.out.print((char)i); //输出当前ASCII码对应的字母
j++; //打印出一个字母后加1
if (j==10) { //如果打印了十个数了
j=0; //把j重新设置为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.
* 工作时间从一天的6:00到23:00开始,在21:00到4:00结束,最长是6:00到4:00或者23:00到21:00,
* 22小时(不考虑孩子个数、中场休息、最长天数累加:N天 * 24 + 22)
* 其实用Calendar对象可能还简单点
*/
public class Babysitter {
public static void main(String[] args) {
//这里设置开始时间和结束时间
Time time = new Time(6,15,3,45);
Calculate calculate = new Calculate(time);
System.out.println(calculate.toPay());
}
}
/**
* 时间类,记录开始时间和结束时间
*
*/
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;
}
}
/**
* 计算报酬
*
*/
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;
}
/**
* 实际计算报酬的方法
*/
public float toPay() {
float resultPay;
//分结束时间在午夜前还是有过午夜
if (interval.getStartHour() interval.getEndHour()) {
resultPay = payForOneDay(interval);//结束时间无过午夜
} else {
resultPay = payForTwoDay(interval);//结束时间过了午夜
}
return resultPay;
}
/**
* 结束时间在午夜前
* @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;
}
/**
* 结束时间在午夜后
* @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);
}
}
/**
* 计算规则
*
*/
class Rule {
public static float BEFORE_MIDNIGHT_PAY = 3.40f;
public static float AFTER_MIDNIGHT_PAY = 3.90f;
}