如何使用 Java 获取多个值的输入?
How do I get input of multiple values using Java?
使用 Vincent Robert's answer,我想在 Java 中重建它。
我是这样开始的:
// This is a draft of the program.
import java.time.LocalDateTime;
import java.util.Scanner;
import java.lang.*;
public class Test
{
public String calcRelTime(int past_month, int past_day, int past_year)
{
// initialization of time values
int minute = 60;
int hour = 60 * minute;
int day = 24 * hour;
int week = 7 * day;
int month = (365.25 * day) / 12;
int year = 12 * month;
// main part of function
LocalDateTime present = LocalDateTime.now();
/*
Something similar to:
var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
*/
// the if-condition statement similar to the answer, but I won't write it down here as it is too much code
}
public static void main(String[] args)
{
Scanner input;
int mon, day, year;
System.out.println("Enter date: ");
// the line where I take input of the date from the user
System.out.println("That was " + calcRelTime(mon, day, year) + ", fellow user.");
}
}
现在在 main()
函数中,我想做一些类似于 C 的 scanf("%d/%d/%d", &mon, &day, &year);
的事情。如何使用单行在 Java 中实现类似的东西?
我期待这样的输入:
8/24/1995 12:30PM
%d/%d/%d %d/%d%cM
一种可能的方法是:
public static void main(String[] args)
{
Scanner input;
int mon=Integer.parseInt(args[0]), day=Integer.parseInt(args[1]),
year=Integer.parseInt(args[2]);
System.out.println("Enter date: ");
// the line where I take input of the date from the user
System.out.println("That was " + calcRelTime(mon, day, year) + ", fellow user.");
}
Making use of String []args
while running from CMD : use command
java Test 1 28 2020
Example :
mon=1
day=28
year=2020
虽然您需要在年份之后添加另一个 /
字符,但此方法有效。
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("/");
System.out.print("Enter date [dd/mm/yyyy/]: ");
int day = scanner.nextInt();
int month = scanner.nextInt();
int year = scanner.nextInt();
scanner.nextLine();
System.out.println("day = " + day);
System.out.println("month = " + month);
System.out.println("year = " + year);
scanner.useDelimiter(":|\s");
System.out.println("Enter time [HH:mm am]: ");
int hour = scanner.nextInt();
int min = scanner.nextInt();
String apm = scanner.next();
System.out.println("hour = " + hour);
System.out.println("minute = " + min);
System.out.println(apm);
例如:
Enter date [dd/mm/yyyy/]: 12/11/1997/
day = 12
month = 11
year = 1997
Enter time [HH:mm am]:
04:50 pm
hour = 4
minute = 50
pm
尽管如此,Java 不是 C(C 也不是 Java)。尝试以与在 C 中完全相同的方式在 Java 中编写代码就像从左到右书写阿拉伯语一样。在 Java 中,只需使用方法 nextLine()
读取整行输入,然后解析该输入。
System.out.print("Enter date [dd/mm/yyyy]: ");
String input = scanner.nextLine();
String[] parts = line.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
使用modern date-time API and String#format
,您可以按如下方式进行:
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter date-time in the format MM/dd/yyyy hh:mm:ssa e.g. 8/24/1995 12:30PM: ");
String dateTime = input.nextLine();
System.out.println(calcRelTime(dateTime));
}
static String calcRelTime(String dateTimeString) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive() // Case insensitive e.g. am,
// AM, Am etc.
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) // Default second to 0 if absent
.appendPattern("M/d/u h:m[ ]a") // Optional space before AM/PM
.toFormatter(Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateTimeString, formatter);
Duration duration = Duration.between(ldt, now);
Period period = Period.between(ldt.toLocalDate(), now.toLocalDate());
return String.format("%d years %d months %d days %d hours %d minutes %d seconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart());
}
}
样本运行:
Enter date-time in the format MM/dd/yyyy hh:mm:ssa e.g. 8/24/1995 12:30PM: 8/24/1995 12:30PM
25 years 3 months 14 days 7 hours 37 minutes 39 seconds
了解有关现代日期时间 API 的更多信息
使用 Vincent Robert's answer,我想在 Java 中重建它。
我是这样开始的:
// This is a draft of the program.
import java.time.LocalDateTime;
import java.util.Scanner;
import java.lang.*;
public class Test
{
public String calcRelTime(int past_month, int past_day, int past_year)
{
// initialization of time values
int minute = 60;
int hour = 60 * minute;
int day = 24 * hour;
int week = 7 * day;
int month = (365.25 * day) / 12;
int year = 12 * month;
// main part of function
LocalDateTime present = LocalDateTime.now();
/*
Something similar to:
var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
*/
// the if-condition statement similar to the answer, but I won't write it down here as it is too much code
}
public static void main(String[] args)
{
Scanner input;
int mon, day, year;
System.out.println("Enter date: ");
// the line where I take input of the date from the user
System.out.println("That was " + calcRelTime(mon, day, year) + ", fellow user.");
}
}
现在在 main()
函数中,我想做一些类似于 C 的 scanf("%d/%d/%d", &mon, &day, &year);
的事情。如何使用单行在 Java 中实现类似的东西?
我期待这样的输入:
8/24/1995 12:30PM
%d/%d/%d %d/%d%cM
一种可能的方法是:
public static void main(String[] args)
{
Scanner input;
int mon=Integer.parseInt(args[0]), day=Integer.parseInt(args[1]),
year=Integer.parseInt(args[2]);
System.out.println("Enter date: ");
// the line where I take input of the date from the user
System.out.println("That was " + calcRelTime(mon, day, year) + ", fellow user.");
}
Making use of String []args
while running from CMD : use command
java Test 1 28 2020
Example :
mon=1
day=28
year=2020
虽然您需要在年份之后添加另一个 /
字符,但此方法有效。
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("/");
System.out.print("Enter date [dd/mm/yyyy/]: ");
int day = scanner.nextInt();
int month = scanner.nextInt();
int year = scanner.nextInt();
scanner.nextLine();
System.out.println("day = " + day);
System.out.println("month = " + month);
System.out.println("year = " + year);
scanner.useDelimiter(":|\s");
System.out.println("Enter time [HH:mm am]: ");
int hour = scanner.nextInt();
int min = scanner.nextInt();
String apm = scanner.next();
System.out.println("hour = " + hour);
System.out.println("minute = " + min);
System.out.println(apm);
例如:
Enter date [dd/mm/yyyy/]: 12/11/1997/
day = 12
month = 11
year = 1997
Enter time [HH:mm am]:
04:50 pm
hour = 4
minute = 50
pm
尽管如此,Java 不是 C(C 也不是 Java)。尝试以与在 C 中完全相同的方式在 Java 中编写代码就像从左到右书写阿拉伯语一样。在 Java 中,只需使用方法 nextLine()
读取整行输入,然后解析该输入。
System.out.print("Enter date [dd/mm/yyyy]: ");
String input = scanner.nextLine();
String[] parts = line.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
使用modern date-time API and String#format
,您可以按如下方式进行:
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter date-time in the format MM/dd/yyyy hh:mm:ssa e.g. 8/24/1995 12:30PM: ");
String dateTime = input.nextLine();
System.out.println(calcRelTime(dateTime));
}
static String calcRelTime(String dateTimeString) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive() // Case insensitive e.g. am,
// AM, Am etc.
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) // Default second to 0 if absent
.appendPattern("M/d/u h:m[ ]a") // Optional space before AM/PM
.toFormatter(Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateTimeString, formatter);
Duration duration = Duration.between(ldt, now);
Period period = Period.between(ldt.toLocalDate(), now.toLocalDate());
return String.format("%d years %d months %d days %d hours %d minutes %d seconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart());
}
}
样本运行:
Enter date-time in the format MM/dd/yyyy hh:mm:ssa e.g. 8/24/1995 12:30PM: 8/24/1995 12:30PM
25 years 3 months 14 days 7 hours 37 minutes 39 seconds
了解有关现代日期时间 API 的更多信息