Help! Enhanced For loop practice | Sololearn: Learn to code for FREE!
+ 1

# Help! Enhanced For loop practice

P 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 Explanation In this example we have 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). 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 }

21st Mar 2021, 8:01 PM

9 Antworten
+ 4
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 s: sides){ System.out.println(s*s); } } } It works.
24th Jun 2021, 6:07 PM
Okan Hazer
+ 2
Can i ask were is this code from? Seen it twice this week
21st Mar 2021, 10:53 PM
D_Stark
+ 2
D_Stark My guess would be that it is the practice 23.2 Geometry Code from the Java course.
22nd Mar 2021, 1:19 AM
ChaoticDawg
+ 1
Maybe share all of your current code and we can help you further. My guess is that you're missing a closing curly brace }
22nd Mar 2021, 1:30 AM
ChaoticDawg
+ 1
1st May 2021, 8:28 PM
ChaoticDawg
0
for (int side: sides) { ... ... }
21st Mar 2021, 8:08 PM
ChaoticDawg
0
Its from Java course. when i try to run for(int side:sides) it days illegal i dont know why.
22nd Mar 2021, 1:29 AM

0
I'm stuck on this one too still no solution ?
1st May 2021, 8:20 PM
Nicoleta Cazac
0
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 s: sides){ System.out.println(s*s); } } } It works. It worked for me too. But somehow it seems not right. I used the same code on Eclipse and doesn't work the same way. Maybe is the translation to Spanish of what they ask us for.
22nd Oct 2021, 12:35 PM
César Díaz Amat
Heute heiß