如何要求用户输入1到50之间的整数,然后小程序打印出一定数量的星号

时间:2016-04-22 08:28:27

标签: javascript java loops for-loop applet

要求用户输入1到50之间的整数。然后小程序打印出一定数量的星号。

import java.applet.Applet;
import java.awt.*; 
import java.awt.event.*;

public class ForDemo extends Applet implements ActionListener{

     TextField num;
     int num1;
     Label prompt;

     public void init(){
         prompt = new Label("Enter a number that is less than 50");
         num = new TextField(10);
         add(prompt);
         add(num);
         num.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        num1 = Integer.parseInt(num.getText());
        repaint();
    }

    public void paint (Graphics g) {
    //For values of an int I from 1, 2, .., 12 the loop evaluates 6*i.
    //Note that the for loop variable can be defined in the loop.
    //Note also that the loop variable is used to position the printout.

    for (num1 = 1; num1 <=250; num1 ++)
        g.drawString("6 times " + num1 + " = " + 6*num1, 25, 25+20*num1);

    }
}

1 个答案:

答案 0 :(得分:0)

打字是不够的。你需要采取行动,你可以按Enter键或添加一些按钮。或者您创建在某些时间间隔内读取inpup的线程。