求大神看一下,我这个JAVA程序哪错了?
中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。importjava.util.S...
中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
import java.util.Scanner;
public class kl {
private static final Scanner input = null;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入日期(年月日之间用空格隔开,如1987 10 13:");
scanner.nextInt();
final int BEFORE_YEAR = 1990;
final int BEFORE_MONTH = 1;
final int BEFORE_DAY = 1;
int year;
int month;
int day;
String whatToDo = null;
System.out.println("想知道某人何时打鱼何时晒网吗?");
while (true) {
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
if ((year >= 1990) && (month >= 1 && month <= 12) && (day >= 1 && day <= kl.maxDayOfYearAndMonth(year, month))) {
break; }
else {
System.err.println("输入的日期错误!请重新输入!"); } }
for (int i = BEFORE_YEAR, dayCal = 0; i <= year; i++) {
for (int j = BEFORE_MONTH; j <= month; j++) {
for (int k = BEFORE_DAY; k <= kl .maxDayOfYearAndMonth(i, j); k++) {
dayCal++; if (dayCal <= 3) {
whatToDo = "打鱼"; }
else if (dayCal <= 5) {
whatToDo = "晒网"; }
if (dayCal == 5) { dayCal = 0; }
if (i == year && j == month && k == day) {
break; } } } }
System.out.println(year + "年" + month + "月" + day + "日这天是在" + whatToDo + "!");
} @SuppressWarnings("unused")
private String next() {
return null;}
public static int maxDayOfYearAndMonth(int year, int month) {
int maxDay = 0; switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31;
case 4: case 6: case 9: case 11: maxDay = 30;
case 2: if (isLeapYear(year)) {
maxDay = 28; }
else {
maxDay = 29; } }
return maxDay;
} /** * 判断是否润年 * * @param year * 年 * @return 润年:true */
private static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true; }
else {
return false;
} } {}{}} 展开
import java.util.Scanner;
public class kl {
private static final Scanner input = null;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入日期(年月日之间用空格隔开,如1987 10 13:");
scanner.nextInt();
final int BEFORE_YEAR = 1990;
final int BEFORE_MONTH = 1;
final int BEFORE_DAY = 1;
int year;
int month;
int day;
String whatToDo = null;
System.out.println("想知道某人何时打鱼何时晒网吗?");
while (true) {
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
if ((year >= 1990) && (month >= 1 && month <= 12) && (day >= 1 && day <= kl.maxDayOfYearAndMonth(year, month))) {
break; }
else {
System.err.println("输入的日期错误!请重新输入!"); } }
for (int i = BEFORE_YEAR, dayCal = 0; i <= year; i++) {
for (int j = BEFORE_MONTH; j <= month; j++) {
for (int k = BEFORE_DAY; k <= kl .maxDayOfYearAndMonth(i, j); k++) {
dayCal++; if (dayCal <= 3) {
whatToDo = "打鱼"; }
else if (dayCal <= 5) {
whatToDo = "晒网"; }
if (dayCal == 5) { dayCal = 0; }
if (i == year && j == month && k == day) {
break; } } } }
System.out.println(year + "年" + month + "月" + day + "日这天是在" + whatToDo + "!");
} @SuppressWarnings("unused")
private String next() {
return null;}
public static int maxDayOfYearAndMonth(int year, int month) {
int maxDay = 0; switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31;
case 4: case 6: case 9: case 11: maxDay = 30;
case 2: if (isLeapYear(year)) {
maxDay = 28; }
else {
maxDay = 29; } }
return maxDay;
} /** * 判断是否润年 * * @param year * 年 * @return 润年:true */
private static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true; }
else {
return false;
} } {}{}} 展开
4个回答
展开全部
package com.founder.hdbeacon;
import java.util.Scanner;
public class kl {
private static final Scanner input = null;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入日期(年月日之间用空格隔开,如1987 10 13:");
int input = scanner.nextInt();
Scanner scanner1=new Scanner(System.in);
int input1 = scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
int input2 = scanner2.nextInt();
final int BEFORE_YEAR = 1990;
final int BEFORE_MONTH = 1;
final int BEFORE_DAY = 1;
int year;
int month;
int day;
String whatToDo = null;
System.out.println("想知道某人何时打鱼何时晒网吗?");
year = input;
month = input1;
day = input2;
while (true) {
if ((year >= 1990) && (month >= 1 && month <= 12) && (day >= 1 && day <= kl.maxDayOfYearAndMonth(year, month))) {
break; }
else {
System.err.println("输入的日期错误!请重新输入!");
System.out.println("请重新输入日期(年月日之间用空格隔开,如1987 10 13:");
Scanner scanner3=new Scanner(System.in);
year = scanner3.nextInt();
Scanner scanner4=new Scanner(System.in);
month = scanner4.nextInt();
Scanner scanner5=new Scanner(System.in);
day = scanner5.nextInt();
}
}
for (int i = BEFORE_YEAR, dayCal = 0; i <= year; i++) {
for (int j = BEFORE_MONTH; j <= month; j++) {
for (int k = BEFORE_DAY; k <= kl .maxDayOfYearAndMonth(i, j); k++) {
dayCal++; if (dayCal <= 3) {
whatToDo = "打鱼"; }
else if (dayCal <= 5) {
whatToDo = "晒网"; }
if (dayCal == 5) { dayCal = 0; }
if (i == year && j == month && k == day) {
break; } } } }
System.out.println(year + "年" + month + "月" + day + "日这天是在" + whatToDo + "!");
} @SuppressWarnings("unused")
private String next() {
return null;}
public static int maxDayOfYearAndMonth(int year, int month) {
int maxDay = 0; switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31;
case 4: case 6: case 9: case 11: maxDay = 30;
case 2: if (isLeapYear(year)) {
maxDay = 28; }
else {
maxDay = 29; } }
return maxDay;
} /** * 判断是否润年 * * @param year * 年 * @return 润年:true */
private static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true; }
else {
return false;
} } {}{}}
import java.util.Scanner;
public class kl {
private static final Scanner input = null;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入日期(年月日之间用空格隔开,如1987 10 13:");
int input = scanner.nextInt();
Scanner scanner1=new Scanner(System.in);
int input1 = scanner1.nextInt();
Scanner scanner2=new Scanner(System.in);
int input2 = scanner2.nextInt();
final int BEFORE_YEAR = 1990;
final int BEFORE_MONTH = 1;
final int BEFORE_DAY = 1;
int year;
int month;
int day;
String whatToDo = null;
System.out.println("想知道某人何时打鱼何时晒网吗?");
year = input;
month = input1;
day = input2;
while (true) {
if ((year >= 1990) && (month >= 1 && month <= 12) && (day >= 1 && day <= kl.maxDayOfYearAndMonth(year, month))) {
break; }
else {
System.err.println("输入的日期错误!请重新输入!");
System.out.println("请重新输入日期(年月日之间用空格隔开,如1987 10 13:");
Scanner scanner3=new Scanner(System.in);
year = scanner3.nextInt();
Scanner scanner4=new Scanner(System.in);
month = scanner4.nextInt();
Scanner scanner5=new Scanner(System.in);
day = scanner5.nextInt();
}
}
for (int i = BEFORE_YEAR, dayCal = 0; i <= year; i++) {
for (int j = BEFORE_MONTH; j <= month; j++) {
for (int k = BEFORE_DAY; k <= kl .maxDayOfYearAndMonth(i, j); k++) {
dayCal++; if (dayCal <= 3) {
whatToDo = "打鱼"; }
else if (dayCal <= 5) {
whatToDo = "晒网"; }
if (dayCal == 5) { dayCal = 0; }
if (i == year && j == month && k == day) {
break; } } } }
System.out.println(year + "年" + month + "月" + day + "日这天是在" + whatToDo + "!");
} @SuppressWarnings("unused")
private String next() {
return null;}
public static int maxDayOfYearAndMonth(int year, int month) {
int maxDay = 0; switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31;
case 4: case 6: case 9: case 11: maxDay = 30;
case 2: if (isLeapYear(year)) {
maxDay = 28; }
else {
maxDay = 29; } }
return maxDay;
} /** * 判断是否润年 * * @param year * 年 * @return 润年:true */
private static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true; }
else {
return false;
} } {}{}}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先呢,忍不住说一个话题外的问题,你的class类名首字母应该大写。好了,附上代码一份,本来想上传图片的,传不上
private static final Scanner input = null;//这个input声明了而没实例化
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入日期(年月日之间用空格隔开,如1987 10 13:");
// scanner.nextInt(); //这句不要,否则会取不到输入的年份值
final int BEFORE_YEAR = 1990;
final int BEFORE_MONTH = 1;
final int BEFORE_DAY = 1;
int year;
int month;
int day;
String whatToDo = null;
System.out.println("想知道某人何时打鱼何时晒网吗?");
while (true) {
// year = Integer.parseInt(input.next()); //上面说了input没实例
// month = Integer.parseInt(input.next());
// day = Integer.parseInt(input.next());
year = scanner.nextInt();//程序执行到这里会等待用户输入数据
month = scanner.nextInt();//所以前面那句scanner.nextInt();没必要
day = scanner.nextInt();
if ((year >= 1990) && (month >= 1 && month <= 12) && (day >= 1 && day <= kl.maxDayOfYearAndMonth(year, month))) {
break;
} else {
System.err.println("输入的日期错误!请重新输入!");
}
}
}
就到这里了,虽然后面还有个异常,没想影响程序执行完
追问
又死循环了
追答
建议你在程序旁边打上断点,debug as java application,然后按F6一步一步去调试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
switch 没有返回 导致不管是几月 都是30天
更多追问追答
追问
怎么修改呢?
追答
case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31;
break;
case 4: case 6: case 9: case 11: maxDay = 30;
break;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询