+ 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
韩韵萌
韩韵萌 - avatar
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
Okan Hazer - avatar
+ 2
Can i ask were is this code from? Seen it twice this week
21st Mar 2021, 10:53 PM
D_Stark
D_Stark - avatar
+ 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
ChaoticDawg - avatar
+ 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
ChaoticDawg - avatar
+ 1
Nicoleta My suggestion would be that you start your own post for your issue. Be sure to include detailed information on what the language, course, module project, etc and copy your current code to the playground and share a link to it in your post. Be sure to give input and output examples. Your question should be about whatever your problem is, whether there is something your don't understand, there are errors, the output isn't as expected etc. The better the description of the issue(s) the more likely you'll receive a proper response. Remember in the future, don't hijack other users posts for your own questions.
1st May 2021, 8:28 PM
ChaoticDawg
ChaoticDawg - avatar
0
for (int side: sides) { ... ... }
21st Mar 2021, 8:08 PM
ChaoticDawg
ChaoticDawg - avatar
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
韩韵萌
韩韵萌 - avatar
0
I'm stuck on this one too still no solution ?
1st May 2021, 8:20 PM
Nicoleta Cazac
Nicoleta Cazac - avatar
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