![]() |
My output does not look right
I've got this program(I wrote it in C++ now transferring it to Java) that has three columns. Under the first column is a number(1, 2, 3, 4, 5) and under the second column is the square, and under the third column is the cube. But for some reason, the numbers under the Cube column don't look right. This is what the output looks like:
:
Value Square CubeI want the numbers under the Cube column to print more like they are under the Square column, so they will look more like: :
CubeBeats me why it's doing that. Here is my program: :
public static void main(String args[]) { |
because of the space seperators... i would use a \t instead of " ". Once the squares hit 2 digits, it makes another space in the line of output.
Heres what could pose a better solution :
public static void main(String args[]) |
I'll try your suggestion and hopefully it will work. But it doesn't explain why when we get to the Cube column, specifically when the number is 64 and then 125 that the output shifts over to the right even more. There are some numbers under the Square column that also have two digits in them, yet they print just fine.
|
Heres a quick C++ version of your code as i don;t have a Java SDK installed. The \t character can be used in either language.
:
#include <iostream>Which when run produces: :
Num Square Cube:::::::::::::: Quote:
|
Well here is what I did. I wanted the numbers to print under the centers of the words Value, Square, and Cube, instead of at the left:
:
System.out.println( " " + n + "\t" + " " + square(n) + "\t" + " " + cube(n) );But when I look at the headings Value, Square, and Cube, the word Cube is spaced closer to the word Square, than Square is to Value. I don't know why that is. I want it to look evenly spaced between the words: :
Value Square CubeBut instead it's looking more like this: :
Value Square CubeI got to try and fix it so it looks evenly spaced. |
A quick fix would be add one or two spaces after the second \t... You could use if's and do checks to make it better, but for a minor program, I would use the quick fix.
|
Yeah I guess that would work. Does anyone know why the words are not evenly spaced just by using the "\t" character? Why does the word Cube appear closer to the word Square, than Square does to Value? Seems to me all three words should be evenly spaced.
|
Use formatted output, space it as you like.
:
public static void main (String[] args) |
What if I really like the idea of calling the methods to do the work? Is there a way to combine calling the Square and the Cube method, and still using the format function?
|
I got it figured out. I did this:
:
System.out.format("%3d", n);My way is more work, but I really wanted to use the methods. |
| All times are GMT -5. The time now is 2:29 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC