问题是:“在教育中使用计算机被称为计算机辅助教学(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
它给我的唯一错误是该程序缺少主类。通常这是一个简单的修复,但我无法弄清楚如何在不破坏任何建议的情况下实现它?
答案 0 :(得分:1)
听起来你需要一些基本的" Hello World"对Swing有所帮助。看看here。
您的代码存在许多问题(为什么您的类是抽象的?为什么要重写油漆?)。查看一些摇摆教程。至于你的主要方法问题,试试这样的事情:
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
ProgrammingAssignment535 myApplet = new ProgrammingAssignment535();
myApplet.init();
myApplet.setVisible( true );
}
} );
}
您还必须使课程不抽象。