所以我可以创建将在通用输出中进行输出的代码,并且它们运行良好。当我尝试运行构造函数或 JOptionPane (GUI) 或来自 swing 导入的任何内容时,它根本无法正常工作。它会在我第一次创建 java 文件时编译并运行,但大约一个小时后,即使我没有更改代码,我也会收到错误消息。
我正在为我的班级创建一个游戏。这是代码。
/**Matt Werner
*Period 1
*Create a game using the random and array methods. objects will be necessary to make the game work. Build a 4 player game that will allow eah person to pick a number then the output will
*display a series of numbers, letters, or whatever you decide.
*/
import java.util.Scanner;
import java.util.Random;
import javax.swing.JOptionPane;
public class Game
{
public static void main(String args[])
{
Password.passwordcode();
}
}
class Password
{
public static void passwordcode()
{
Scanner input = new Scanner(System.in);
Scanner inputint = new Scanner(System.in);
String passwordInput;
int x = 0;
int a = 0;
int n;
do
{
System.out.print("Please type your password: ");
passwordInput = input.nextLine();
a++;
if(passwordInput.equals("Purple5040"))
{
System.out.println("Thank you.");
x = 5;
}
else
{
System.out.println("Incorrect password please try again.");
System.out.println();
}
if(a > 2)
{
throw new IllegalArgumentException("Too many tries. Please try again later.");
}
x++;
}
while(x < 3);
gamebuild.menu();
}
}
class gamebuild
{
static String names[] = new String[4];
static int numval[] = new int[4];
static int num;
static int index;
static int x;
static int a;
static Scanner inputstring = new Scanner(System.in);
static Scanner inputint = new Scanner(System.in);
public static void menu()
{
do
{
System.out.println("Select what you would like to do.");
System.out.println();
System.out.println();
System.out.println("1: Play game");
System.out.println("2: Exit");
System.out.println();
System.out.print("#");
x = inputint.nextInt();
switch(x)
{
case 1: build(); break;
case 2: System.out.print("Exiting..."); break;
default: System.out.println("Incorrect command please try again. Type any number to continue");
a = inputint.nextInt();
}
}
while(x != 2);
}
private static void build()
{
System.out.println("Roll dice to see who gets the highest number. Order will be decided in that manner. game must have 4 players.");
System.out.print("#");
dicebuild();
num = inputint.nextInt();
for(index = 0; index < num; index++);
{
System.out.print("Please enter your name: ");
names[index] = inputstring.nextLine();
}
}
private static void gamecode()
{
gamebuild.gui();
System.out.println("");
}
private static void dicebuild()
{
Random dice = new Random();
int number;
for(int counter = 1; counter <= 1; counter++);
{
number = 1 + dice.nextInt(6);
System.out.println(number + " ");
}
}
public static void gui()
{
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");
int num1 = Integer.parseInt(fn);
int num2 = Integer.parseInt(sn);
int sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The answer is " + sum, "GUI testing", JOptionPane.PLAIN_MESSAGE);
}
}
输出是
--------------------Configuration: <Default>--------------------
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Process completed.