![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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 Cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 I want the numbers under the Cube column to print more like they are under the Square column, so they will look more like: Cube 1 8 27 64 128 Beats me why it's doing that. Here is my program: public static void main(String args[]) {
System.out.println("Value Square Cube");
for (int n = 1; n <= 5; n++)
{
System.out.print(" ");
System.out.print(n);
System.out.print(" ");
System.out.print(square(n));
System.out.print(" ");
System.out.print(cube(n));
System.out.println();
}
}
static int square(int x)
{
return x * x;
}
static int cube(int y)
{
return y * y * y;
}
} |
|
|
|
|
|
#2 |
|
Programmer
|
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[])
{
System.out.println("Value\tSquare\tCube");
for (int n = 1; n <= 5; n++)
{
System.out.println( " " + n + "\t" + square(n) + "\t" + cube(n) );
}
}
static int square(int x)
{
return x * x;
}
static int cube(int y)
{
return y * y * y;
}
} |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#4 | |
|
Programmer
|
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>
int Square ( int &num )
{
return num * num;
}
int Cube ( int &num )
{
return num * num * num;
}
int main ( void )
{
std::cout << "Num\tSquare\tCube" << std::endl;
for ( int i = 0; i < 6; ++i )
std::cout << i << "\t" << Square( i ) << "\t" << Cube( i ) << std::endl;
std::cout << std::endl;
return 0;
}Which when run produces: Num Square Cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 Press any key to continue . . . :::::::::::::: Quote:
|
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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 Cube But instead it's looking more like this: Value Square Cube I got to try and fix it so it looks evenly spaced. |
|
|
|
|
|
#6 |
|
Programmer
|
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.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Use formatted output, space it as you like.
public static void main (String[] args)
{
System.out.println("Value Square Cube");
for (int n = 1; n <= 5; n++)
{
System.out.format ("%3d%10d%10d\n", n, n*n, n*n*n);
}
}
Output:
Value Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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?
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
I got it figured out. I did this:
System.out.format("%3d", n);
System.out.format ("%10d", square(n));
System.out.format("%10d", cube(n));
System.out.println()My way is more work, but I really wanted to use the methods. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| advanced user output | l2u | C++ | 6 | May 9th, 2007 8:16 PM |
| Implementation of CRC32 | grimpirate | Other Web Development Languages | 2 | Mar 3rd, 2007 2:13 PM |
| Change the name of output file | jazz | C | 4 | Jun 28th, 2006 2:54 AM |
| Numerical data output to a file | can342man | C++ | 4 | Jan 27th, 2006 4:21 PM |
| It's giving me "Press any key to continue" and no other output.. | Insomniac | C | 15 | Jun 5th, 2005 7:07 AM |