一、获取当前时间
package exercise;
import javax.swing.JOptionPane;
public class ShowCurrentTime {
public static void main(String[] args) {
long totalMilliseconds=System.currentTimeMillis(); //获取当前时间的方法
long totalSeconds=totalMilliseconds/1000;
int currentSecond=(int)(totalSeconds%60); //将long整数型转换为int整数型
long totalMinutes=totalSeconds/60;
int currentMinutes=(int)(totalMinutes%60);
long totalHours=totalMinutes/60;
int currentHours=(int)(totalHours%24);
JOptionPane.showMessageDialog(null,"现在的时间是"+currentHours+"点"+currentMinutes+"分"+currentSecond+"秒");
}
}
二、从控制台获取输入
package exercise;
import java.util.Scanner;
public class ScannerInput {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in); //获取方法
System.out.println("请随便输入一个整数");
int i=scanner.nextInt();
System.out.println("请随便输入一个小数");
double d=scanner.nextDouble();
System.out.println("请随便输入字符串(中间不能有空格)");
String s=scanner.next(); //字符串不需要next
System.out.println("您输入的整数为 "+i+"\n"+"您输入的小数为 "+d+"\n"+"您输入的字符串为 "+s);
}
}
网友评论