简体   繁体   中英

How many integers, addition of integers

import java.util.Scanner;
public class InputLoop
{
    public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer to continue or a non integer to finish");

    while (scan.hasNextInt())
    {
        System.out.println("Enter an integer to continue or a non integer to finish");
        int value = scan.nextInt();
        System.out.print("user: ");
    }
    scan.next();
    {
        System.out.println ("You entered");
        System.out.println ();

    }
}

}

Where it says 'you entered' I have to have how many Integers have been input, for example '3' and then the total of the integers added together for example '56'. I don't know how to do this, how can I implement this?

Maintain a List<Integer> and add to this list every time the user enters an integer. The number of integers added will therefore simply be list.size() . With what you're doing currently, there is no way to access the user's old inputs.

You can alternatively use variables that store the total and the count (which will work fine in this case), but in my opinion using the List approach will give you much greater flexibility if you ever decide to update/revise this code, which is something you should bear in mind as a programmer.

List<Integer> inputs = new ArrayList<Integer>();

while (scan.hasNextInt()) {
    ...
    inputs.add(scan.nextInt());
}

...

Just keep a variable named count and a variable named sum . And change your code in the while loop to:

 int value = scan.nextInt();
 sum += value;
 count++;

In the end you can output both after the while loop ends.

By the way you don't need to put those curly braces { } after scan.next() ; They're unrelated, and will always be executed independently of scan.next() ;

So just change it to:

scan.next();  //I presume you want this to clear the rest of the buffer?
System.out.println("You entered " + count + " numbers");
System.out.println("The total is " + sum);

have a count variable, declared at the beginning of main and increment it.

you can also mantain a sum variable in the same way.

while (scan.hasNextInt())
{
    System.out.println("Enter an integer to continue or a non integer to finish");
    int value = scan.nextInt();
    count++;
    sum += value;
    System.out.print("user: ");
}

scan.next();
{
    System.out.println ("You entered");
    System.out.println (count);
}

For what you want to output, you don't need to keep a history of the user's input. All you need are a running total and a count. You also don't need the last call to scan.next() or to enclose the last println calls in a separate block.

public class InputLoop
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter an integer to continue or a non integer to finish");

        int total = 0;
        int count = 0;
        while (scan.hasNextInt())
        {
            System.out.println("Enter an integer to continue or a non integer to finish");
            int value = scan.nextInt();
            total += value;
            ++count;
            System.out.print("user: ");
        }
        System.out.println ("You entered " + count + " values with a total of " + total);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM