While Loops

I'm going to start off by mentioning that this is my first java class and I've been having a lot of trouble... Hopefully someone can help...

I have to write an app that uses a loop to print a table (no user input). I start out with:

publicclass Lab6

{

publicstaticvoid main( String args[] )

{

int count = 1;

while ( count <= 5 )

I need to print: 1, then 100, then 1000 Then 2, then 20, 200. All the way up to 5. I don't want the answer, I just need someone to point me in the right direction on what do next. I really appreciate it!

[879 byte] By [iheartnjdevilsa] at [2007-10-3 8:04:26]
# 1
Can you use any kind of loop? A for loop is better for this example.
CaptainMorgan08a at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 2
The problem just says loop, so probably!
iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 3

Do you know how to use for loops? Look at this and see if you can figure out what else you need to do.

for(int i=1; i<=5; i++)

{

System.out.println(i);

}

CaptainMorgan08a at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 4

I was reading about "for". I believe that's for the next chapter :( I will admit that I don't entire understand the ++. All I could get from the chapter was that it means less typing. If i'm so far out there, I understand. If you could tell me what I should be focusing on though, that would be a big help. (The class I am taking is online so everything has been self taught.)

iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 5

The ++ just means increase variable by one. It is equivalent to i = i + 1.

If you haven't done for loops yet then stick with a while loop but remember just about everything done with a while loop can be done with a for loop and vice versa.

int num = 1;

while(num <= 5) {

System.out.println(num);

num++;

}

floundera at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 6

Can I print several numbers on one line? Like

int num = 1;

while(num <= 5) {

System.out.println(num)(num*10)(num*100);

num++;

}

Obviously it doesn't compile...

iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 7
You can use concatenation, ie the + sign. But remember print(1 + 2) will output 3 not 12.Or you can use printf method.
floundera at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 8

It doesn't like the + sign :(

I basically need 3 calulations on the same line.. if i try to seperate them

public class lab6

{

public static void main( String args[] )

{

int num = 1;

while(num <= 5)

{ System.out.println(num);

num++;

}

{ System.out.println(num*10);

num++;

}

}

}

Then it's taking 1, incrementing it up to 5, then adding another increment to make it 6 before it multiples by 10.

If I try to use printf, I get the following compile error:

C:\Documents and Settings\Zoe\Desktop\lab6.java:8: cannot find symbol

symbol : method printf(int)

location: class java.io.PrintStream

{ System.out.printf(num);

iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 9

OK, serveral issues.

{ System.out.println(num*10);

num++;

}

This code is outside your while loop.

System.out.println(num)(num*10)(num*100);

This is close but the println method takes a single parameter ie everything needs to be inside one set of brackets.

You could try and figure out how to do this using above. But I strongly suggest using printf. Go to the PrintStream class in the API and read about how to use printf. Basically it takes a string, followed by a series of values to be inserted into that string. There is no printf(int) method.

floundera at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 10

I can replicate the first part with printf

public class lab6

{

public static void main( String args[] )

{

int num = 1;

while(num <= 5){

System.out.printf("%d\n", num);

num++;

}

}

}

But still can't figure out how to have a calculation on the same line, I've tried several things but it either prints the calculation (for instance, when you run the app, you see 1*10 instead of it actually calculating "num*10".

I'm sorry I'm an idiot :(

iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 11

> I'm sorry I'm an idiot :(

You have to start somewhere. ;-)

Look at how this works.

int i=0;

while(i <= 10)

{

System.out.println("i = "+i);

i++;

}

Hopefully that will help you.

CaptainMorgan08a at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 12
System.out.printf("%d\n", num);OK this is a start but what if we need more than one number? See what the following does and how you can modify it.System.out.printf("%d %d\n", num, num);
floundera at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 13
I was going to help but I'm a Stars fan.
kablaira at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 14

Well, i try

public class lab6

{

public static void main( String args[] )

{

int num = 1;

while(num <= 5){

System.out.printf("%d %d*10\n", num, num);

num++;

}

}

}

But get:

1 1*10

2 2*10

2 3*10

when I need

1 10

2 20

3 30

I know I need something to let it know it's a calculation and not to actually print it but that's where I'm stuck. I can't find anything in my book and every time I google it, it's got crazy code I can't begin to understand. Parathesis don't work either because it still prints them.

iheartnjdevilsa at 2007-7-15 3:08:07 > top of Java-index,Java Essentials,New To Java...
# 15
The first parameter, the String, is what gets printed to screen. All the other parameters get inserted and replace the %d symbols. So you don't put the *10 in the string, you modify the num parameter before it gets inserted into the string.
floundera at 2007-7-21 12:18:01 > top of Java-index,Java Essentials,New To Java...
# 16

HAHAHHAHAHHAHHA

public class lab6

{

public static void main( String args[] )

{

int ones = 1;

while(ones <= 5){

System.out.printf("%d %d %d\n", ones, (ones*10), (ones*100));

ones++;

}

}

}

iheartnjdevilsa at 2007-7-21 12:18:01 > top of Java-index,Java Essentials,New To Java...