Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:25:38+00:00 2026-06-16T02:25:38+00:00

I’m new to Java and I’m trying to make a program that allows the

  • 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);
    }
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:25:39+00:00Added an answer on June 16, 2026 at 2:25 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.