2015年11月01日

来源:互联网 发布:淘宝连帽 编辑:程序博客网 时间:2024/05/20 16:32
今天写了2005,首先输入格式2015/3/5这种。我是先把这个数组分开,用的split函数,然后把分开的字符串要变成整数类型的。
然后再用switch然选择。步骤感觉很麻烦。后来提交错误了,因为闰年的判断有错误,||写成&&了。
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year,month,day;
int sum=0;
boolean leap=false;
String ch = sc.nextLine();
String [] dat = ch.split( "/" );//分割字符串
year=Integer.parseInt(dat[0]);//字符串换成整数
month=Integer.parseInt(dat[1]);
day=Integer.parseInt(dat[2]);
if(year@0==0||year%4==0&&year0!=0){
leap = true;
}
while(month-->0){
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case12:
sum+=31;break;
case 4: case 6: case 9: case 11:
sum+=30;break;
case 2:sum+= leap ? 29:28;break;//case 2: leap ?(sum+=29):(sum+=28);break;
}
}
System.out.println(sum+day);
}
}
}看了其他人的c或者c++写的,就是scanf(%d/%d/%d,&a.&b,&c);很简单的哦,完全没有我的这么麻烦。之后再弄个月份的数组,求月份总数,是闰年再加个1就好了,代码超少的。我去找找看Java还有没有比较方便的方法。
对了,关于split中放\\\\这个的,百度是说可以分开\这个符号,但是我自己试,还是存在很多的问题,以后再看看。
我想换换用数组来写,没想到啊,数组没学好的啊。2015年11月01日
这样是可以编译的,但是这样
2015年11月01日
编译是错误的。
然后用了数组的方法 代码好像也没少多少
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year,month,day;
boolean leap;
   inta[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
String ch = sc.nextLine();
String [] dat = ch.split( "/" );//分割字符串
year=Integer.parseInt(dat[0]);//字符串换成整数
month=Integer.parseInt(dat[1]);
day=Integer.parseInt(dat[2]);
 leap =year@0==0||year%4==0&&year0!=0;
for(int i=1;i
day+=a[i];
if(month>2) 
day+= leap?1:0;
System.out.println(day);
}
}
}不过那个leap的可以把month>2一起弄个条件。
0 0