В классе программирования начинающих нам было назначено хранить метки классов, среднюю, самую низкую отметку, наивысшую отметку и т. Д.Можно ли выключить компьютер из 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;
}
}
Ваш код [не работает] (http://importblogkit.com/2015/07/does-not-work/)? –
Нам нужно намного меньше кода и намного больше фоновой информации. Что происходит, когда вы пытаетесь запустить код выключения? Означает ли это конкретное сообщение об ошибке? Он ничего не делает и уходит? Это вызывает замораживание? Разве это заставляет ваш компьютер играть Радостью в мир? В какой операционной системе вы работаете? Mac (версия?) Linux (какой тип, какая версия?) –
Robert Columbia - Вы всегда должны помнить, что я новичок, так что легко на меня :). Я запускаю эту программу на окнах, и эта ошибка вызывает ошибку: expected ---> public void (main String []) throws IOException { –