gpt4 book ai didi

java - 尝试在 Java 中获取日期格式 - 从单独的函数调用

转载 作者:行者123 更新时间:2023-12-02 01:32:05 25 4
gpt4 key购买 nike

我正在尝试根据用户输入的月、日和年检索日期格式。应该从一个函数和另一个函数中的逻辑调用它。当我运行代码时,问题会运行,但日期不会输出,即使它返回到 getArrivalDate 方法也是如此。有什么想法吗?

代码:

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.Month;
import java.text.NumberFormat;
import java.time.LocalDate;

public class Reserve {

public static void main(String[] args) {


System.out.println("Enter the requested input");
System.out.println();

//Call Arrival Date Method
getArrivalDate();
}

public static void getArrivalDate() {
// create a Scanner object
Scanner sc = new Scanner(System.in);
String choice = "y";

while (choice.equalsIgnoreCase("y")) {

System.out.print("month (1-12): ");
int month = sc.nextInt();
System.out.print("day (1-31): ");
int day = sc.nextInt();
System.out.print("year: ");
int year = sc.nextInt();

setArrivalDate(choice);
//Users choice
choice = sc.nextLine();
}
} //End getArrivalDate

public static String setArrivalDate(String arrivalDate1) {

String month = "no month";
int day = 0;
int year = 0;

switch(month) {
case "1":
month = "January";
break;

case "2":
month = "February";
break;

case "3":
month = "March";
break;

case "4":
month = "April";
break;

case "5":
month = "May";
break;

case "6":
month = "June";
break;

case "7":
month = "July";
break;

case "8":
month = "August";
break;

case "9":
month = "September";
break;

case "10":
month = "October";
break;

case "11":
month = "November";
break;

case "12":
month = "December";
break;
}

return arrivalDate1 = "Arrival Date: " + month + day + year;
return arrivalDate1;
}


}

最佳答案

你至少需要打印出返回值

并且您正在传递choice,它始终只是“y”...如果您想显示实际日期,请首先传递每个值

System.out.println(setArrivalDate(year, month, day));

这并不是真正正确的 returnarrivalDate1 = "Arrival Date: "...。您不必返回变量,只需返回字符串,例如 return "Arrival Date:"...

我建议学习 DateFormatter 类,而不是 switch case

关于java - 尝试在 Java 中获取日期格式 - 从单独的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com