从后面输入一串数字的逗号 [英] Input comma to a string of numbers from back

查看:99
本文介绍了从后面输入一串数字的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反转一串数字并输入逗号而没有数字格式类。



当输入1000这样的数字时,输出将是



0,0,0,1



而不是


000,1



我的尝试:



 字符串 num = JOptionPane.showInputDialog( 输入数字); 
Int length = num.length() - 1 ;
字符串 res = ;
Char ch;

// 对于数千的数字
If(length< = 6){
for int k = length; k> = 0; k- - ){
ch = num.chartAt(k);
res + = ch;
if (ch == num.chartAt( 2 )){
res + = ;
}
}
}
System.out.println(res);

解决方案

< blockquote>试试这个:

 int integer = 1000; 
String string = Integer.toString(integer);
String reverseString = new StringBuffer(string).reverse()。toString();
String [] splitArray = reverseString.split();
String finalResult = String.join(,,splitArray);
System.out.println(finalResult);


应该是

 字符串 num = JOptionPane.showInputDialog( 输入数字< /跨度>); 
int length = num.length() - 1 ;
字符串 res = ;
char ch;

// 对于数千的数字
if (length< = 6){
for int k = length; k> = 0; k - ){
ch = num.charAt(k);
res + = ch;
if (k == 1)
res + = ;
}
}
System.out.println(res);


I'm trying to reverse a string of numbers and input comma without Number format class.

When a number like 1000 is inputted the output will be

0,0,0,1

Instead of

000,1

What I have tried:

String num=JOptionPane.showInputDialog("enter number");
    Int length=num.length()-1;
    String res="";
    Char ch;

    //For numbers with thousands
    If(length<=6){
       for(int k=length; k>=0; k--){
              ch=num.chartAt(k);
              res+=ch;
              if(ch==num.chartAt(2)){
                    res+=",";
              }
        }
    }
    System.out.println(res);

解决方案

Try this:

int integer = 1000;
String string = Integer.toString(integer);
String reverseString = new StringBuffer(string).reverse().toString();
String [] splitArray = reverseString.split("");
String finalResult = String.join(",", splitArray);
System.out.println(finalResult);


It should be

String num=JOptionPane.showInputDialog("enter number");
    int length=num.length()-1;
    String res="";
    char ch;

    //For numbers with thousands
    if(length<=6){
       for(int k=length; k>=0; k--){
              ch=num.charAt(k);
              res+=ch;
              if(k==1)
                    res+=",";
        }
    }
    System.out.println(res);


这篇关于从后面输入一串数字的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆