java 求三数和 平均数 最大数 最小数

| 0 Comments
      用JAVA实现三个数的求和,平均数,最大数和最小数。

import javax.swing.JOptionPane; // import class JOptionPane
public class Hw2_18 {
   public static void main( String args[] )
   {
      String firstNumber,   // first string entered by user
             secondNumber,third,result; // second string entered by user
      int n1,          // first number to add
          n2,n3,   // second number to add
          sum,a,p,small,large;              // sum of number1 and number2
      // read in first number from user as a string
      firstNumber =
         JOptionPane.showInputDialog( "请输入第一个数" );
      // read in second number from user as a string
      secondNumber =
         JOptionPane.showInputDialog( "请输入第二个数" );
      third =
         JOptionPane.showInputDialog( "请输入第三个数" );  
      // convert numbers from type String to type int
      n1 = Integer.parseInt( firstNumber );
      n2 = Integer.parseInt( secondNumber );
      n3 = Integer.parseInt( third );
      result="";
      sum=n1+n2+n3;
      p=n1*n2*n3;
      a=sum/3;
      if (n1>n2) {
        if (n1>n3) {
            if (n2>n3) { large=n1; small=n3; }
            else       { large=n1; small=n2; }
        }
        else { large=n3; small=n2;}
      }
      else {
        if (n2 > n3) {
           if (n1 > n3) {large=n2; small=n3;}
           else {large=large=n2; small=n1;}
        }      
        else {large=n3; small=n1;}
      }
      result=result+n1+"+"+n2+"+"+n3+"="+sum+"\n";
      result=result+n1+"*"+n2+"*"+n3+"="+p+"\n";
      result=result+"("+n1+"+"+n2+"+"+n3+")/3="+a+"\n";
      result=result+"最大数是 "+large+"\n";
      result=result+"最小数是 "+small+"\n";   
      // display the results
      JOptionPane.showMessageDialog(
         null, result, "Results",
         JOptionPane.PLAIN_MESSAGE );
      System.exit( 0 );   // terminate the program
   }
}

相关日志

Advertisements

Leave a comment

Featured Post

 

Blogger博客支持实时统计

 

曙光博客稍稍修改