Java 如何在单个while循环中格式化此文件,使其打印为四列?

Java 如何在单个while循环中格式化此文件,使其打印为四列?,java,Java,我需要把它分成四列 public class Lab7aCelsiustoFahrenheit { public static void main(String[] args) { //variables double orig = -40.0; double cels = orig; double fahr = cels; final int MAX_TEMP = -1;

我需要把它分成四列

 public class Lab7aCelsiustoFahrenheit
    {
       public static void main(String[] args)
       {
        //variables
       double orig = -40.0;
       double cels = orig;
       double fahr = cels;
       final int MAX_TEMP = -1;      
         //loop that prints asingle column on celsius to F conversions
         while(cels <= MAX_TEMP)
         {
            System.out.println(cels + "C " + "is " + fahr + "F" + "\t");
            cels++;
            fahr = (((9.0 * cels) + 160) / 5);

            }

     }    
    }
公共类Lab7aCelsiustoFahrenheit
{
公共静态void main(字符串[]args)
{
//变数
双原点=-40.0;
双细胞=原始细胞;
双fahr=细胞;
最终整数最大温度=-1;
//循环,在摄氏到F的转换中打印单个列

而(cels你可以在4个条目后换行,计算你写了多少条目,然后从下一行开始

此外,为了使每个“单元格”位于另一个单元格的下方,您应该使用
System.out.printf
来编写条目,以格式化
双值

如果您确实希望在单个while循环中执行此操作,则可能是:

int column = 1;

while(cels <= MAX_TEMP) {
    fahr = (((9.0 * cels) + 160) / 5);

    System.out.printf("%8.2fC is %8.2fF" + "\t", cels, fahr);
    cels++;
    column++;

    if(column > 4) {
        column = 1;
        System.out.println();
    }
}
int列=1;
而(cels 4){
列=1;
System.out.println();
}
}

我认为您还应该在System.out.printf(…)之前进行计算

谢谢你的编辑。这是我第一次,格式有点混乱。你说的四个不同的列是什么意思?你到底需要什么作为输出?根据你作业的要求,一种可能是使用两个循环。第一个循环将所有结果放入一个数组中。第二个循环只循环一夸脱r是实际数组长度的倍数。每个循环将从数组的每个四分位数中获取第一个值,并将它们构建到一列中。