如何更改代码以获得正确的输出

时间:2019-05-03 20:02:05

标签: java

一个简单的程序,可以计算秒到小时,分钟和秒。 但是我正在努力与输出。我应该如何转换代码?

它正在工作,但我对输出不满意。

package whatever;
import java.util.Scanner;
public class whatever {
public static void main(String[] args){


int hours, minutes, seconds;

Scanner in = new Scanner(System.in);
System.out.printf("put seconds");
int secondz = in.nextInt();


int hour, minutes, seconds;

Scanner in = new Scanner(System.in);
System.out.printf("put seconds");
int secondz = in.nextInt();

hour = secondz / 60;
minutes = hour % 60;
seconds = minutes % 60;
hour = hour / 60;
  System.out.printf("it's  " +hours ,"hours" +minutes ,"minutes" +seconds ,"seconds");  // the output is "it's  36" only 


in.close();
}
}

2 个答案:

答案 0 :(得分:3)

您需要阅读printf的工作方式。第一个参数是唯一要打印的参数,第二个参数是变量

System.out.printf("it's %s hours, %s minutes, %s seconds", hours, minutes, seconds);

答案 1 :(得分:0)

问题出在

System.out.printf("it's  " +hours ,"hours" +minutes ,"minutes" +seconds ,"seconds");

它使用格式为 printf 的打印。否则,您必须使用 println 或提供正确的格式,例如:

System.out.printf("it's  %d hours, %d minutes, %d seconds\n", hours , minutes, seconds);