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!
Can you use any kind of loop? A for loop is better for this example.
The problem just says loop, so probably!
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);
}
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.)
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++;
}
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...
You can use concatenation, ie the + sign. But remember print(1 + 2) will output 3 not 12.Or you can use printf method.
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);
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.
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 :(
> 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.
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);
I was going to help but I'm a Stars fan.
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.
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.
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++;
}
}
}