如何使用java获得多个值的输入?

b0zn9rqh  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(266)

使用vincent robert的答案,我想用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
sqougxex

sqougxex1#

这是有效的,尽管你需要添加另一个 / 年后的性格。

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)。试着用java和用c完全一样的方式编写代码就像从左到右写阿拉伯语一样。java中的just use方法 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]);
a6b3iqyw

a6b3iqyw2#

使用现代日期时间api和 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

在trail:date-time了解有关现代日期时间api的更多信息。

tvz2xvvm

tvz2xvvm3#

一种可能的方法是:

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

相关问题