2008年12月11日 星期四

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

Write an application(i) asks the user to enter two integers(ii) displays the larger number followed by the words “is larger” in an informationmessage dialog.(iii) If the numbers are equal, print the message “These numbers are equal.”

solution:輸入2個數字..比較它們的大小..用else if去寫.

import javax.swing.*;

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

String give,give2;

give=JOptionPane.showInputDialog("enter first integer:");

number1=Integer.parseInt(give);

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

if (number1>number2){
JOptionPane.showMessageDialog(null,number1+"is the lager");}

else if(number2>number1){JOptionPane.showMessageDialog(null,number2+"is the lager");}
else{
JOptionPane.showMessageDialog(null,"These numbers are equal");}

System.exit( 0 );}
}
********************************
Write an application that reads an integer and determines and prints whether itis odd or even.
solution:佢要你寫一個揾雙數..單數的程式..我地可以要if else去寫:如下:
import javax.swing.*;

public class range {
public static void main( String args[] ){
int number1;
String input;
input=JOptionPane.showInputDialog("enter integer");

number1=Integer.parseInt(input);

if(number1%2==0)
JOptionPane.showMessageDialog(null,number1+"is even");
else
JOptionPane.showMessageDialog(null,number1+" is odd");

System.exit( 0 );}
}

沒有留言: