每行仅打印10个输出

时间:2017-03-03 01:51:38

标签: java format printf output

我有一个Printf格式问题。我要打印10个数字,然后再转到下一行并再打印10个数字,依此类推。最终目标就像一张桌子,所有列都排成一行并与右边对齐。我也在使用while语句。我尝试过在研究中发现的一些不同的东西,但没有成功。除Printf之外,我会使用不同的print语句吗?如Print或PrintLn?还考虑过使用If语句。任何帮助将不胜感激!谢谢。

    System.out.printf("Please enter a maximun integer value: ");
    Scanner scan = new Scanner(System.in);
    double n = scan.nextDouble();

    System.out.printf("The number you entered was: %.0f \n", n); // Just to check if user input is correct

    double startNum = 0;
    double sqrt = startNum;

    System.out.printf("Squares less than %.0f are: ", n);
    while ( sqrt < n) {
        sqrt = Math.pow(startNum, 2);
        System.out.printf("%6.0f", sqrt);
        startNum ++;
    }

2 个答案:

答案 0 :(得分:1)

使用MOD条件,您可以确保每行10个输出。

import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        System.out.printf("Please enter a maximun integer value: ");
        Scanner scan = new Scanner(System.in);
        double n = scan.nextDouble();

        System.out.printf("The number you entered was: %.0f \n", n); // Just to check if user input is correct

        double startNum = 0;
        double sqrt = startNum;

        System.out.printf("Squares less than %.0f are: ", n);
        while (sqrt < n) {
            sqrt = Math.pow(startNum, 2);
            if(startNum != 0 && startNum % 10 == 0) {
                System.out.println();
            }
            System.out.printf("%6.0f", sqrt);
            startNum++;
        }
    }
}
  

输出 -

     

请输入最大整数值:150

     

您输入的数字是:150

     

小于150的正方形是:0 1 4 9 16 25 36 49 64 81

     

121 144 169

答案 1 :(得分:0)

if(deviceIsMobile) {
    jQuery('#PickupTime').attr('type','date');
} else {
     jQuery('#PickupTime').datetimepicker(dateTimeOptions);
}