在控制台Java中计算空间

时间:2015-10-13 23:56:41

标签: java console

使用我的程序,我需要显示所有喜欢的数字,如果数字不满意,则显示“。”。 但是当你显示数字时,它应该是一个正方形的形状。而我的不是。这是我要显示的代码:

int space = 0;
for (int num = beginningNumber; num <= endingNumber; num++) {
    if (r1.happy(num)) {
        System.out.print(num);

        space++;

        if (space == 50) {
            System.out.println();
            space = 0;
        }
    } else {
        System.out.print(".");
        space++;
        if (space == 50) {
            System.out.println();
            space = 0;
        }
    }
}

我的控制台显示:

Enter starting number
1
Enter your ending number
1000
1.....7..10..13.....19...23....28..3132...........44....49.
.................68.70........79..82...86....91..94..97..100
..103.....109...................129130..133.....139...........
................167........176...........188.190.192193.......
..203....208..........219......226...230.....236..239...........
...........262263................280..........291.293.......
301302.......310..313.....319320.....326..329.331......338............
.....356.....362..365.367368.......376..379...383..386....391392....397...
...404....409..............................440.....446....
.............464....469........478........487..490.....496....
...................................536..............
.....556......563.565566..................................
.......608........617....622623........632..635.637638.....644....649.
..653.655656........665.....671.673......680..683..........694.....700
........709......716...................736..739........748..
..........761.763....................784.....790..793.......
.802...806...........818.820............833..836..........847...
.........860..863..........874......881......888..........899.
901..904..907..910.912913.......921.923.......931932....937..940.....946....
.............964.....970..973...............989........998.1000

应该看起来像:

1.....7..10..13.....19...23....28..3132...........44....49.
.................68.70........79..82...86....91..94..97.
..103.....109...................129130..133.....139.....
................167........176...........188.190.192193.
..203....208..........219......226...230.....236..239...
...........262263................280..........291.293...
301302.......310..313.....319320.....326..329.331.......
.....356.....362..365.367368.......376..379...383..386..
...404....409..............................440.....446..
.............464....469........478........487..490.....496
...................................536....................
.....556......563.565566..................................
.......608........617....622623........632..635.637638....
..653.655656........665.....671.673......680..683.........
........709......716...................736..739........748
..........761.763....................784.....790..793.....
.802...806...........818.820............833..836..........
.........860..863..........874......881......888..........
901..904..907..910.912913.......921.923.......931932....937
.............964.....970..973...............989........998.1000

没有像我一样删除数字

2 个答案:

答案 0 :(得分:0)

您可以在第一个循环中使用printf("%4d",num);代替System.out.print(num);。在第二个循环中printf("%4s",".")而不是System.out.print(".");。这将使所有内容对齐,占用4个空格,在您的情况下,这是您的数字的最大长度。如果最大长度增加,您可以增加数量(此时您可能应该使用"%"+maxLength+"d""%"+maxLength+"s")。

d是所有十进制整数的转换器标记,而s是字符串

有关printf如何在java中工作的更多信息,请查看https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

或者,如果您想保留自己的方法,则需要将space++更改为第一个space = space + 1 + (int)(Math.log(num)/Math.log(10)),如果

但是,如果一行中的最后(两个)内容是数字,则您可以保证它在50个字符/空格后结束。

答案 1 :(得分:0)

这就像你要获得的那样接近

产生正好50个字符宽的输出。

Q33114502.java&lt; - 完整的工作解决方案

google.maps.event.addListener(map, "idle", function(){
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center);
}); 

map.setZoom( map.getZoom() - 1 );
map.setZoom( map.getZoom() + 1 ); 

生成以下输出:

DELETE t1 OUTPUT DELETED.*
FROM t1
LEFT JOIN t2 on (t1.t2_id = t2.id)
WHERE t2.id IS NULL OR t2.is_valid = false