我为编程类(class)编写了这段代码,它完整且正确,我只是出于我自己的知识对此感兴趣。我怎样才能让这个程序在 Eclipse 之外运行并将扫描器和输出放入 JFrame 或类似的东西中?任何意见和建议都意义重大。我对 java 很陌生,这是我的第三个程序。
这是主类
import java.util.Scanner;
public class SeqGenTest
{
/**
* Instance Variables passed to SeqGen.java
*/
String max;
String minmax;
String maxdiv;
boolean loop = true;
int a;
int b;
int c;
/**
* main method that allows user input to select and execute commands
* based upon the input of said user
* @param max, minmax, maxdiv,
* @return ints based upon calculation
*/
public static void main(String[] args)
{
boolean loop = true;
while(loop = true)
{
System.out.println("Enter a command (max, minmax, maxdiv, end)");
Scanner inpt = new Scanner(System.in);
String userInpt = inpt.nextLine();
if (userInpt.equalsIgnoreCase("max"))
{
System.out.println("Please enter your Max (intergers)");
int maximum = 0; //= inpt.nextInt();
try {
maximum = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
if (maximum <= 0)
{
System.out.println("Error, empty sequence");
}
else if (maximum < -9 || maximum > 200)
{
System.out.println("Error, bounds exception");
}
else if (maximum > 0 && maximum < 201)
{
System.out.println("Here is your sequence:");
SeqGen x = new SeqGen (maximum);
while(x.hasNextValue()){
System.out.println(x.getNextValue());}
}
}
else if (userInpt.equalsIgnoreCase("minmax"))
{
System.out.println("Please enter your min, max (intergers)");
int minimum = 0;
int maximum = 0;
try {
minimum = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
try {
maximum = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
if (minimum > maximum)
{
System.out.println("Error, empty sequence");
}
else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200)
{
System.out.println("Error, bounds exception");
}
else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201)
{
System.out.println("Here is your sequence:");
SeqGen x = new SeqGen (minimum, maximum);
while(x.hasNextValue()){
System.out.println(x.getNextValue());}
}
}
else if (userInpt.equalsIgnoreCase("maxdiv"))
{
System.out.println("Please enter your min, max, div (intergers)");
int minimum = 0;
int maximum = 0;
int divisor = 0;
try {
minimum = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
try {
maximum = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
try {
divisor = inpt.nextInt();
} catch (Exception e) {
System.out.println("The value you have input is not an integer");
inpt.close();
return;
}
if (maximum < minimum || minimum < 1 || divisor > maximum)
{
System.out.println("Error, empty sequence");
}
else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200 || divisor < -9 || divisor > 200)
{
System.out.println("Error, bounds exception");
}
else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201 && divisor > 0 && divisor < 201)
{
System.out.println("Here is your sequence:");
SeqGen x = new SeqGen (minimum, maximum, divisor);
while(x.hasNextValue()){
System.out.println(x.getNextValue());}
}
}
else if (userInpt.equalsIgnoreCase("end"))
{
System.out.println("Program Terminated Normally");
inpt.close();
System.exit(0);
}
else
{
System.out.println("Error, bounds exception. System will now exit");
inpt.close();
System.exit(0);
}
}
}
}
这里是“其他类(class)?”我不确定正确的术语是什么。
public class SeqGen
{
//instance vars
int minmax;
int max;
int maxdiv;
int current;
boolean hasNextValue;
boolean nextValue;
boolean isMax;
boolean isMinMax;
boolean isMaxDiv;
int getNextValue;
/**
* Constructor for objects of class SeqGen
*/
public SeqGen(int a)
{
minmax = 1;
max = a;
maxdiv = 1;
current = minmax;
isMax = true;
}
/**
* Constructor for objects of class SeqGen
*/
public SeqGen(int b, int a)
{
minmax = b;
max = a;
maxdiv = 1;
current = minmax;
isMinMax = true;
}
public SeqGen(int b, int a, int c)
{
minmax = b;
max = a;
maxdiv = c;
current = minmax;
isMaxDiv = true;
}
public boolean hasNextValue()
{
if (current <= max) {return true; }
else {return false;}
}
/**
* Computes math based upon user entered commands
* @return correct computation (int)
*/
public int getNextValue()
{
while (current < max)
{
if (current%maxdiv == 0)
{
return current++;
}
else
{
current++;
}
}
return current++;
}
}
最佳答案
基本上我可以想到两种选择:
扩展
JFrame
类并使用带有JTextArea
的嵌入式控制台,如本 answer 所示。 .扩展
JFrame
类并创建一个框架,其中有一个按钮来执行从具有所有不同命令的JCombobox
中选择的命令,一堆JTextField
来获取用户输入的值并使用JLabel
打印结果。public class SeqGenTest extends JFrame{ public SeqGenTest(){ //set up all you UI here. //use these to show the frame. setSize(600, 600); setVisible(true); } }
其他信息:
关于java - 我怎样才能让这段代码在 Eclipse 之外运行并允许用户在 JFrame 或类似的东西中输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927513/