Java程序设计习题集
System.out.println(\}
}
四、写出下列程序完成的功能
1. public class Sum
{ public static void main( String args[ ]) { double sum = 0.0 ;
for ( int i = 1 ; i <= 100 ; i + + ) sum += 1.0/(double) i ;
System.out.println( \ }
}
答: sum=5.18737517639621
2.import java.io.* ;
public class Reverse
{ public static void main(String args[ ]) { int i , n =10 ;
int a[ ] = new int[10]; for ( i = 0 ; i < n ; i ++ ) try {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
a[i] = Integer.parseInt(br.readLine( )); // 输入一个整数
} catch ( IOException e ) { } ; for ( i = n-1 ; i >= 0 ; i ―― ) System.out.print(a[i]+\ System.out.println( ); } } 答:
3.import java.awt.*;
- 25 - 第 页
Java程序设计习题集
public class abc
{ public static void main(String args[]) { new FrameOut(); } }
class FrameOut extends Frame // Frame为系统定 { Button btn; // 义的窗框类
FrameOut( )
{ super(\按钮\
btn = new Button(\按下我\ setLayout(new FlowLayout( )); add(btn);
setSize(300,200); show( ); } }
答:
4. import java.io.*; public class Class1 {
public static void main(String args[ ]){ int i , Max , Min ; int a[ ] = { 12,67,8,98,23,56,124,55,99,100 }; Max = Min = a[0];
for ( i = 1 ; i < a.length; i ++ ) { if ( a[i] if ( a[i]>Max ) Max = a[i]; } System.out.println( Max+\ System.out.println( ); } } 答: 5. public class ABC { public static void main(String args[ ]){ int i , j ; - 26 - 第 页 Java程序设计习题集 int a[ ] = { 12,67,8,98,23,56,124,55,99,100 }; for ( i = 0 ; i < a.length-1; i ++ ) { int k = i; for ( j = i ; j < a.length ; j++ ) if ( a[j] a[k] = temp; } for ( i =0 ; i System.out.print(a[i]+\ System.out.println( ); } } 答 6.import java.io.*; public class Class1 { public static void main( String args[] ) { Fact N = new Fact( 4 ); System.out.println( N.fact( ) ); } } class Fact { int n ; Fact( int nn ) { n = nn; } int fact( ) { } 答: 7.import java.awt.*; import java.applet.Applet; - 27 - 第 页 int i , f = 1; for ( i=1; i<=n; i++ ) f = f*i; return f; } Java程序设计习题集 public class DrawMyImage extends Applet { } Image myImage; // 定义一个图像类Image的对象myImage public void init( ) { } myImage = getImage(getDocumentBase( ),\ public void paint(Graphics g) { } g.drawImage(myImage,0,0,this); 答: 8. import java.io.*; public class Class1 { public static void main( String args[ ] ) { SubClass a = new SubClass( 10,3 ); System.out.println( a.exp( )); } } class SuperClass { float x; int n; SuperClass( float xx , int nn ) { x = xx ; n = nn; } } class SubClass extends SuperClass { SubClass( float xx , int nn ) { super( xx , nn ); } float exp( ) { } } 答: 9. import java.awt.*; import java.applet.Applet; - 28 - 第 页 float s = 1; for ( int i = 1; i<=n; i++ ) s = s*x; return s;