[英]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'.在它说“你输入”的地方,我必须输入有多少整数,例如“3”,然后将整数加在一起,例如“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.维护一个
List<Integer>
并在用户每次输入整数时添加到此列表中。 The number of integers added will therefore simply be list.size()
.因此,添加的整数数量将只是
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
方法将为您提供更大的灵活性,这是某事作为程序员,您应该牢记这一点。
List<Integer> inputs = new ArrayList<Integer>();
while (scan.hasNextInt()) {
...
inputs.add(scan.nextInt());
}
...
Just keep a variable named count
and a variable named sum
.只需保留一个名为
count
的变量和一个名为sum
的变量。 And change your code in the while
loop to:并将
while
循环中的代码更改为:
int value = scan.nextInt();
sum += value;
count++;
In the end you can output both after the while
loop ends.最后,您可以在
while
循环结束后输出两者。
By the way you don't need to put those curly braces { } after scan.next()
;顺便说一句,您不需要在
scan.next()
之后放置那些花括号 { } ; They're unrelated, and will always be executed independently of scan.next()
;它们是不相关的,并且总是独立于
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.有一个 count 变量,在
main
的开头声明并递增它。
you can also mantain a sum variable in the same way.您还可以以相同的方式维护 sum 变量。
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.您也不需要最后一次调用
scan.next()
或将最后一次println
调用包含在单独的块中。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.