java数组使用iterate求和的问题!
importjava.util.*;classMyFrist1{publicstaticvoidmain(String[]args){Scannerscanner=new...
import java.util.*;
class MyFrist1{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] myArray = new int[10];
System.out.println("Please type ten integers");
for(int i = 0; i < 10; i++){
myArray.add(scanner.nextLine());
}
Iterator<Integer> myIterator = myArray.iterator();
while(myIterator.hasNext()){
int sum = myIterator.next();
sum = sum + myIterator.next();
System.out.println(sum);
}
}
}
目的是想建立一个包含10ints类型的Array,然后从键盘里打10个int类型的字符放入Array,最后通过Iterate方式计算出总和。帮我看看代码哪里错了。谢谢! 展开
class MyFrist1{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] myArray = new int[10];
System.out.println("Please type ten integers");
for(int i = 0; i < 10; i++){
myArray.add(scanner.nextLine());
}
Iterator<Integer> myIterator = myArray.iterator();
while(myIterator.hasNext()){
int sum = myIterator.next();
sum = sum + myIterator.next();
System.out.println(sum);
}
}
}
目的是想建立一个包含10ints类型的Array,然后从键盘里打10个int类型的字符放入Array,最后通过Iterate方式计算出总和。帮我看看代码哪里错了。谢谢! 展开
2个回答
展开全部
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class MyFrist1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list =new ArrayList<Integer>();
System.out.println("Please type ten integers");
for (int i = 0; i < 10; i++) {
list.add(scanner.nextInt());
}
Iterator<Integer> myIterator = list.iterator();
int sum = 0;
while (myIterator.hasNext()) {
int number = myIterator.next();
sum = sum + number;
}
System.out.println(sum);
}
}
首先,数组没有iterator,看你的代码应该用List<Integer>为佳
其次求和的时候sum应该在for循环外面,作用域范围错。
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class MyFrist1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list =new ArrayList<Integer>();
System.out.println("Please type ten integers");
for (int i = 0; i < 10; i++) {
list.add(scanner.nextInt());
}
Iterator<Integer> myIterator = list.iterator();
int sum = 0;
while (myIterator.hasNext()) {
int number = myIterator.next();
sum = sum + number;
}
System.out.println(sum);
}
}
首先,数组没有iterator,看你的代码应该用List<Integer>为佳
其次求和的时候sum应该在for循环外面,作用域范围错。
展开全部
import java.util.*;
class StringIndex {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> myArray = new ArrayList<Integer>();
int tempNum;
int sum ;//变量的声明最好不要放到循环中
System.out.println("Please type ten integers");
for(int i = 0; i < 10; i++){
tempNum= scanner.nextInt();
myArray.add(tempNum);
}
Iterator<Integer> myIterator = myArray.iterator();
while(myIterator.hasNext()){
sum= myIterator.next();
sum = sum + myIterator.next();
System.out.println(sum);
}
}
}
首先:不是任何的容器都能有iterator方法,必须要有一个实现Iterable接口的内部类,然后写了iterator方法 才能用
class StringIndex {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> myArray = new ArrayList<Integer>();
int tempNum;
int sum ;//变量的声明最好不要放到循环中
System.out.println("Please type ten integers");
for(int i = 0; i < 10; i++){
tempNum= scanner.nextInt();
myArray.add(tempNum);
}
Iterator<Integer> myIterator = myArray.iterator();
while(myIterator.hasNext()){
sum= myIterator.next();
sum = sum + myIterator.next();
System.out.println(sum);
}
}
}
首先:不是任何的容器都能有iterator方法,必须要有一个实现Iterable接口的内部类,然后写了iterator方法 才能用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询