本程序主要练习了控制结构(if、while等)的使用,另外联系了文本框的使用及数据格式化处理的相关操作。程序代码如下:......
    
      本程序主要练习了控制结构(if、while等)的使用,另外联系了文本框的使用及数据格式化处理的相关操作。程序代码如下:

Code
/**
 * @(#)Average.java
 *
 *
 * @author 
 * @version 1.00 2008/9/5
 */
package Class.Book;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Average {
    public Average() {
    }
    public static void main (String[] args) {
        int counter,
            total,
            value;
        double average;
        String input;
        
        total=0;
        counter=0;
        
        input=JOptionPane.showInputDialog("Enter the grade of the "+(counter+1)+"th student[-1 to QUIT]:");
        value=Integer.parseInt(input);        
        while(value!=-1){
            total+=value;
            counter++;
            
            input=JOptionPane.showInputDialog("Enter the grade of the "+(counter+1)+"th student[-1 to QUIT]:");
            value=Integer.parseInt(input);
        }
        if(counter!=0){
            DecimalFormat twodigits=new DecimalFormat("0.00");
            average=(double)total/counter;
            JOptionPane.showMessageDialog(null,"The average value of "+counter+" students is : "+twodigits.format(average),"AVERAGE",JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null,"ERROR: The number of the students is 0 !!","ERRAGE",JOptionPane.ERROR_MESSAGE);
        }
        
        System.exit(0);
    }
} 
 
 
     主要的新知识点是数据的格式化处理。twodigits是DecimalFormat类的一个对象,注意类对象的声明方法。另外还包括了数据的强制类型转换操作。
     进一步简单练习一下DecimalFormat的使用,[点此查看DecimalFormat的帮助文档],代码如下:
     

Code
/**
 * @(#)DecimalForamtTest.java
 *
 *
 * @author 
 * @version 1.00 2008/9/5
 */
package Class.Book;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class DecimalForamtTest {
    public DecimalForamtTest() {
    }
    
    public static void main (String[] args) {
        DecimalFormat dec1=new DecimalFormat("#0.####E0");
        DecimalFormat dec2=new DecimalFormat("##.#####E0");
        
        double a=122.451457545,
               b=15455456.54646464,
               c=0.001454512;
               
        JOptionPane.showMessageDialog(null,"a = "+a+"\nAfter Format by dec1:"+dec1.format(a)+"\nAfter Format by dec2:"+dec2.format(a),"Format a:",JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null,"b = "+b+"\nAfter Format by dec1:"+dec1.format(b)+"\nAfter Format by dec2:"+dec2.format(b),"Format b:",JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null,"c = "+c+"\nAfter Format by dec1:"+dec1.format(c)+"\nAfter Format by dec2:"+dec2.format(c),"Format a:",JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }
    
} 
 
 结果如下:

******************************************************************************
         刚开始学Java,慢慢来呗,咱是新手,咱怕谁。【无知者无畏,但无知者不要无所谓。】
        另外使用电脑的朋友,告诉你们一种保护眼睛的好方法: 
       桌面-]右键-]属性-]外观-]高级-]项目选择(窗口)、颜色1(L)选择(其它)将色调改为:85。饱和度:123。亮度:205-]添加到自定义颜色-]在自定义颜色选定点确定。【刚偷学来的,效果不错】