| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 719 人关注过本帖
标题:求助!
只看楼主 加入收藏
xishuiwuqing
Rank: 1
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-1
结帖率:100%
收藏
 问题点数:0 回复次数:6 
求助!
在JAVA中,给定一个正整数 m ,判断它的具体位数,分别打印没一位数,再按照逆序打印出各位数.
例如这里的 m=24863.这个程序怎么写啊???
2008-04-03 08:20
xishuiwuqing
Rank: 1
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-1
收藏
得分:0 
急啊
怎么还没反映呢?

2008-04-03 08:41
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
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]]
收到的鲜花
  • xishuiwuqing2008-04-03 10:36 送鲜花  3朵   附言:我很赞同
2008-04-03 08:59
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
转换成String就是很好搞的一个东西
2008-04-03 09:00
xishuiwuqing
Rank: 1
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-1
收藏
得分:0 
回复 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);
         }
       }
    }

2008-04-03 10:04
tomhuatek
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-5
收藏
得分:0 
哈哈
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);
        }

    }
}

这样是不是很简单啊
2008-04-05 05:46
xishuiwuqing
Rank: 1
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-1
收藏
得分:0 
回复 6# 的帖子
呵呵,谢谢 !
的确更简单了!

2008-04-24 10:05
快速回复:求助!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037359 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved