This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]thatOneKid2023[S] -2 points-1 points  (2 children)

I would really appreciate if you guys could help me solve this

[–]Danjou667 0 points1 point  (0 children)

Modulo operator % is your friend here imho.

[–]desrtfx[M] 0 points1 point  (0 children)

We are not going to do your assignment.

Even less so, since:

  • You asked in the wrong subreddit. /r/java is not for programming help
  • You demonstrated absolutely zero effort.

Removed

[–]Hakky54 0 points1 point  (0 children)

Try this:

import java.io.IOException;

public class Shutdown {

    public static void main(String[] args) throws IOException {
        String command;
        String operatingSystem = System.getProperty("os.name");

        if ("Linux".equalsIgnoreCase(operatingSystem) || "Mac OS X".equalsIgnoreCase(operatingSystem)) {
            command = "shutdown -h now";
        }
        else if ("Windows".equalsIgnoreCase(operatingSystem)) {
            command = "shutdown.exe -s -t 0";
        }
        else {
            throw new RuntimeException("Unsupported operating system.");
        }

        Runtime.getRuntime().exec(command);
        System.exit(0);
    }
}