2008年12月11日 星期四

simple java(十一)---application(5)

Write an application that reads in two integers and determines and prints if the first is a multiple of the second.
solution:


import javax.swing.*;

public class range {
public static void main( String args[] ){
int number1,number2;
String input1,input2,result;

input1=JOptionPane.showInputDialog("enter first integer");
number1=Integer.parseInt(input1);

input2=JOptionPane.showInputDialog("enter second integer");

number2=Integer.parseInt(input2);
if (number1%number2==0)
result=number1+"is a multiple of "+ number2;

else
result=number1+"is a not multiple of "+ number2;
JOptionPane.showMessageDialog(null,result);System.exit( 0 );}
}
******************************
Write an application that inputs three integers from the user and displays the sum,average, product, smallest and largest of the numbers in an information messagedialog.
solution:
同之前果D差唔多.只不過把它們結合..很容易的.如下:
import javax.swing.*;

public class range {
public static void main( String args[] ){
int number1,number2,number3,sum,product,average;
String input1,input2,input3,result;

input1=JOptionPane.showInputDialog("enter first integer");
number1=Integer.parseInt(input1);

input2=JOptionPane.showInputDialog("enter second integer");
number2=Integer.parseInt(input2);

input3=JOptionPane.showInputDialog("enter third integer");
number3=Integer.parseInt(input3);result = "For the numbers " + number1 + " "+ number2 + " " + number3 + "\n";
sum=number1+number2+number3;
product=number1*number2*number3;
average=(number1+number2+number3)/3;

if (number2>.number1){
result="The largest is"+ number2+"\n"+"The smallest is"+number1;}

if(number3>.number2){
result="The largest is"+ number3+"\n"+"The smallest is"+number1;}

if(number3<.number1){
result="The smallest is"+ number3;}

JOptionPane.showMessageDialog(null,"For the numbers " + number1 + " "+ number2 + " " + number3 +
"\n"+result+"\n"+"The sum is"+sum+"\n"+"The product is"+product+"\n"+"the average is"+average);

System.exit( 0 );}
}

沒有留言: