Page 1 of 1

does it work?

Posted: Mon Mar 13, 2006 08:50
by nobile
[syntax="java"]
import javax.swing.JOptionPane;
public class Modulo
{
public static int mod(int x)
{
int s=0,y;
for(int i=100;i>0;i/=10)
{
y=(x/i);
s+=y;
x-=(y*i);
}
return(s);
}
public static void main(String arg[])
{
String sn;
int in,suma=0;
sn=JOptionPane.showInputDialog("Give me a three digit number: ");
in=Integer.parseInt(sn);
while(in>1)
{
if(in>=100 && in<=999)
{
suma=mod(in);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Error, the number you gave me wasn't three digits.\nGive me a number again.","Error",JOptionPane.ERROR_MESSAGE);
}
sn=JOptionPane.showInputDialog("IGive me a three digit number: ");
in=Integer.parseInt(sn);
}
JOptionPane.showMessageDialog(null,"The sum of the digits is: "+suma,"Resultados",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
[/syntax]
Just run it, please =)