發新話題

[JAVA] 字串處理

[JAVA] 字串處理

引用:
/****************************************
說明:請在視窗上配置一文字方塊及一按鈕,在文字方塊上輸
入一大於0的正整數N(最多為十個位數),按上述按鈕後,輸
出每一個位數之數字,若該數字為0則不輸出,輸出位數的順序
為:十億位、億位、千萬位、百萬位、十萬位、萬位、千位、
百位、十位、個位。並找出此正整數中最大之數字,最後並將
數字反轉輸出,反轉輸出時若最前面有數字為0則不輸出
(如04321輸出4321)。以上之結果,必須輸出到檔案。程式
中必須有須有判斷範圍的程式,若是超出題目所訂定的字串
長度則要求重新輸入。
*************************************************/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//想想還有沒有其他的東西要import
import java.io.*;
public class p5 extends JFrame implements ActionListener//名稱改一改
{
    Container c;
    //設定UI元件
    JLabel lab,ans;
    JTextField txt;
    JButton bot;
    //設定共用的變數與類別
    public p5()   //建構元,名稱改一改
    {
        super("UI設計範本");
        c=getContentPane();//取得ContentPane
        //設定版面設定
        c.setLayout(new FlowLayout(FlowLayout.CENTER));//設定為用flowlayout
        //初始化UI元件
        lab=new JLabel("請輸入數字:");
        ans=new JLabel("");
        txt=new JTextField(10);
        bot=new JButton("計算");
        //將UI元件加入ContentPane
        c.add(lab);
        c.add(txt);
        c.add(bot);
        c.add(ans);
        //設定UI元件與滑鼠的事件觸發傾聽者
        bot.addActionListener(this);
        setSize(640,480);//設定size,顯示出去
        show();
    }
    public void paint(Graphics g)
    {
            super.paint(g);//畫出元件
            //額外的畫圖程式寫在這裡
    }
    //UI元件事件處理類別寫在這裡
    public void actionPerformed(ActionEvent e)
    {
       String item[]={"個位","十位","百位","千位","萬位","十萬位","百萬位",
           "千萬位","億位","十億位"};
       String getbr;
       int i,j,now,flag,max;
        //判斷輸入是否有錯
        if (txt.getText().length() >10 ) //長度超過或不足
                {
                        ans.setText("輸入數字超過十位數請重新輸入");
            repaint();
            return;
                }
        else if ( txt.getText().length() <=0)
        {
            ans.setText("輸入數字長度不足請重新輸入");
            repaint();
            return;
        }
        //開始計算
        max=0;
        getbr=txt.getText();
        while (getbr.charAt(0)=='0' || getbr.charAt(0)==' ') //殺掉前導0與空白
                        getbr=getbr.substring(1);
                j=getbr.length();
        //開檔案
        try
        {
            FileWriter fw=new FileWriter("output.txt");
            BufferedWriter bfw=new BufferedWriter(fw);
            for (i=0;i<getbr.length();i=i+1)
            {
                j=j-1;
                now=getbr.charAt(i)-'0'; //用Character.digit(now)也行
                if (now>0) //非0才輸出
                {
                       bfw.write(item[j]+"為"+now);
                       bfw.newLine();
                }
                if (now>max) max=now;
             } //for i
             bfw.newLine();
              bfw.write("最大為"+max);
              bfw.newLine();
              bfw.write("反轉輸出");
              bfw.newLine();
              flag=0;//判斷是否有前導0,先假設有
             for (i=getbr.length()-1;i>=0;i=i-1)
             {
                now=getbr.charAt(i)-'0';
                if (now>0) flag=1; //必須先判斷前導0
                if (flag==1) //如果沒有前導0了,就開始輸出
                      bfw.write(""+now);//化成字串
             } //for i
             bfw.flush();
             fw.close();
        }//try
        catch (IOException e1)
        {
            System.out.println("fsdfsdfsdfds");
        };
    }
    //滑鼠事件處理類別寫在這裡
  /***主程式***/
    public static void main(String args[]) //程式起點
    {
        p5 app=new p5(); //名稱改一改,啟動UI元件
        app.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                {
                        System.exit(0);
                }
        }); //處理視窗關閉要求
    }
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。