java定时关机(Copy Code)
import java.util.Scanner;class Timeing implements Runnable{ int i = 0; public Timeing (int i) { this.i = i; } @Override public void run () { try { Thread.sleep(i * 60 * 1000); System.out.print("将要关机!"); Program.exec("S"); } catch (Exception e) { e.printStackTrace(); } }}public class Program{ /** * @param args * @throws InterruptedException */ public static void main (String [] args) throws InterruptedException { int i = 0; while (true) { System.out.println("1.定时关机 2.立即关机 3.重启 4.退出"); Scanner scanner = new Scanner(System.in); try { i = scanner.nextInt(); switch (i) { case 1: timing(); break; case 2: close(); break; case 3: restart(); break; case 4: System.exit(0); break; default: System.out.println("输入有误,有效数字:1-2-3-4"); continue; } break; } catch (Exception e) { System.out.println("输入有误!请重新输入!"); continue; } } } private static void timing () throws InterruptedException { System.out.println("倒计时 几分钟?"); Scanner scanner= new Scanner(System.in); int i; while(true) { try { i = scanner.nextInt(); break; } catch (Exception e) { System.out.println("输入合法数字"); continue; } } Thread thread = new Thread(new Timeing(i)); thread.start(); time(i); } private static void time (int i) throws InterruptedException { System.out.println("开始计时:"); i = i * 60; while (true) { Thread.sleep(1000); i -= 1; if (i <= 0) break; System.out.println(i + ","); } } private static void close () { exec("S"); } private static void restart () { exec("R"); } public static void exec (String kind) { try { Runtime.getRuntime().exec("cmd /c start call shutdown -" + kind + " -f -t 0"); } catch (Exception e) { e.printStackTrace(); } }}