如何将javax.swing.*
转换为正常的命令行提示符?我尝试在https://compilr.com/上测试代码,但由于它有javax.swing.*
,因此无法提示对话框。
import javax.swing.*;
public class Program
{
public static void main ( String[] args)
{
int numOfGrades = 0, gradeCount = 0, sum = 0;
char grade;
String openMsg, getNumOfGrades, getGrades, outMsg1, gradeOut;
openMsg = "Welcome.\n\n" +
"Grading Scale: A = 4, B = 3, C = 2, D = 1, F = 0\n\n" +
"Please tell me how many grades to enter today:";
getNumOfGrades = JOptionPane.showInputDialog ( openMsg );
numOfGrades = Integer.parseInt ( getNumOfGrades );
while ( gradeCount != numOfGrades )
{
getGrades = JOptionPane.showInputDialog("Please enter grade number: "+ (gradeCount+1) + "\n" +
"Grading Scale: A = 4, B = 3, C = 2, D = 1, F = 0\n\n");
grade = getGrades.charAt ( 0 );
JOptionPane.showMessageDialog ( null, grade );
//int sum = 0;
switch(grade)
{
case 'a': case 'A':
grade = 4;
break;
case 'b': case 'B':
grade = 3;
break;
case 'c': case 'C':
grade = 2;
break;
case 'd': case 'D':
grade = 1;
break;
case 'f': case 'F':
grade = 0;
break;
default:
String errOut = "Sorry, Invalid entry. Exiting Program!";
JOptionPane.showMessageDialog ( null, errOut );
break;
}
sum += grade;
gradeCount++;
}
double gpa = sum / gradeCount;
outMsg1 = "You entered " + numOfGrades + " grades\n" +
"Your GPA is: " + gpa + "\n" +
"These were the grades you entered: "; //+ grade;
JOptionPane.showMessageDialog ( null, outMsg1 );
System.exit ( 0 );
}
}
答案 0 :(得分:2)
这完全取决于你想要做什么。如果你只想要一个可运行的程序,那么最简单的方法就是使用命令行参数来传递输入而不是提示输入。
要运行的类的名称后面的每个命令行参数都是作为main方法接收的args字符串数组传入的。所以你可以简单地从这个数组中读出它们。
如果要提示输入,则需要使用System.out.println
写出消息。可以使用System.in
从“标准输入”中读取输入。由于这是一个输入流,它通常包含在BufferedReader和输入流读取器中。 E.g。
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type some data");
String theData = reader.readLine()