简体   繁体   中英

How to ask the user to input a whole number between 1 and 50, and then the applet prints out a line of this number of stars

Ask the user to input a whole number between 1 and 50. The applet then prints out a line of this number of stars.

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);

    }
}

Typing is not enough. You need to take action and you do that either by pressing enter or you add some button. Or you create thread that reads inpup in some intervals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM