I am having trouble printing all the array at the bottom.
Source Code:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of bills: ");
int numbills = x.nextInt();
int myArray[]=new int[numbills];
int sum = 0;
for(int i = 0; i<myArray.length;i )
{
System.out.print("Bill #" (i 1) ": ");
myArray[i]=x.nextInt();
sum = sum myArray[i];
if (i<myArray.length-1)
{
System.out.print(myArray[i]);
System.out.print(" ");
}
}
System.out.print(" = " sum);
}
}
Output:
Enter the number of bills: 2
Bill #1: 100
100 Bill #2: 200
= 300
Possible output:
Enter the number of bills: 2
Bill #1: 100
Bill #2: 200
100 200 = 300
CodePudding user response:
The issue is you are taking input and printing it in the same for-loop.
You can use 2 for-loop(one for input and one for output).
or you can store it in string in same loop and print it after loop ends.
String sumstr = "";
for(int i = 0; i<myArray.length;i )
{
System.out.print("Bill #" (i 1) ": ");
myArray[i]=x.nextInt();
sum = sum myArray[i];
if (i>0)
{
sumstr = sumstr " ";
}
sumstr = sumstr myArray[i];
}
System.out.print(sumstr " = " sum);