0

I'm new to Java and I'm trying to make a program that allows the user to input 100 numbers and if the user writes '0', then the program is suppose to print the smallest, largest, sum and all the numbers. I got all that to work but not to exit and print it all. My teacher said something about using a while loop, but how is that possible when you have a for loop?

Regards

public static void main(String[] args) {
    int[] list = new int[100];
    int min = 0;
    int max = 0;
    int sum = 0;
    boolean first = true;

    Scanner scan = new Scanner(System.in);
    while(list[i] != 0) {
        for (int i = 0; i < list.length; i++) {

            System.out.print("Enter number (0 to exit) " + (1 + i) + ":");
            list[i] = scan.nextInt();
        }

        for (int i = 0; i < list.length; i++) {

            if (first == true) {

            min = list[i];
            first = false;
        }

        if (list[i] < min) {

            min = list[i];
        }

        else if (list[i] > max) {

            max = list[i];
        }

        sum = list[i] + sum;

    }

    if (list[i] == 0) {

    System.out.print("Numbers are: " + list[0] + ", ");

    for (int i = 1; i < list.length; i++)

    System.out.print(list[i] + ", ");
    System.out.println();

    System.out.println("Smallest number is: " + min);
    System.out.println("Largest numeber is: " + min);
    System.out.println("Sum is: " + sum);
    }
    }
}

}
3
  • Well then maybe the for loop is not appropriate ? Either you use a for loop and have an if inside or you have a while loop and have a variable to act as a counter. Commented Dec 15, 2012 at 0:09
  • 1
    A "for loop" is a specialization of a "while" loop (or, for that matter, a "do/while" loop). You can do everything in a while loop that you can in a for loop. You can also nest loops, one inside another. Commented Dec 15, 2012 at 0:10
  • For descends from while, which descends from boolean recursion. Commented Apr 12, 2015 at 15:10

3 Answers 3

2

You only need one while loop to do this and additionally a for loop just to print the array if you want:

Scanner scan = new Scanner(System.in);
int i = 0;
int sum = 0;
int maxValue = Integer.MIN_VALUE;
int[] history = new int[100];
System.out.println("INPUT:");
int option = scan.nextInt();
while (option != 0 && i <= 100)
{
    if (option > maxValue)
        maxValue=option;
    sum += option;
    history[i] = option;
    option = scan.nextInt();
    i++;
}
System.out.println("OUTPUT: \n" + "SUM: " + sum + "\n MAX VALUE: " + maxValue);
for (int x : history)
    System.out.print(x + "");
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! But how do i get rid of the zeros? The task is make it print "The numbers are: 1, 3, 7"
@user1905426 if you want to get rid of all the zeros i.e. incrementally add to the array, you need to use a dynamic structure like an arrayList.
0

Here's the body of the method which will do what you've been asked. I have not used a while loop (but in fact, a for-loop is a kind of a while-loop internally).

int size = 100; // Set the number of numbers to input.
int[] list = new int[size]; // Create an array with 'size' elements.
int min = Integer.MAX_VALUE; // Set the highest possible integer as start value.
int max = 0; // Set the minimum to zero, assuming that the user won't input negative numbers.
int sum = 0; // Initialize the sum of the numbers in the list.

Scanner scan = new Scanner(System.in);

for (int i = 0; i < size; i++) { // Run 'size' times the process of inputting a number.
    System.out.print("Enter number (0 to exit) " + (i + 1) + ": ");
    int number = scan.nextInt();
    if (number == 0) { // Quit program if input equals '0'
        System.out.println("Exiting...");
        break;
    }
    list[i] = number; // Add the current number to the list
    sum += number; // Add the number to the total
    if (number < min) { // If the number is smaller than the previous one, set this number as the smallest
        min = number;
    }
    if (number > max) { // If the number is greater than the previous smallest number, set this number as the greatest
        max = number;
    }
}

// Output all numbers in the list
for (int i = 0; i < list.length; i++) {
    if (list[i] != 0) {
        System.out.print((i == 0 ? "" : ", ") + list[i]);
    }
}

// You see the snippet (i == 0 ? "" : ", ")
// That is a shorthand if-else statement:
// If i equals 0, then "" (empty string), else ", " (comma and space).
// The statement
//     System.out.print((i == 0 ? "" : ", ") + list[i])
// is the same as
//     if (i == 0) {
//         System.out.println("") + list[i];
//     }
//     else {
//         System.out.println(", ") + list[i];
//     }

System.out.println("Smallest number is: " + min);
System.out.println("Largest numeber is: " + max);
System.out.println("Sum is: " + sum);

4 Comments

Oh thank you. I'm sorry I formulated the task so badly but its not just suppose to exit, it has to print the numbers, smallest biggest etc aswell
Updated. Just use break instead of System.exit(0). Break will exit the current loop and continue execution of the code.
Ops, didnt read it through properly. Thank you very much! You dont happen to now how to get rid of the zeros when you exit?
@user1905426: Please vote up and mark the question as answered if it is the right answer.
0

You have muddled code. Better to use a pattern like this:

while (true) {
    // read next
    if (input == 0) 
        break;
}

2 Comments

He also needs to keep track of the number of entries so far, and stop at 100.
But how im suppose to print out all the numbers with a while loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.