172k views
0 votes
What is the problem with the code snippet below? public class Test { public static void main(String[] args) { System.out.println(cost(10, 4)); } public static void cost(int price, int reps) { for (int i = 0; i < reps; i++) { System.out.print(price); } } }

User MkHun
by
5.0k points

1 Answer

4 votes

Answer:

The problem in the following snippet is that the function "cost" returns the void and it cannot used as the expression in the print statement

Step-by-step explanation:

public class Test

{

public static void main(String[] args)

{

System.out.println(cost(10, 4));

}

public static void cost(int price, int reps)

{

for (int i = 0; i < reps; i++)

{

System.out.print(price);

}

}

}

Output:

Main.java:4: error: 'void' type not allowed here

System.out.println(cost(10, 4));

^

1 error

In the following code the void type is not allowed in the function "cost".

User Kwz
by
5.3k points