java - 在小数点处对齐 float 列 Java

标签 java floating-point alignment double

我想在小数点处对齐一列 float 。我知道如果限制小数点后的点数很容易做到,但我希望用户能够输入无限数量和长度的 float 。

这是我的程序中处理 float 对齐的部分:

String[] input = new String[3];
    System.out.format("%n%n\tEnter three floating point numbers%n");
    for (int i=0;i<3;i++)
        input[i] = in.nextLine();

    System.out.format("\tHere they are lined up on their decimal point%n");

/*This is used to find decimal location in each string*/
    int[] decLoc = new int[3];
    for (int i=0; i<3; i++)
    {
        for(int j=1; j<=input[i].length();j++)
            if(input[i].charAt(j-1) == '.') 
                decLoc[i] = j;
    }
/*print 5 spaces before number if the decimal is at place 0, 4 spaces for 1...*/ 
    for(int i=0;i<3;i++)
    {
        if(decLoc[i]==0) System.out.print("      ");
        else if(decLoc[i]==1) System.out.print("     ");
        else if(decLoc[i]==2) System.out.print("    ");
        else if(decLoc[i]==3) System.out.print("   ");
        else if(decLoc[i]==4) System.out.print("  ");
        else if(decLoc[i]==5) System.out.print(" ");

        System.out.println(input[i]);
    }

输出:

        Enter three floating point numbers
3265.3265
23.365
254.3256
        Here they are lined up on their decimal point
 3265.3265
   23.365
  254.3256

需要更好的解决方案来对齐不同长度的 float 。

最佳答案

为了使其灵活,您只需在已有的代码中添加几行代码即可。 首先,我们来看看点之前最长的数字是多少:

int LENGTH = 3;
int longestCountBeforeDecimalPoint = 0;

for (int i=0; i<LENGTH; i++) {
    int indexOfDot = input[i].indexOf(".");

    if (longestCountBeforeDecimalPoint < indexOfDot) {
        longestCountBeforeDecimalPoint = indexOfDot;
    }
}

然后,不要使用“if”条件,而是添加此行,它将利用您之前找到的小数点位置,基本上完成您正在做的事情,但增加了灵 active :

for (int j=0; j<longestCountBeforeDecimalPoint - decLoc[i] + 1; j++) {
    System.out.print(" ");
}

完整代码:

Scanner in = new Scanner(System.in);

int LENGTH = 3;
String[] input = new String[LENGTH];
System.out.format("%n%n\tEnter three floating point numbers%n");
for (int i=0; i<LENGTH; i++)
    input[i] = in.nextLine();

//finds the longest number of digits before the dot
int longestCountBeforeDecimalPoint = 0;

for (int i=0; i<LENGTH; i++) {
    int indexOfDot = input[i].indexOf(".");

    if (longestCountBeforeDecimalPoint < indexOfDot) {
        longestCountBeforeDecimalPoint = indexOfDot;
    }
}

System.out.format("\tHere they are lined up on their decimal point%n");

/*This is used to find decimal location in each string*/
int[] decLoc = new int[LENGTH];
for (int i=0; i<LENGTH; i++)
{
    //as R.J noted below, finding dot place can be done like this
    decLoc[i] = input[i].indexOf('.');
}

/*print 5 spaces before number if the decimal is at place 0, 4 spaces for 1...*/ 
for(int i=0; i<LENGTH; i++)
{
    //add spaces
    for (int j=0; j<longestCountBeforeDecimalPoint - decLoc[i] + 1; j++) {
        System.out.print(" ");
    }

    System.out.println(input[i]);
}

在输出中,您将按照要求将所有数字在点位置对齐。

使用随机生成的 10000 个数字进行测试,所有数字都在点位置对齐。

LENGTH 指定用户将输入的数字数量。当然,这也可以做得更灵活,比如输入某些特殊字符时终止输入数字等。

关于java - 在小数点处对齐 float 列 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858024/

相关文章:

java - JEdi​​torPane 未检测到 <a> 标签,而是将它们放入我的字符串中

java int的最大值

c++ - 将数字与按位运算符相乘时的错误

css - 在多行上下文中具有响应式等宽的元素组,最后一行问题

alignment - 嘈杂文本语料库中的句子分割和对齐

Java Swing 为基本音乐播放器绘制 PlayButton

java - java swing JList 中的快捷方式

ios - Swift:将 String 转换为 Float 并在进行一些数学运算后再次返回 String

c - 什么时候下溢?

CSS:让 child (img)在背景(图片)上正确对齐