是否可以从java代码关闭计算机?

时间:2016-12-23 14:23:21

标签: java

在初学者编程课程中,我们被分配了存储分类,平均分,最低分,最高分等等。

我们还被要求关闭计算机。

我输出了一个菜单,其中一个案例是关闭计算机。

然而它不起作用。方法4和方法8是唯一与问题相关的方法。

import javax.swing.*;
import java.io.*;

class ClassMarks {

    String names[];
    int marks[];
    int counter;
    String marksString;

    final String PASSWORD = "Top Secret";
    String name, surname;
    int mark;

    int total;
    double average;

    //method to check password
    public void checkPassword() {
        int counter = 0;
        String password_user;
        do {
            password_user = JOptionPane.showInputDialog("Enter password: ");

            if (password_user.equals(PASSWORD)) {

                JOptionPane.showMessageDialog(null, "Access Granted");
                outputMenu();
            } else {
                JOptionPane.showMessageDialog(null, "Access Denied");
            }
            counter++;
        } while ((counter < 4) && !(password_user.equals(PASSWORD)));
        JOptionPane.showMessageDialog(null, "No more attempts available");

    }

    public void compulsoryMethod() {

        String namesString = JOptionPane.showInputDialog("Enter number of students");
        int noOfNames = Integer.parseInt(namesString);

        //print all the array elements
        for (counter = 0; counter < noOfNames; counter++) {
            names[counter] = JOptionPane.showInputDialog("Enter names");
            marksString = JOptionPane.showInputDialog("Enter Mark for" + names[counter] + " ");
            marks[counter] = Integer.parseInt(marksString);
        }
    }

    // method 4
    public void outputMenu() {
        int input;
        do {

            String stringInput = JOptionPane.showInputDialog("Choose the decision you want to make: \n\n 1.Enter marks \n 2. See marks \n 3.Find Average \n 4.See highest mark \n 5.See lowest mark \n 6.See any mark above average \n 7.Turn off this Pc/Laptop/any other device\n 8.See Grade");

            input = Integer.parseInt(stringInput); // to convert stringInput which is String to input which is int

            switch (input) {
                case 1:
                    enterMarks();
                    break;
                case 2:
                    viewMarks();
                    break;
                case 3:
                    averageMark();
                    break;
                case 4:
                    highestMark();
                    break;
                case 5:
                    lowestMark();
                    break;
                case 6:
                    markAboveAverage();
                    break;
                case 7:
                    shutDown();
                    break;
                case 8:
                    viewMarks();
                    break;

                default:
                    JOptionPane.showMessageDialog(null, "Invalid choice");
            }
        } while (input != 7);
    }

    //method 3
    public void enterMarks() {

        String namesString = JOptionPane.showInputDialog("Enter no of students:");
        int noOfNames = Integer.parseInt(namesString);

        names = new String[noOfNames];
        marks = new int[noOfNames];
        for (counter = 0; counter < marks.length; counter++) {
            names[counter] = JOptionPane.showInputDialog("Enter names:");

            marksString = JOptionPane.showInputDialog("Enter Mark for " + names[counter] + " ");
            marks[counter] = Integer.parseInt(marksString);

        }

    }

    // method 5
    public void viewMarks() {

        for (counter = 0; counter < marks.length; counter++) {
            JOptionPane.showMessageDialog(null, new JTextArea(names[counter] + "\t\t" + marks[counter] + "\t\t" + displayGrade(marks[counter])));
        }
    }

    //method 6
    public void averageMark() {
        int total = 0;
        for (counter = 0; counter < marks.length; counter++) {
            total = total + marks[counter];
        }

        average = total / 5;
        JOptionPane.showMessageDialog(null, "Average is:" + average);
    }

    //method 7
    public void highestMark() {
        int large = 0;
        int num;

        // i starts from 2 because we already took one num value
        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] > large) {
                large = marks[counter];
            }
        }
        JOptionPane.showMessageDialog(null, large);
    }

    //method 8
    public void lowestMark() {

        int small = 100;
        int num;

        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] < small) {
                small = marks[counter];
            }

        }
        JOptionPane.showMessageDialog(null, small);

    }

    //method 9
    public void markAboveAverage() {

        averageMark();

        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] > average) {

                JOptionPane.showMessageDialog(null, marks[counter] + ": This mark is above average");
            } else {
                JOptionPane.showMessageDialog(null, marks[counter] + "This mark is below average");
            }
        }
    }

    //method 10
    public void (main String[]) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("shutdown -s -t 0");
        System.exit(0);
    }

    //method11
    public char displayGrade(int marks) {

        char grade = ' ';
        if ((marks >= 0) && (marks <= 20)) {
            grade = 'U';

        }
        if ((marks >= 21) && (marks <= 40)) {
            grade = 'E';

        }

        if ((marks >= 41) && (marks <= 60)) {
            grade = 'D';

        }

        if ((marks >= 61) && (marks <= 80)) {
            grade = 'C';

        }

        if ((marks >= 81) && (marks <= 90)) {
            grade = 'B';

        }

        if ((marks >= 91) && (marks <= 100)) {
            grade = 'A';

        }

        return grade;
    }    
}

2 个答案:

答案 0 :(得分:3)

您可以使用CMD命令关闭或重启计算机,如

shutdown -s -t 10

以下是使用 windows

关闭并重新启动计算机的方法

关闭计算机:

public void shutdownPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Shutdown system time mean, time to wait before my system will shutdow or restart
        r.exec("shutdown -s -t " + time);
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}

重新启动计算机:

public void restartPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Restart system
        r.exec("shutdown -r -t " + time);
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}

如果您使用的是linux或mac,则可以使用以下两个命令:

sudo poweroff

sudo reboot

您可以了解命令行linuxmac

修改

<强>第一

案例7中的第4种方法中出现错误,因为您调用了shutDown();,而且代码中不存在这种情况,因此您应该调用正确的方法。

<强>第二

主方法public void (main String[]) throws IOException {中出现错误,因为它未正确创建,因此您需要像这样创建它:

public static void main(String[] args) throws IOException {
   //your code here
}

不要忘记导入此import java.io.IOException;

第三次

关于我在上一个答案中设置它们的两种方法,你需要将它们设为静态:

public static void shutdownPC(int time) {}

public static void restartPC(int time) {}

第四

要从方法4 中调用它们,您需要指定重新启动或关闭电脑的时间,如下所示:

case 7:
    shutdownPC(5);//5 mean 5 second if you want to shut the pc imidattilly just set it 0
    break;

希望这可以帮到你。

答案 1 :(得分:0)

我想添加一些MrLy代码以使程序非常智能

public static void shutdownPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Shutdown system time mean, time to wait before my system will shutdow or restart
        if(System.getProperty("sun.desktop").equalsIgnoreCase("windows"))
        {
            r.exec("shutdown -s -t " + time);
        }
        else
        {
            r.exec("sudo poweroff");
        }  
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}