可以从java代码关闭计算机吗?
在一个初级编程课程中,我们被分配存储类分数、平均分、最低分、最高分等 我们还被要求关闭电脑 我输出了一个菜单,其中一个例子是关闭电脑 但是它不起作用。方法4和方法8是唯一与问题相关的方法可以从java代码关闭计算机吗?,java,Java,在一个初级编程课程中,我们被分配存储类分数、平均分、最低分、最高分等 我们还被要求关闭电脑 我输出了一个菜单,其中一个例子是关闭电脑 但是它不起作用。方法4和方法8是唯一与问题相关的方法 import javax.swing.*; import java.io.*; class ClassMarks { String names[]; int marks[]; int counter; String marksString; final String
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;
}
}
import javax.swing.*;
导入java.io.*;
班级成绩{
字符串名[];
整数标记[];
整数计数器;
字符串标记字符串;
最终字符串PASSWORD=“绝密”;
字符串名称、姓氏;
整数标记;
整数合计;
双倍平均;
//检查密码的方法
公共无效检查密码(){
int计数器=0;
字符串密码\用户;
做{
password\u user=JOptionPane.showInputDialog(“输入密码:”);
if(密码_user.equals(密码)){
showMessageDialog(null,“授予访问权”);
outputMenu();
}否则{
showMessageDialog(null,“拒绝访问”);
}
计数器++;
}而((计数器<4)和&!(密码用户等于(密码));
showMessageDialog(null,“不再尝试可用”);
}
public void强制方法(){
String namesString=JOptionPane.showInputDialog(“输入学生人数”);
int noOfNames=Integer.parseInt(名称字符串);
//打印所有数组元素
用于(计数器=0;计数器大){
大=标记[计数器];
}
}
showMessageDialog(空,大);
}
//方法8
公共无效下限标记(){
小整数=100;
int-num;
用于(int计数器=0;计数器平均值){
showMessageDialog(空,标记[计数器]+“:此标记高于平均值”);
}否则{
showMessageDialog(空,标记[计数器]+“此标记低于平均值”);
}
}
}
//方法10
公共void(主字符串[])引发IOException{
Runtime=Runtime.getRuntime();
processproc=runtime.exec(“shutdown-s-t0”);
系统出口(0);
}
//方法11
公共字符显示等级(整数标记){
煤焦等级=“”;
如果((marks>=0)&&(marks=21)&&(marks=41)&&(marks=61)&&(marks=81)&&(marks=91)&(marks您可以使用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.");
}
}
sudo poweroff
sudo reboot
public static void main(String[] args) throws IOException {
//your code here
}
public static void shutdownPC(int time) {}
public static void restartPC(int time) {}
case 7:
shutdownPC(5);//5 mean 5 second if you want to shut the pc imidattilly just set it 0
break;
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.");
}
}