Java Programming Tutorial 28 - Nested for Loops (Triangles and Pyramids)

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

I have done a nested loop like this by learning from you:

for (int i = 1; i < 10; i++) {
for (int k = 1; k < 10; k++) {
System.out.print(k + " ");
if(k >= i) {

}
}
System.out.println();
}
learning a lot with your courses, thanks Caleb :)

eduardomarinho
Автор

i searched " how is k better than j" and this vid showed up.

manishbr
Автор

Been tryin to figure out nested loops for a minute now, you earned a subscribe.

matthewsnyder
Автор

I'm relearning all of this from almost 15 years ago at this point and the triangle challenge was a fun little lab to solve after the video was done.

TheBliza
Автор

K is better than j because when debugging it's easier to notice the difference between i and k than i and j, trust me, an engineer that is going through the tutorial to learn a different language.

healer
Автор

Like i said the jokes are becoming darker💀

emmanuellahai_
Автор

So if I'm seeing it correctly, the numbers are printed in horizontal direction and than the next line comes right?

jimvers
Автор

For the triangle thing:

Scanner sc = new Scanner(System.in);
System.out.println("How many rows?");
double num = sc.nextInt()/2f, x = (num*2)%2 == 0 ? 0 : -1;

for(int i = 1; i<=num+0.5;i++){
for(int j = 0; j<i; j++){
System.out.print("¤");
}
System.out.print("\n");
}

for(int i = (int) ((num*2)+x)/2; i>0; i--){
for(int j = 0; j<i; j++){
System.out.print("¤");
}
System.out.print("\n");
}

It works, however, I am unsure as to what I could improve as it is not something super long but it feels kinda clunky and as I have 2 separate for loops here it feels as if I could do it in less lines and more effectively

yayl
Автор

Day 2 🙌 Done 28 out of 100, 72 more to go let's go! 🔥

hihellookay
Автор

Is there anyone explaining the concept of 3 nested for loops??

johnieboy
Автор

Hey Caleb, I have a question regarding the while loop. I mean why can't we use the while loop for this type of question?

Sasaki_Kenchiro
Автор

Any reason why your for loops count backwards rather than forwards?

Shaun-EZX
Автор

If you're looking for this shape:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Just change a few things:
- change the k >= 0; to k <= 9;
- change k-- to k++
- put a "*" in the " " space
- remove the k + and leave the "*" alone
That's all.

FULL CODE BELOW

for(int i = 9; i >= 0; i--){
for(int k = i; k <= 9; k++){
System.out.print("* ");
}
System.out.println();
}

manuelvillagomez
Автор

The j messes me up too, and I think it's because it looks so much like the i in the editing font, it can be hard to keep things straight. K all the way!

nicolestrahm
Автор

idk man i dont like j or k! i wouldve used c for clare

letztaco
Автор

Plot twist: he has actually put his life for this 💀

emmanuellahai_
Автор

i need one that constantly start from 6 5 4 3 2 1 then\n and 6 5 4 3 2 and so on idk howw

changchiensin
Автор

Hullo,
what is the problem with this code?
public class Main {
public static void main(String[] args) {
for(int i = 1; i < 11; i++){
for(int k = 1; k < 11; k++); {
System.out.println(k);
}
System.out.println();
}
}
}
It apparently is unable to resolve 'k' to a symbol. I tried making 'k' a variable outside of the two forloops and inside of the main method, and the error that resulted was 'Not a statement'.
This could be happening because I'm using the IntelliJ IDE. I used Eclipse for a while, but it stopped working for no reason.

JohnDoe-umgt
Автор

try printing i instead of k and you'll get something interesting

NinguFromVN
Автор

"mom, mom, i broke my phone'
mom-"how?'
me- by smashing that like button

joselinherrera
visit shbcf.ru