本文介绍了如何在java中用空格分隔的日期输入中打印日期名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你有一个约会。你的任务是找到那天的日期。 输入格式 一行输入,分别包含分别为月,日和年的空格,分别为MM DD YYYY格式。 输出格式 以大写字母输出正确的日期。 我的代码:You are given a date. Your task is to find what the day is on that date.Input FormatA single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.Output FormatOutput the correct day in capital letters.My code:import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); Calendar cal = Calendar.getInstance(); cal.set(month,day,year); switch(cal.get(cal.DAY_OF_WEEK)) { case 1:System.out.println("SUNDAY"); break; case 2:System.out.println("MONDAY"); break; case 3:System.out.println("TUESDAY"); break; case 4:System.out.println("WEDNESDAY"); break; case 5:System.out.println("THURSDAY"); break; case 6:System.out.println("FRIDAY"); break; case 7:System.out.println("SATURDAY"); break; } }} 我尝试了什么: 以上代码仅在少数测试案例中取得成功,在主要测试案例中失败。 有人可以帮我通知我在哪里制作一个错误?What I have tried:The above code yields success only for few test cases and fails for major test cases.Can someone help me notifying where am I making a mistake ?推荐答案cal.set(month,day,year); 是对的吗?我将查看文档日历(Java Platform SE 7) [ ^ ] - 我原本以为使用日历是有点矫枉过正 - 在我的日子里,我们使用Zeller的同意并喜欢它 Zeller的同意 - 维基百科,免费的百科全书 [ ^ ]is correct ? I'd have a look at the doc Calendar (Java Platform SE 7 )[^] - I would have thought using a calendar for this was overkill - in my day we used Zeller's congruence and liked it Zeller's congruence - Wikipedia, the free encyclopedia[^]import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in);int month = in.nextInt();int day = in.nextInt(); int year = in.nextInt(); Calendar cal = Calendar.getInstance(); /// cal.set(month,day,year); cal.set(cal.YEAR,year); cal.set(cal.MONTH,month-1); cal.set(cal.DATE,day);//System.out.println("Calendar's year:-- " + cal.get(Calendar.YEAR)); // System.out.println("Calendar's month:-- " + cal.get(Calendar.MONTH)); // System.out.println("Calendar's Date:-- " + cal.get(Calendar.DATE)); // System.out.println( + cal.get(Calendar.DAY_OF_MONTH)); // System.out.println("Calendar's Day:-- " + cal.get(Calendar.DAY_OF_WEEK)); // switch(cal.get(cal.DAY_OF_WEEK))switch(cal.get(Calendar.DAY_OF_WEEK)) { case 1:System.out.println("SUNDAY"); break; case 2:System.out.println("MONDAY"); break; case 3:System.out.println("TUESDAY"); break; case 4:System.out.println("WEDNESDAY"); break; case 5:System.out.println("THURSDAY"); break; case 6:System.out.println("FRIDAY"); break; case 7:System.out.println("SATURDAY"); break; } }} 这篇关于如何在java中用空格分隔的日期输入中打印日期名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-28 06:25