127k views
0 votes
Read integer numVals from input as the number of integers to be read next. Use a loop to read the remaining integers from input. Output all integers on the same line, and surround each integer with curly braces. Lastly, end with a newline.

Ex: If the input is:

2
75 60
then the output is:

{75}{60}

2 Answers

4 votes

Final answer:

To solve this problem, use a loop to read the integers and output them surrounded by curly braces.

Step-by-step explanation:

To solve this problem we need to use a loop to read the integers from the input and then output them with curly braces. First, read the integer numVals from the input to determine how many integers to read. Then, create a loop that iterates numVals times. Inside the loop, read each integer from the input and output it surrounded by curly braces. Finally, end with a newline character.

Here is the code to solve the problem:

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numVals = input.nextInt();

for (int i = 0; i < numVals; i++) {
int value = input.nextInt();
System.out.print("{" + value + "}");
}

System.out.println();
}
}

User Bantmen
by
8.4k points
3 votes

Answer:

Here's the Python code to solve the problem:

num_vals = int(input()) # read the number of integers to be read next

# read the remaining integers from input

integers = []

for i in range(num_vals):

integers.append(int(input()))

# output all integers on the same line, surrounded by curly braces

output = ''

for num in integers:

output += '{' + str(num) + '}'

print(output) # print the output with integers surrounded by curly braces

Step-by-step explanation:

When the input is 2\\75 60\\, the code reads the number of integers to be read next (i.e., 2), and then reads the remaining two integers (i.e., 75 and 60) using a loop. The code then creates a string output by surrounding each integer with curly braces, and finally prints the output with a newline at the end. The output for this input is {75}{60}

User Tony Cheetham
by
8.3k points