2008年12月11日 星期四

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

Write a program to let users input a year and then the program will give a “true” if that input is a leap year, otherwise the output will be “false”.
solution:
公元年數被4除盡的是閏年,但如被100除得盡而被400除不盡的則不是閏年。
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
boolean leap_year;
int year;
Scanner sc = new Scanner (System.in);
System.out.print("Please input a year:");
year = sc.nextInt();
leap_year = ((year % 4 == 0) && (year % 100 != 0)) (year % 400 == 0);
System.out.println(leap_year);}}
************************************
Write an application that reads three nonzero integers and determines and prints ifthey could represent the sides of a right–angled triangle
solution:用公式a*a+b*b=c*c
import javax.swing.JOptionPane;
public class Triangle {
public static void main( String args[] ){
int side1, side2,hypotenuse;
String input,result;
input = JOptionPane.showInputDialog("Enter length of side 1:" );
side1 = Integer.parseInt( input );
input = JOptionPane.showInputDialog("Enter length of side 2:" );
side2 = Integer.parseInt( input );
input = JOptionPane.showInputDialog("Enter length of hypotenuse:" );
hypotenuse = Integer.parseInt( input );
if ( ( side1 * side1 + side2 * side2 ) ==( hypotenuse * hypotenuse ) )
result = "These are the sides of a right-angled triangle.";
else
result = "These do not form a right-angled triangle.";
JOptionPane.showMessageDialog( null, result, "Result",JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );} }

沒有留言: