import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Digitos extends Applet implements ActionListener {
public void init() {
resize(600, 100);
}
TextField t1, t2;
Button bc, bl;
Label l1;
public Digitos() {
l1 = new Label("Ingrese el numero : ");
t1 = new TextField(8);
t2 = new TextField("el numero tiene: ");
bc = new Button("Calcular");
bl = new Button("Limpiar");
add(l1);
add(t1);
add(t2);
add(bc);
add(bl);
bc.addActionListener(this);
bl.addActionListener(this);
}
public void paint(Graphics g) {
double n = Double.parseDouble(t1.getText());
int ac, c;
c = 0;
if (n<=0) n= n* (-1);
do {
n = n / 10;
c = c + 1;
} while (n >= 1);
t2.setText("el numero tiene: " + c);
}
public void actionPerformed(ActionEvent ae) {
repaint();
if (bl == ae.getSource()) {
t1.setText("");
t2.setText("el numero tiene: ");
}
}
}
correción
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.lang.Math;
public class correccion extends Applet implements ActionListener {
double a,b;
Button b1,b2;
TextField t1;
TextField t2;
public void init() {
resize(655,50);
}
public correccion (){
t1= new TextField(10);
b1=new Button("CALCULE");
t2= new TextField(20);
b2=new Button("LIMPIE");
add (t1);
add (b1);
add(t2);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void paint (Graphics g){
g.drawString("Ingrese un numero:",20,20);
a = Double.parseDouble(t1.getText());
int c=0;
do{
a = a / 10;
c = c + 1 ;
} while(a>1);
t2.setText("El numero tiene: "+ c + " digitos");
}
public void actionPerformed(ActionEvent ae) {
repaint();
if(b1==ae.getSource()){
}
if (b2==ae.getSource()){
t1.setText("");
t2.setText("");
}
}
}
0 comentarios: