Java 计算机辅助程序
问题是这样的:“在教育中使用计算机被称为计算机辅助教学(CAI)。编写一个程序,帮助小学生学习乘法。使用一个随机对象生成2个正1位整数。然后程序会提示用户一个问题,例如“6乘7等于多少?” 然后,学生输入答案。接下来,程序检查学生的答案。如果答案正确,则显示消息“非常好!”并询问另一个多个问题。如果答案错误,则显示消息“否。请重试”并让学生反复尝试同一个问题,直到学生最终答对为止。将使用单独的方法生成每个新问题。当应用程序开始执行时以及每次用户正确回答问题时,将调用此方法一次。” 以下是我到目前为止的情况Java 计算机辅助程序,java,Java,问题是这样的:“在教育中使用计算机被称为计算机辅助教学(CAI)。编写一个程序,帮助小学生学习乘法。使用一个随机对象生成2个正1位整数。然后程序会提示用户一个问题,例如“6乘7等于多少?” 然后,学生输入答案。接下来,程序检查学生的答案。如果答案正确,则显示消息“非常好!”并询问另一个多个问题。如果答案错误,则显示消息“否。请重试”并让学生反复尝试同一个问题,直到学生最终答对为止。将使用单独的方法生成每个新问题。当应用程序开始执行时以及每次用户正确回答问题时,将调用此方法一次。” 以下是我到目前
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package programmingassignment5.pkg35;
/**
*
* @author Jeremy
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public abstract class ProgrammingAssignment535 extends JApplet implements ActionListener{
JTextField question, input;
JLabel prompt;
int answer, guess;
String questionString;
/**
* @param args the command line arguments
*/
public void init(){
// set guess to a flag value indicating no user input 17
guess = -999;
// create text fields and a label 20
question = new JTextField( 20 );
question.setEditable( false );
prompt = new JLabel( "Enter your answer: " );
input = new JTextField( 4 );
input.addActionListener( this );
// add components to applet 29
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( question );
container.add( prompt );
container.add( input );
// generate a question 36
createQuestion();
}
public void paint( Graphics g ){
super.paint( g );
// determine whether response is correct 44
// if guess isn't flag value 45
if (guess != -999){
if (guess != answer)
g.drawString( "No. Please try again.", 20, 70 );
else {
g.drawString( "Very Good!", 20, 70 );
createQuestion();
}
guess = -999;
}
}
// verify the entered response
public void actionPerformed( ActionEvent e ){
guess = Integer.parseInt( input.getText() );
// clear the text field
input.setText( "" );
// display the correct response
repaint();
}
// create a new question and a corresponding answer
public void createQuestion(){// get two random numbers between 0 and 9 73
int digit1 = ( int ) ( Math.random() * 10 );
int digit2 = ( int ) ( Math.random() * 10 );
answer = digit1 * digit2;
questionString = "How much is " + digit1 + " times " + digit2 + " ?";
// add to applet 81
question.setText( questionString );
}
} // end class
它给我的唯一错误是程序缺少主类。通常这是一个简单的修复方法,但我无法在这里找到如何实现它而不破坏所有内容的方法。有任何建议吗?听起来您需要一些关于Swing的基本“Hello World”帮助。看一看 您的代码存在许多问题(为什么类是抽象的?为什么要重写paint?)。请查阅一些swing教程。至于您的主要方法问题,请尝试以下方法:
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
ProgrammingAssignment535 myApplet = new ProgrammingAssignment535();
myApplet.init();
myApplet.setVisible( true );
}
} );
}
您还必须使类不抽象。那么,您的
main
方法在哪里?您的程序必须有一个publicstaticvoidmain(String[]args)
method.Argh击败了我lol,你需要你的main方法来启动你的程序lol我知道它需要一个main方法。我的问题是我不知道把它放在哪里。我尝试的每一个地方都会带来大量错误。@Jesper每次我尝试添加公共静态void main(字符串[]args)我收到一个错误,说我在静态上下文中引用非静态变量?谢谢你的建议。我将查阅一些基本教程。至于使类抽象,netbeans显示了一个错误,说要使用ActionListener,类需要抽象?@JeremyBrooks我怀疑你误读了错误消息。当你irst添加了接口,您收到一个错误,说您需要添加缺少的actionPerformed方法或使类抽象。您是对的,我从抽象更改了它并添加了主参数。当我这样做时,程序成功生成,但netbeans中除了生成成功消息外没有显示任何内容。感谢您的支持救命啊。如果你没有更多的建议,我想我可能会从头开始。不过再次谢谢你!