Results 1 to 13 of 13
- 09-07-2011, 02:42 PM #1
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Help (Java Eclipse loop using inputted number)
I've tried EVERYTHING THAT I KNOW! I was suppose to make a code that will create a Triangle depending on your inputted number, for example if you put 5 it will show:
*
**
***
****
***** - 5
******
*****
****
***
**
*
or 8:
*
**
***
****
*****
******
*******
******** - 8
*********
********
*******
******
*****
****
***
**
*
I don't know but as you see on the output I showed you, there's wrong with the middle right, when that was supposed to be the number you putted:
*
**
***
****
***** - 5
****
***
**
*
But anyways, please show me how its done! And by the way the output I showed was really supposed to be an opposite direction, you can see it in my code when you run it.
import java.util.*;
public class Loopcrap
{
public static void main(String [] args)
{
int ctr1, ctr2, ctr3;
for(ctr1=0;ctr1<=5;ctr1++){
for(ctr3=4;ctr3>=ctr1;ctr3--){
System.out.print(" ");
}
for(ctr2=0;ctr2<=ctr1;ctr2++){
System.out.print("*");
}
System.out.println("");
}
for(ctr1=4;ctr1>=0;ctr1--){
for(ctr3=4; ctr3>=ctr1; ctr3--){
System.out.print(" ");
}
for(ctr2=0;ctr2<=ctr1;ctr2++){
System.out.print("*");
}
System.out.println("");
}
}
}Last edited by micolord; 09-07-2011 at 03:09 PM.
-
Re: Help (Java Eclipse loop using inputted number)
That's not how it works here as this is not a service to provide you with your homework solutions.
I suggest that you break down the big task into small tasks and try to solve each small task one at a time. If you get stuck at a step, come back with your code and your question.
By the way, your problem has nothing to do with Eclipse and all to do with Java. I'm moving this to the New To Java section.
- 09-07-2011, 02:48 PM #3
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
- 09-07-2011, 02:54 PM #4
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Help (Java Eclipse loop using inputted number)
Here's the code:
import java.util.*;
public class Loopcrapp
{
public static void main(String [] args)
{
int ctr1, ctr2, ctr3;
Scanner Input = new Scanner(System.in);
for(ctr1 = Input.nextInt();ctr1<=5;ctr1++){
for(ctr3=4;ctr3>=ctr1;ctr3--){
System.out.print(" ");
}
for(ctr2=0;ctr2<=ctr1;ctr2++){
System.out.print("*");
}
System.out.println("");
}
}
}
It inputs a five *****, which was supposed to be:
*
**
***
****
*****
Any idea please?
-
Re: Help (Java Eclipse loop using inputted number)
You'll want to nest a for loop inside of another for loop. If you figure out the logic on paper, you'll be able to figure the logic in Java. Keep experimenting with your loops and keep testing your code as that's the fun of programming -- watching the fruits of your logic take shape before your eyes.
- 09-07-2011, 03:02 PM #6
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
-
Re: Help (Java Eclipse loop using inputted number)
- 09-07-2011, 03:10 PM #8
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Help (Java Eclipse loop using inputted number)
"use two for loops, one nested inside of the other."
ARRRRRRRRRRRRRRRRRRRRRRRRRRRRRRGH.Last edited by micolord; 09-07-2011 at 03:14 PM.
-
Re: Help (Java Eclipse loop using inputted number)
U.s.e.
T.w.o.
F.o.r.
L.o.o.p.s.,
O.n.e
N.e.s.t.e.d.
I.n.
T.h.e.
O.t.h.e.r.
Again, just try, try, try. You don't know how lucky you are to have your very own computer lab on your desktop, one that will allow you to experiment and play with code to your heart's content. Why when I was a youngster learning to program...Last edited by Fubarable; 09-07-2011 at 03:13 PM.
- 09-07-2011, 03:16 PM #10
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Help (Java Eclipse loop using inputted number)
Last edited by micolord; 09-07-2011 at 03:20 PM.
- 09-07-2011, 03:45 PM #11
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Help (Java Eclipse loop using inputted number)
I come up with this:
import java.util.*;
public class Loopcrapp
{
public static void main(String [] args)
{
double ctr1, ctr2, ctr3;
Scanner Input = new Scanner(System.in);
ctr1 = Input.nextIn();
for(ctr1<=5;ctr1++){
for(ctr3=4;ctr3>=ctr1;ctr3--){
System.out.print(" ");
}
for(ctr2=0;ctr2<=ctr1;ctr2++){
System.out.print("*");
}
System.out.println("");
}
}
}
I'm trying to make an limitation so that it wont end up on a infinite loop but it keeps giving me this error, kindly please tell me what should I do to correct this error:
C:\> Javac Loopcrapp.java
Loopcrapp.Java:9: not a statement
for(ctr1<=50;ctr++){
The one which is being pointed was: "<" of for(ctr1<=50;ctr++){Last edited by micolord; 09-07-2011 at 03:47 PM.
-
Re: Help (Java Eclipse loop using inputted number)
That's not how you make a for loop. Please check out the tutorial here: for loops
- 09-11-2011, 01:23 PM #13
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Help (Java Eclipse loop using inputted number)
Sorry for replying 5 days late, haha XD I forgot to log back! because I forgot the site and collage really keeps me busy, but I've figured out the codes after 2 and a half hours when I log out here.
import java.util.*;
public class Crappyloop
{
public static void main(String [] args)
{
double ctr1,ctr2,ctr3,ctr4;
Scanner Input = new Scanner(System.in);
ctr4 = Input.nextDouble();
for(ctr1=0;ctr1<ctr4;ctr1++){
for(ctr3=ctr4;ctr3>=ctr1;ctr3--){
System.out.print(" ");
}
for(ctr2=0;ctr2<=ctr1;ctr2++){
if(ctr2==0){
System.out.print("*");
}
else{
System.out.print("**");
}
}
System.out.println("");
}
}
}Last edited by micolord; 09-11-2011 at 01:24 PM. Reason: Forgot to add the "}"
Similar Threads
-
Help (java eclipse loop)
By micolord in forum EclipseReplies: 2Last Post: 09-07-2011, 01:54 PM -
Generate Random Number Loop
By saber210 in forum New To JavaReplies: 9Last Post: 08-12-2011, 04:49 PM -
Loop through all letter and number possibilities?
By Mr.abe90 in forum New To JavaReplies: 11Last Post: 05-29-2011, 07:47 PM -
Java and Eclipse: Adding a build or revision number to application using SVNAnt
By sal_manilla in forum EclipseReplies: 4Last Post: 10-25-2010, 05:59 AM -
Help Creating A Graph From Inputted Data
By adlb1300 in forum New To JavaReplies: 6Last Post: 10-28-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks