时间输入和显示

时间:2017-01-14 18:36:50

标签: java

我正在尝试让程序接受输入并以我在displayTime方法中指定的格式显示它。它不是要求用户输入值,而是直接跳到displayTime方法并将值赋给00; 00; 00。有人可以帮助我,让我知道需要改变什么,所以首先需要输入然后显示?

import java.util.Scanner;
public class Time {
 Scanner input = new Scanner(System.in);
 private int hour = 0;
 private int minute = 0;
 private int second = 0;

 public void getHour(int h) {
 h = hour;
 System.out.println("Enter Hour:");
 h = input.nextInt();

 }
 public void getMinute(int m) {
     m = minute;
     System.out.println("Enter Minute:");
     m = input.nextInt();
 }
 public void getSecond(int s) {
     s = second;
     System.out.println("Enter Second:");
     s = input.nextInt();
 }

 public String displayTime() {
 return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
public class Test {

public static void main (String args[]) {
Time TimeObject = new Time();
System.out.println(TimeObject.displayTime());
}   
}

3 个答案:

答案 0 :(得分:1)

你的例子中有很多问题。首先,变量名称按照约定在Java中以小写字母开头,其次,您的方法getHour(int h)预计会传递一个小时,但也会从键盘请求它。对我没有意义。

说,我宁愿做类似的事情:

public class Time {

Scanner input = new Scanner(System.in);
private int hours = 0;
private int minutes = 0;
private int seconds = 0;

private int getHours() {
    System.out.println("Hours:");
    int hour = input.nextInt();
    if (hour < 0 || hour >= 24) {
        System.out.println("Wrong input. Hours must be between 0 and 23.Try again...");
        return getHours();
    }
    return hour;
}

private int getMinutes() {
    System.out.println("Minutes:");
    int minute = input.nextInt();
    if (minute < 0 || minute >= 60) {
        System.out.println("Wrong input. Minutes must be between 0 and 59. Try again...");
        return getMinutes();
    }
    return minute;
}

private int getSeconds() {
    System.out.println("Seconds:");
    int second = input.nextInt();
    if (second < 0 || second >= 60) {
        System.out.println("Wrong input. Seconds must be between 0 and 59. Try again...");
        return getSeconds();
    }
    return second;
}

public void getTime() {
    hours = getHours();
    minutes = getMinutes();
    seconds = getSeconds();
}

public String displayTime() {
    return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}

你的主要人物:

public class Test {

public static void main(String args[]) {
    Time time = new Time();
    time.getTime();
    System.out.println(time.displayTime());

}

}

它会递归地询问值,直到它进入适当的范围。

答案 1 :(得分:0)

试试这个:

class Time {
private int hour = 0;
private int minute = 0;
private int second = 0;

public void getHour(int h) {
    hour = h;
}
public void getMinute(int m) {
    minute = m;
}

public void getSecond(int s) {
    second = s;
}
public String displayTime() {
    return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
public class Test{
public static void main(String[] args) {
    int h, m, s;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter Hour:");
    h = input.nextInt();
    System.out.println("Enter Minute:");
    m = input.nextInt();
    System.out.println("Enter Second:");
    s = input.nextInt();
    Time TimeObject = new Time();
    TimeObject.getHour(h);
    TimeObject.getMinute(m);
    TimeObject.getSecond(s);
    System.out.println(TimeObject.displayTime());
}
}

答案 2 :(得分:0)

对于您想要创建和格式化时间的实际处理,为什么不使用Java的Calendar API?

看看这个:

https://www.mkyong.com/java/java-date-and-calendar-examples/