当前位置: 代码迷 >> Java相关 >> 数字的逆序输出,可以不用函数的递归调用吗?
  详细解决方案

数字的逆序输出,可以不用函数的递归调用吗?

热度:379   发布时间:2008-03-27 22:47:41.0
数字的逆序输出,可以不用函数的递归调用吗?
从键盘上输入一个不大于6位的数,判断他的具体位数,然后逆序输出各位数字!  
代码如下:
程序代码:
package baogao;
import java.util.Scanner;
public class Untitled1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个不大于5位的数:");
     int n = in.nextInt();
    int i,k,m=10,ik_1,ik_2,ik_3,ik_4;
    for(i=1;i<=4;i++)
    { m=m*10;
        if (m > n)
            break;
    }  k=i+1;
    int ik=n/k;         
    System.out.println(ik);
    if(k>1) {  ik_1=(m-ik*k)/(k-1);
        System.out.println(ik_1);}
    if (k>2) {  ik_2=(m-ik*k-ik_1*(k-1))/(k-2);
        System.out.println(ik_2);}   
    if(k>3) { ik_3=(m-ik*k-ik_1*(k-1)-ik_2*(k-2))/(k-3);
        System.out.println(ik_3);}
    if(k>4) {ik_4=(m-ik*k-ik_1*(k-1)-ik_2*(k-2)-ik_3*(k-3))/(k-4);
        System.out.println(ik_4); }
   
    if(n>=99999)  System.out.println("你输入的数字超过5位!");
      
    }
}


但是每个if 调用上面的ik_1、ik_2、ik_3时候,都会报错,是不是需要用构造函数,我下面该怎么做??

搜索更多相关的解决方案: 逆序  递归  函数  数字  Scanner  

----------------解决方案--------------------------------------------------------
我是菜鸟~~~~~~~~~刚学Java,看了之后斗胆说一句,那个是不是因为ik的问题啊。还有if (m > n)     m=m*10;
            break;
那个n :int n = in.nextInt();这...这m和n能比较吗?
----------------解决方案--------------------------------------------------------
可以转换成String类型撒。

非用int的话,可以%运算和/运算结合。
----------------解决方案--------------------------------------------------------
太复杂了
转换成StrngBuffer,语句reverse就搞定了
----------------解决方案--------------------------------------------------------
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);  
            
          }   
          }
本帖最近评分记录
2008-04-03 01:05:08
sunkaidong

来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4492
专家分:137
注 册:2006-12-28
  得分:0 
楼上程序不错...特别是对输入数字的处理...给你加点分
----------------解决方案--------------------------------------------------------
  相关解决方案