gpt4 book ai didi

java - 将 javax.swing 转换为命令输入

转载 作者:行者123 更新时间:2023-12-02 07:28:04 24 4
gpt4 key购买 nike

如何将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 );

}
}

最佳答案

这完全取决于您想要做什么。如果您只想要一个可运行的程序,那么最简单的方法是使用命令行参数来传递您的输入,而不是提示输入。

要运行的类名称后面的每个命令行参数都作为 main 方法接收的 args 字符串数组传入。所以你可以简单地从这个数组中读取它们。

如果您想提示输入,那么您需要使用System.out.println 写出消息。可以使用 System.in 从“标准输入”读取输入。由于这是一个输入流,因此通常包装在 BufferedReader 和输入流读取器中。例如

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type some data");
String theData = reader.readLine()

关于java - 将 javax.swing 转换为命令输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com