Rewrite the following code fragment, replacing for loop with equivalent while loop.
public static void main(String[] args)
{
for (String arg : args)
{
System.out.println(arg);
}
}
What I did was as follows:
public static void main(String[] args)
{
while (String arg : args)
{
System.out.println(arg);
}
6 replies to this topic
#1
Posted 08 December 2010 - 02:16 PM
|
|
|
#2
Posted 08 December 2010 - 06:38 PM
need help sooooooon please
#3
Posted 08 December 2010 - 07:50 PM
What you're doing makes no sense first of all. "arg" is not a string so you can't use it as a string.
This is how a loop works.
These two examples do the same thing except one is using a for loop and the other is using a while loop. I hope this helps! If i was helpful please give me kudos!(or w/e its called :c-grin:)
This is how a loop works.
public static void main(String [] args)
{
for(int count = 0; count < 10; count++)
System.out.println("this is a for loop example");
}
public static void main(String [] args)
{
while(int count < 10)
{
System.out.println("this is the same with a while loop");
count++;
}
}
These two examples do the same thing except one is using a for loop and the other is using a while loop. I hope this helps! If i was helpful please give me kudos!(or w/e its called :c-grin:)
#4
Posted 08 December 2010 - 07:52 PM
^^ i meant reputation points. sorry for double post
#5
Posted 08 December 2010 - 07:56 PM
I don't understand your question. A while loop has the increment/decrement inside the loop.
for(int i = 0; i < 10; i++) System.out.println(i);
while(int i < 10)
{
System.out.println(i);
i++;
}
#6
Posted 09 December 2010 - 12:43 AM
Here you go:
int i=0;
while (i<args.length)
System.out.println(args[i++]);
#7
Posted 09 December 2010 - 05:50 AM
Thank you all
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









