苏丽丽吧 关注:15贴子:779
  • 6回复贴,共1

JAVA 示例小程序

只看楼主收藏回复

//homework 4.14 GrossPay.java 
//Determine the gross pay for each of three employees 

//Java extension packages 
import javax.swing.JOptionPane; 

import java.text.DecimalFormat; 

public class GrossPay{ 
    
   public static void main(String args[]) 
   { 
      String hours,rate; 
      int num=0,h; 
      double pay=0,sum=0,r; 
       
      DecimalFormat twoDigits=new DecimalFormat("0.00"); 

      hours=JOptionPane.showInputDialog("Enter the number of hours each employee worked and -1 to quit:"); 

      h=Integer.parseInt(hours); 

      while(h!=-1) 
     { 
         rate=JOptionPane.showInputDialog("Enter the hourly rate of each employee:"); 
         r=Double.parseDouble(rate); 

         if(h>40) 
            pay=(1.5*h-20)*r; 
         else 
            pay=h*r; 

         num++; 
         sum+=pay; 
          
         if(num==3) 
        { 
            JOptionPane.showMessageDialog(null,"The gross pay of the three employees is "+twoDigits.format(sum)); 
            num=0; 
            sum=0; 

            hours=JOptionPane.showInputDialog("Enter the number of hours each employee worked and -1 to quit:"); 
            h=Integer.parseInt(hours); 
        } 
        else
        {
           hours=JOptionPane.showInputDialog("Enter the number of hours each employee worked and -1 to quit:");
           h=Integer.parseInt(hours);
        }
     }//end while 

   }//end method 

}//end class 


1楼2005-09-27 15:24回复
    //homework 4.16 Table.java 
    //Use looping to print the table 

    //Java core packages 
    import java.awt.Graphics; 

    //Java extension packages 
    import javax.swing.JApplet; 

    public class Table extends JApplet{ 

     public void paint( Graphics g ) 
     { 
     int n=1; 
     String output=""; 
     
     super.paint( g ); 
     
     g.drawRect( 10,10,200,200);
     g.drawString( "N 10*N 100*N 1000*N",25,25 ); 
     
     while(n<=5) 
     { 
     output=n + " " + 10*n + " " + 100*n + " " + 1000*n; 
     g.drawString(output,25,40+15*n); 
     n++; 
     }//end while 

     }//end method 
     
    } //end class 

    <html>
    <applet code="Table.class" width="250" height="250">
    </applet>
    </html>


    2楼2005-09-27 15:25
    回复
      2025-08-27 16:29:22
      广告
      不感兴趣
      开通SVIP免广告
      //homework 4.22 Demo.java
      //which is correct

      //Java extension packages 
      import javax.swing.JOptionPane;

      public class Demo{
       public static void main( String args[] )
       {
       int x,y;
       String s1,s2;
       
       s1=JOptionPane.showInputDialog("Enter x:"); 
       x=Integer.parseInt(s1);
       s2=JOptionPane.showInputDialog("Enter y:"); 
       y=Integer.parseInt(s2);
       
       if( y==8 )
       if( x==5 )
       System.out.println("@@@@@");
       else
       System.out.println("#####");
       System.out.println("$$$$$");
       System.out.println("&&&&&");
       
       System.exit(0);
       }

      }


      3楼2005-09-27 15:25
      回复
        //homework 4.30 Triangle.java 
        //Determine if the three numbers could represent the sides of a right triangle

        //Java extension packages 
        import javax.swing.JOptionPane; 

        public class Triangle{ 
         
         public static void main(String args[]) 
         { 
         String num1,num2,num3; 
         int a,b,c,max; 
         
         num1=JOptionPane.showInputDialog("Enter the side a:"); 
         a=Integer.parseInt(num1); 
         
         num2=JOptionPane.showInputDialog("Enter the side b:"); 
         b=Integer.parseInt(num2);
         
         num3=JOptionPane.showInputDialog("Enter the side c:"); 
         c=Integer.parseInt(num3);
         
         if(a==0||b==0||c==0||!((a+b)>c&&(a+c)>b&&(b+c)>a))
         {
          JOptionPane.showMessageDialog(null,"Illege number,Enter again:");
         
         num1=JOptionPane.showInputDialog("Enter the side a:"); 
         a=Integer.parseInt(num1); 
         
         num2=JOptionPane.showInputDialog("Enter the side b:"); 
         b=Integer.parseInt(num2);
         
         num3=JOptionPane.showInputDialog("Enter the side c:"); 
         c=Integer.parseInt(num3);
           
         }
         
         max=a>b?a:b;
         max=max>c?max:c;
         
          if( 2 * max * max == a * a + b * b + c * c )
         JOptionPane.showMessageDialog
         (null,"The three numbers could represent the sides of a right triangle.");
         else
         JOptionPane.showMessageDialog
         (null,"The three numbers could not represent the sides of a right triangle."); 
         
         System.exit(0);
         
         }//end method 

        }//end class


        4楼2005-09-27 15:27
        回复
          • 61.183.207.*
          请问诸位,各个帖子都有什么作用呀
          他们的输出是什么
          能否告知一下?
          我的邮箱是fageyidingqing8@sina.com


          6楼2006-02-26 09:17
          回复
            • 218.193.25.*
            这只是课程小练习,根本不作何用。忘了删掉了。。。。。
            有兴趣想知道它们输出什么,运行一下就可以了


            7楼2006-02-26 13:29
            回复
              • 218.28.135.*
              import javax.swing.JOptionPane;

              public class Demo{
               public static void main( String args[] )
               {
               int x,y;
               String s1,s2;
               s1=JOptionPane.showInputDialog("Enter x:");
               x=Integer.parseInt(s1);
               s2=JOptionPane.showInputDialog("Enter y:");
               y=Integer.parseInt(s2);

               if( y==8&&x==5 )
               {
               System.out.println("@@@@@");
               }
              else
              {
               System.out.println("#####");
               System.out.println("$$$$$");
               System.out.println("&&&&&");
              }

               }

              }


              8楼2006-04-09 09:34
              回复