当前位置: 代码迷 >> Java相关 >> 求助!
  详细解决方案

求助!

热度:407   发布时间:2008-04-03 08:20:31.0
求助!
在JAVA中,给定一个正整数 m ,判断它的具体位数,分别打印没一位数,再按照逆序打印出各位数.
例如这里的 m=24863.这个程序怎么写啊???
----------------解决方案--------------------------------------------------------
急啊
怎么还没反映呢?
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import   javax.swing.text.AttributeSet;   
import   javax.swing.text.BadLocationException;   
import   javax.swing.text.PlainDocument;

public class NumAna extends JFrame{
    private Container container;
    private JTextField input;
    private JTextArea output;
    private JLabel inputLabel;
    private JButton button,closeButton;
    
    public NumAna(){
        super("数字判断");
        
        container = getContentPane();
        
        input = new JTextField("",15);
        inputLabel = new JLabel("请输入数字");
        input.setDocument(new customDoc());
        output = new JTextArea(20,30);
        output.setEditable(false);
        
        button = new JButton("分析");
        button.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){    
                        String num = input.getText();
                        if(num.length()==0)
                            output.setText("您没有输入数字。");
                        else{
                            output.append("您输入的是:"+input.getText()+",是"+num.length()+"位数.\n");
                            for(int i=0;i<num.length();i++)
                                output.append("第"+(i+1)+"位数是:"+num.charAt(i)+"\n");
                            output.append("\n");
                            StringBuffer buf = new StringBuffer(num);
                            buf.reverse();
                            output.append("逆序后为"+buf.toString()+"\n");
                            input.setText("");
                            for(int i = 0;i<buf.toString().length();i++)
                                output.append("第"+(i+1)+"位数是:"+buf.toString().charAt(i)+"\n");
                        }
                        input.grabFocus();
                }
            }
        );
        closeButton = new JButton("关闭");
        closeButton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.exit(0);
                }
            }
        );
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(inputLabel);
        panel1.add(input);
        container.add(new JScrollPane(output));
        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.add(closeButton);
        panel2.add(button);
        container.add(panel1,BorderLayout.NORTH);
        container.add(panel2,BorderLayout.SOUTH);
        
        pack();
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation((screenWidth - 800) / 2, (screenHeight - 110) / 2);
        
        setVisible(true);
        setResizable(false);
    }
    
    public static void main(String args[]){
        NumAna num = new NumAna();
        num.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


    class   customDoc   extends   PlainDocument{        
        public   void   insertString(int   offs,   String   str,   AttributeSet   a)     
            throws   BadLocationException   {   
                for(int i=0;i<str.length();i++){  
                    if(str.charAt(i)<'0'||str.charAt(i)>'9'){   
                        return;   
                    }
                }   
            super.insertString(offs,str,a);  
            
          }   
          }

[[it] 本帖最后由 freish 于 2008-4-3 09:03 编辑 [/it]]
本帖最近评分记录
2008-04-03 00:59:42
freish

等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
  得分:0 
转换成String就是很好搞的一个东西
----------------解决方案--------------------------------------------------------
回复 3# 的帖子
谢谢!
我也做好了,呵呵 !
package lk3;

public class lk {
    public static void main(String[] args) {
        int m = 24689, i, j, k, n = 1, w;
        w = m;
        // if(m>=0&&m<10){
        //     System.out.println("m是一位数");
        // }
        // if(m>=10&&m<100){
        //     System.out.println("m是二位数");
        // }
        // if(m>=100&&m<1000){
        //     System.out.println("m是三位数");
        // }
        // if(m>=1000&&m<10000){
        //     System.out.println("m是四位数");
        // }
        // if(m>=10000&&m<100000){
        //     System.out.println("m是五位数");
        // }
      
        for(j=1;;j++){

        if (w / 10 != 0) {
            n++;
            w = w / 10;
        } else {
            break;
        }
        }
        System.out.println("m的位数是:" + " " + n);

         for (i = 1; i <= 5; i++) {
             k = m % 10;
            m = m / 10;
           System.out.println(" " + k);
         }
       }
    }
----------------解决方案--------------------------------------------------------
哈哈
class Test{
    public static void main(String[] args){
        
    int m = 24863;
        String s = Integer.toString(m);
        byte[] bb = new byte[5];
        bb = s.getBytes();
        for(int i = 0; i < 5; i++){
            System.out.println(bb[i] - 48);
        }

    }
}

这样是不是很简单啊
----------------解决方案--------------------------------------------------------
回复 6# 的帖子
呵呵,谢谢 !
的确更简单了!
----------------解决方案--------------------------------------------------------
  相关解决方案