Java:从我的自定义方法获取所需的打印/输出
我在在线任务的帮助下学习Java,并在方法上乱搞。我们的目标是编写一个程序,它使用一个方法,该方法以年、月、日和一个输出表单作为参数。然后程序使用所选格式打印整个日期。对于格式,我的意思是, 正常:10。2016年7月/2016年7月10日/官方:10-07-16 我的问题是我不完全确定我将如何做到这一点,但到目前为止,我已经做到了这一点 我一直在尝试使用字符串数组作为标准格式,但似乎无法正常工作 编辑:我得到了它的预期效果,但它仍然存在一些小问题,例如最后一个用户输入在最后打印,我似乎没有得到第三个格式(Official)的预期效果,因为它没有在10以下的值之前的0中打印 对不起,如果不容易理解我想说的话,英语不是我的母语 多谢各位Java:从我的自定义方法获取所需的打印/输出,java,Java,我在在线任务的帮助下学习Java,并在方法上乱搞。我们的目标是编写一个程序,它使用一个方法,该方法以年、月、日和一个输出表单作为参数。然后程序使用所选格式打印整个日期。对于格式,我的意思是, 正常:10。2016年7月/2016年7月10日/官方:10-07-16 我的问题是我不完全确定我将如何做到这一点,但到目前为止,我已经做到了这一点 我一直在尝试使用字符串数组作为标准格式,但似乎无法正常工作 编辑:我得到了它的预期效果,但它仍然存在一些小问题,例如最后一个用户输入在最后打印,我似乎没有得到
public static int returnCorrectFormat(int year, int month, int day, int format) {
switch(format)
{
case 1:
switch (month)
{
case 1: System.out.println(day +"." + " January " + year);
break;
case 2: System.out.println(day +"." + " February " + year);
break;
case 3: System.out.println(day +"." + " March " + year);
break;
case 4: System.out.println(day +"." + " April " + year);
break;
case 5: System.out.println(day +"." + " May " + year);
break;
case 6: System.out.println(day +"." + " June " + year);
break;
case 7: System.out.println(day +"." + " July " + year);
break;
case 8: System.out.println(day +"." + " August " + year);
break;
case 9: System.out.println(day +"." + " September " + year);
break;
case 10: System.out.println(day +"." + " October " + year);
break;
case 11: System.out.println(day +"." + " November " + year);
break;
case 12: System.out.println(day +"." + " December " + year);
break;
default:
break;
}
break;
case 2:
System.out.println(day + "." + month + "." + year);
break;
case 3: if(( day < 10) && (month < 10))
{
System.out.println(year + "-" + "0" + month + "-" + "0" + day);
}
else{
System.out.println(year + "-" + month + "-" + day);
}
}
return format;
}
public static void main(String[] args) {
try{
int printableInfo;
int year;
int date;
int month;
int format;
System.out.println("The programm will ask you to input year,date, month and then choose a format for printing.");
System.out.println("Insert year: ");
year = input.nextInt();
System.out.println("Insert date: ");
date = input.nextInt();
System.out.println("Insert month: ");
month = input.nextInt();
System.out.println("Choose format: [1] normal [2] shorten [3] official ");
format = input.nextInt();
printableInfo = returnCorrectFormat(year,date,month,format);
System.out.println(printableInfo);
}
catch (Exception e)
{
System.out.println("ERROR: closing program..");
System.exit(1);
}
}
公共静态整数格式(整数年、整数月、整数日、整数格式){
开关(格式)
{
案例1:
开关(月)
{
案例1:System.out.println(日期+“+”一月+”年);
打破
案例2:System.out.println(日期+“+”二月+”年);
打破
案例3:System.out.println(日期+“+”三月+”年);
打破
案例4:System.out.println(日期+“+”四月+”年);
打破
案例5:System.out.println(日期+“+”五月+”年);
打破
案例6:System.out.println(日期+“+”六月+”年);
打破
案例7:System.out.println(日期+“+”七月+”年);
打破
案例8:System.out.println(日期+“+”八月+”年);
打破
案例9:System.out.println(日期+“+”九月+”年);
打破
案例10:System.out.println(年月日);
打破
案例11:System.out.println(日期+“+”十一月+”年);
打破
案例12:System.out.println(日期+“+”十二月+”年);
打破
违约:
打破
}
打破
案例2:
系统输出打印项次(天+月+年);
打破
案例3:如果((日<10)和&(月<10))
{
系统输出打印项次(年+“-”+“0”+月+“-”+“0”+日);
}
否则{
系统输出打印项次(年+“-”+月+“-”+日);
}
}
返回格式;
}
公共静态void main(字符串[]args){
试一试{
int可打印信息;
国际年;
国际日期;
整月;
int格式;
System.out.println(“程序将要求您输入年份、日期、月份,然后选择打印格式。”);
System.out.println(“插入年份:”);
年份=input.nextInt();
System.out.println(“插入日期:”);
date=input.nextInt();
System.out.println(“插入月份:”);
month=input.nextInt();
System.out.println(“选择格式:[1]正常[2]缩短[3]正式”);
format=input.nextInt();
printableInfo=returnCorrectFormat(年、日、月、格式);
System.out.println(可打印信息);
}
捕获(例外e)
{
System.out.println(“错误:关闭程序…”);
系统出口(1);
}
}
对于“正常”格式,您可以使用HashMap
,从1到12的每个数字都是一个键,值是相应的月份。
例如:
HashMap monthMap=newhashmap();
每月(5月5日);
好的,OP缺少一些关于你到底想做什么的信息
考虑以下几点:
1) returnCorrectFormat方法不应返回int。该方法应获取相应格式化日期所需的所有参数,然后继续返回带有格式化日期的字符串
2) 如上所述,对于您想要做的事情,使用HashMap将非常有效
更具体地说:
1) 您需要一个方法,该方法将返回一个带有键对象值的hashmap,如下所示:
1、“一月”等等
创建此hashmap所需的值将是一个带有月份名称的硬编码字符串数组,以及一个带有月份编号的硬编码int数组,该数组将作为maps键服务
String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"};
int[] numMothns = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
这两个数组都将放在main方法中。在此之后,您将需要创建实际HashMap的方法
public static HashMap<Integer, String> initMonthMap (int[] numM, String[] m) {
HashMap<Integer, String> monthsMap = new HashMap<>();
for (int i = 0; i <= numM.length i++) {
for (int j = 0; j <= m.length; j++){
monthsMap.put(numM[i], m[i]);
}
}
return monthsMap;
}
publicstatichashmap initMonthMap(int[]numM,String[]m){
HashMap monthsMap=新的HashMap();
for(int i=0;i java.time)
您可以使用Java 8和更高版本中内置的类来完成此操作。搜索堆栈溢出以查找数千个示例
如果这些格式被定义为特定地区的文化规范,那么就让java.time为您自动本地化
首先用特定的和定义一个
接下来,应用于日期值
LocalDate localDate = LocalDate.of( 2016 , Month.JULY , 10 );
String output = localDate.format( formatter );
再次搜索堆栈溢出,因为您的问题实际上与许多其他问题重复。如果这是一个家庭作业练习,因此您确实不想使用库来辅助,您应该在问题中这样说。基于输入输出的条件语句或开关语句?应该有来自end us选项的格式类型的开关格式呃,谢谢你,我知道我的英语不好。但是有一个小问题:如果我理解了任务
public static String formatDate(int format ,int year, int month, int day, HashMap<Integer, String> monthsMap) {
StringBuilder sb = new StringBuilder();
switch (format) {
case 1:
sb.append(day + " " + month + " " + year);
break;
case 2:
sb.append(day + "." + month + "." + year);
break;
case 3:
sb.append(day + " " + monthsMap.get(month) + " " + year);
break;
}
return sb.toString();
}
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM );
Locale locale = Locale.CANADA_FRENCH;
formatter = formatter.withLocale( locale );
LocalDate localDate = LocalDate.of( 2016 , Month.JULY , 10 );
String output = localDate.format( formatter );