Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

For loop

java for loop loop

  • Please log in to reply
8 replies to this topic

#1 UberMeister

UberMeister

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 11 February 2008 - 01:04 PM

and this is how it should look when you compile it.

*****
****
***
**
*

heres my program so far...

public class ForLoop
{
public static void main(String[] args)
{
for (int number = 1; number <= 5; number++)
{
System.out.print("*");
System.out.print("");

for (int umber = 1; umber <= 4; umber++)
{
System.out.print ("");
System.out.print ("*");



}
}
}
}

when i run it this far i get

*************

when it should be
*****

****

at that stage.
i want to know why the 2nd for line isn't going down a space and not the correct number of asteriks aren't going in. thx in advance for help.
  • 0

#2 keller

keller

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 11 February 2008 - 03:53 PM

I think you need to add a line feed at the end of your for loops(inside).
  • 0

#3 Arkie

Arkie

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 90 posts

Posted 13 February 2008 - 06:07 AM

I've created something similiar like that for a student some time ago.

The code to show * or whatever you want is listed below:

#
String input = "Number of rows";
#
input = JOptionPane.showInputDialog(input);
#
System.out.println(input);
#

#
String inputChar = "Character to be printed out..";
#
inputChar = JOptionPane.showInputDialog(inputChar);
#
System.out.println("Rows: " + input + "Chars: " + inputChar);
#
System.out.println("Generating output..");
#
//for(;;) .. figure something on your own..
#

#
int printRows = Integer.parseInt(input);
#
String extra = inputChar;
#
txtArea.setText("Generated output..\n");
#
for (int i = 0; i < printRows; i++){
#

#
txtArea.append(inputChar + "\n");
#
inputChar = inputChar + extra;
#
System.out.println(inputChar);
#
}


output:

10
Rows: 10Chars: a
Generating output..
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaa


for more info and complete code: Program Menu

//edit
oh man, my code is all messed up!
  • 0

#4 UberMeister

UberMeister

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 14 February 2008 - 10:58 PM

is their not a simpler way to do that....? i can ge the first 5 asteriks that i need printed fine but after that its tricky. and i can only use system.out.print once.

this is the code my brother sent me thats not exactly in java form

public class test
{
public static void main(String[] args)
{
int count = 5

for (int i = 1,5,i++) {
for (int j 5,1, j--) {
System.out.print(*);
}
System.out.println();
}


anyone think they understand what he intended to write in java format?
  • 0

#5 Arkie

Arkie

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 90 posts

Posted 15 February 2008 - 09:37 AM

Well, i suppose i can give you the link where I coded a similiar application but i usually let students think for themselfs. Trust me, it helps you in the long run.

Stars
  • 0

#6 UberMeister

UberMeister

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 15 February 2008 - 09:51 AM

public class ForLoop
{
public static void main(String[] args)
{
int i=0;
int j=5;
for (i=0;i<5; i++) {
for (j=5;j>0; j--) {

System.out.print("*");
}

System.out.println();
}
}
}


heres what i have. problem is it doesn't descend like this
*****
****
***
**
*
what needs to be adjusted to do just that?
  • 0

#7 Netsheik

Netsheik

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 17 February 2008 - 01:19 PM

what needs to be adjusted to do just that?


Just initialize j to 5-i in the second for and it will run perfectly

Netsheik
  • 0

#8 kmhosny

kmhosny

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 123 posts

Posted 22 February 2008 - 01:04 PM

try to put "System.out.println(String arg);" at the outer loop and see what happens

to make the number of * minimize each time make a new variable and initialize it with 5 and minimize it in the outer loop
for e.g
int k=5;
//outter loop{
//inner loop{}
//new line
k--;
//}
  • 0

#9 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 02 March 2008 - 02:16 AM

in place of j>0 in the inner loop put j>i.thats all.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download