Enhanced For Loop - Geometry Code (Practice) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Enhanced For Loop - Geometry Code (Practice)

Your company is writing a program for a geometry course. The program takes the number of squares as the first input, creates an array, and then takes the sides of squares as its elements. Write the part of the program that receives a list of square sides and prints the area of those squares for the user. Sample Input 2 3 4 Output 9 16 2 squares (the first input) and their sides accordingly - 3 and 4 (the second and the third inputs). The area of the first square is 9 (3*3), the second one 16 (4*4).

20th Jun 2021, 7:16 AM
Chin Eu
Chin Eu - avatar
8 ответов
+ 5
Chin Eu [Part - 1] "My answer is just for learning purpose". There is no doubt you have completed your task but that's not actual Enhanced For Loop because there is no values in array. We have just initialised array of size "length" then storing values in array. So if you want to follow "Enhanced For Loop" then code should look like this: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] sides = new int[length]; for (int i = 0; i < length; i++) { sides[i] = scanner.nextInt(); } //your code goes here for (int j : sides) { System.out.println(j * j); } } } .................. continue................
20th Jun 2021, 9:42 AM
A͢J
A͢J - avatar
+ 4
You haven't initialized the value of s and also you have to print the output every time when you inserted the value in s.It would be something like this: https://code.sololearn.com/cfHu4RlfagN3/?ref=app
20th Jun 2021, 7:29 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 3
You have to declare each new variable
20th Jun 2021, 7:30 AM
JaScript
JaScript - avatar
+ 3
for( int i : sides ) Means for each int (represented by variable <i>) in collection <sides>. Here, <i> represents an int (element value) in a collection, it does not represent index of element. So for a collection containing { 5, 4, 3, 2, 1 }, value of <i> will be 5, 4, 3, 2, 1 (elements' values) in each loop round, instead of 0, 1, 2, 3, 4 (elements' index). A regular for-loop is preferable over enhanced for-loop when we need to assign value for elements in a collection. for( int i = 0, val = 0; i < length; i++ ) { val = scanner.nextInt(); sides[ i ] = val * val; System.out.println( sides[ i ] ); }
20th Jun 2021, 9:24 AM
Ipang
+ 2
My code as below: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] sides = new int[length]; for (int i: sides) { sides[i] = scanner.nextInt(); s = sides[i] * sides[i]; } //your code goes here System.out.println(s); } } But it says can’t find symbol. Please help.
20th Jun 2021, 7:17 AM
Chin Eu
Chin Eu - avatar
20th Jun 2021, 8:28 AM
Chin Eu
Chin Eu - avatar
+ 2
Chin Eu [Part - 2] But if you are taking input inside for loop and printing values in same loop then doens't make sense to store values in array. You can directly do like this: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); // int[] sides = new int[length]; for (int i = 0; i < length; I++) { int s = scanner.nextInt(); System.out.println(s * s); } } }
20th Jun 2021, 9:44 AM
A͢J
A͢J - avatar
+ 2
Thanks Ipang and 🅰🅹 🅐🅝🅐🅝🅣 for the explanations
20th Jun 2021, 10:28 PM
Chin Eu
Chin Eu - avatar