Describe what problem occurs in the following code What modi

Describe what problem occurs in the following code. What modifications should be made to it to eliminate the problem?

Code:

double[] prices = {2.25, 3.12, 6.55, 7.14, 910};

for (int count = 1; count < prices.length; count++)

{

System.out.println(prices[count]);

Solution

the problem with above code is you are printing the array content starting from index 1 not from index 0.

Due to which you will not print the first value in array.

hence the output for the above is :

3.12

6.55

7.14

910.0

but to include the first element also a little modification is needed

instead for starting count from 1 start it from 0.

plus include the closed brace so that for loop does not result in an error.

below is the modified code.

double[] prices = {2.25, 3.12, 6.55, 7.14, 910};
for (int count = 0; count < prices.length; count++)
{
System.out.println(prices[count]);
}

Describe what problem occurs in the following code. What modifications should be made to it to eliminate the problem? Code: double[] prices = {2.25, 3.12, 6.55,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site