Java 当输入以空格分隔,以字母结尾时,如何计算数字的数量?

Java 当输入以空格分隔,以字母结尾时,如何计算数字的数量?,java,input,while-loop,java.util.scanner,counting,Java,Input,While Loop,Java.util.scanner,Counting,我对java很陌生。 我正在为一个项目尝试一些东西,但我不明白为什么这不起作用。 这里的目标是让用户输入以空格分隔的数字,并以字母结尾。然后程序需要计算偶数和奇数索引数,并输出较大的和 当给定的数字量为常量时,我已经成功地实现了这一点,但现在我想使其适应用户输入。 因为我想把数字放在一个数组中,所以我需要知道这个数组的长度。为了得到这个结果,我想计算用户输入的数字量,以便创建适当的长度数组 由于某些原因,while循环没有结束并保持运行。我如何计算输入的数字量 编辑 我添加了.next();在第

我对java很陌生。 我正在为一个项目尝试一些东西,但我不明白为什么这不起作用。 这里的目标是让用户输入以空格分隔的数字,并以字母结尾。然后程序需要计算偶数和奇数索引数,并输出较大的和

当给定的数字量为常量时,我已经成功地实现了这一点,但现在我想使其适应用户输入。 因为我想把数字放在一个数组中,所以我需要知道这个数组的长度。为了得到这个结果,我想计算用户输入的数字量,以便创建适当的长度数组

由于某些原因,while循环没有结束并保持运行。我如何计算输入的数字量

编辑

我添加了.next();在第一个while循环中,这样它就不会卡在第一个输入元素上。然而,这又给我带来了另一个问题,即有两个while循环试图通过相同的输入进行循环。我尝试创建第二个扫描仪并重置第一个扫描仪,但它无法使第二个循环从第一个元素开始。以前的答案表明这是不可能的,有没有一种方法可以在仍然使用数组存储值的情况下将其放入一个while循环中

另外,输入值应该可以是任何正整数或负整数

这是我的完整代码:

import java.util.Scanner;

public class LargerArraySum {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int length = 0;
    System.out.println("Enter your numbers seperated by spaces, end with a letter");

    while(in.hasNextInt()) {
        length++;
        in.next();
    }
    System.out.println(length);

    int arra[] = new int[length];

    while(in.hasNextInt()) {
        for(int i=0;i<length;i++) {
            int x = in.nextInt();
            arra[i] = x;
        }
    }
    int evenSum = EvenArraySum(arra);
    int oddSum = OddArraySum(arra);

    if(evenSum<oddSum) {
        System.out.println("The sum of the odd indexed elements is bigger");
    } else if(oddSum<evenSum) {
        System.out.println("The sum of the even indexed elements is bigger");
    } else {
        System.out.println("The sum of the odd and even indexed elements is equal");
    }

}
public static int EvenArraySum(int[] a) {
    int sum = 0;
    for(int i=1;i<a.length;i+=2) {
        sum += a[i];
    }
    System.out.println("The sum of the even indexed elements is: " + sum);
    return sum;
}
public static int OddArraySum(int[] a) {
    int sum = 0;
    for(int i=0;i<a.length;i+=2) {
        sum += a[i];
    }
    System.out.println("The sum of the odd indexed elements is: " + sum);
    return sum;
}
}
import java.util.Scanner;
公营类大黄{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
整数长度=0;
System.out.println(“输入以空格分隔的数字,以字母结尾”);
while(在.hasNextInt()中){
长度++;
in.next();
}
系统输出打印长度;
int arra[]=新的int[长度];
while(在.hasNextInt()中){
循环中的for(int i=0;iadd-in.next();实际上,您不需要数组。您可以在读取时对索引的偶数和奇数求和而不保存它们。

1)您的第一个while循环不起作用,因为迭代器总是从同一位置检查更多的数字

例如:

Position     0 1 2 3 4 5
Value        1 3 5 7 9 0
在开始时,迭代器指向位置0。如果调用
hasNextInt()
,它将检查位置1是否可用,在这种情况下,它将为真。此时,迭代器仍然指向位置0。因此,您增加
长度
,然后再次执行相同的操作,因此您有一个无限循环

要将迭代器移动到下一个位置,需要调用
nextInt()

2) 您不能以这种方式使用第二个while循环对同一个
扫描仪进行迭代。如果您先更正while循环,则迭代器将指向位置5(它到达扫描仪的末尾)。因此,对
hasnetint()
的检查将为false,并且不会输入第二个while循环

3) 注释已经提到了它,您可以使用
ArrayList
来处理这个用例,如下所示:

    final ArrayList<Integer> input = new ArrayList<>();
    while ( in.hasNextInt() ) {
        input.add( in.nextInt() );
    }
    System.out.println( input.size() );
final ArrayList input=new ArrayList();
while(在.hasNextInt()中){
input.add(in.nextInt());
}
System.out.println(input.size());

(或者像kitxuli在回答中提到的那样,不要存储值,只需在第一个while循环中计算它们即可)

您的代码有两个主要问题。第一个和第二个while循环让我们看看您的第一个循环

while(in.hasNextInt()) {
    length++;
}
您在.hasNextInt()中的条件
使您插入输入,因为没有使用.nextInt
中的
初始化变量,但也会返回[true]或[false],只要它为true,它将添加到长度变量中,而不提示您插入[new input]。因此代码应该如下所示

 Int length = 0;
 int k ;
     while(in.hasNextInt()) {
       length++ ;
       k = in.nextInt();
     }
将输入插入到初始化变量k for ex中,然后在添加到[length]后提示用户进一步输入k,然后循环将检查您的条件,而不提示用户输入

让我们看看你的第二个while循环

 while(in.hasNextInt()) {
    for(int i=0;i<length;i++) {
        int x = in.nextInt();
        arra[i] = x;
    }
}
您必须添加if语句,因为如果在int数组中获得字母,则会出现异常错误。数组a[i]不会提示用户

当然,让用户输入两次值是不切实际的,因此在不使用ArrayList类的情况下实现更好的代码,我想您可能不太清楚的是,使用空字符串

新代码:-

String g = "";
String j ="";
int y ;
int q=0;
int w = 0;
while (in.hasNextInt()) 
    {
   y = in.nextInt();
   g =g+y+",";
   q++;

}
int arra [] = new int [q];

   for(int r =0;r<g.length();r++) {

       if(g.charAt(r)==(',')){
          arra[w]=Integer.parseInt(j);
          System.out.println(arra[w]);
          w++;
          j="";
       }else{
     j=j+g.charAt(r); 
       }

}
String g=”“;
字符串j=“”;
int-y;
int q=0;
int w=0;
while(在.hasNextInt()中)
{
y=in.nextInt();
g=g+y+“,”;
q++;
}
int arra[]=新int[q];

对于(int r=0;r which
while
循环继续运行?你有2个。你的while循环没有结束,因为你不消耗下一个int,所以你总是检查同一个数字。我建议对你的情况使用
ArrayList
而不是固定长度的数组。在这个数组中,我提高了长度,你可以完整地读取你的数字作为string并在空格处拆分此字符串,并将数字转换为int。但如上所述,最好使用ArrayList。所有未来开发工作的一般提示:在IDE中调试代码。然后可以逐步运行代码并检查所有内容的值。此外,尝试编写易于阅读和理解的代码。但他希望要像他做其他计算一样保留这些数字,请查看
evenararysum
oddarrarysum
。因此
in.next()
并不能解决他的问题。他只检查两种方法中输入的索引,并创建奇数和偶数之和。他可以在第一个while循环中一起完成这一切(不存储具体的输入编号)。谢谢你的回答,nextInt确实有效,但它确实让我想到了你的第二点。有没有办法让扫描仪从位置1开始?我尝试过重置它或创建一个新的扫描仪,但这似乎不起作用。我还没有使用ArrayList,我首先想知道使用阵列存储是否可以解决这一问题
String g = "";
String j ="";
int y ;
int q=0;
int w = 0;
while (in.hasNextInt()) 
    {
   y = in.nextInt();
   g =g+y+",";
   q++;

}
int arra [] = new int [q];

   for(int r =0;r<g.length();r++) {

       if(g.charAt(r)==(',')){
          arra[w]=Integer.parseInt(j);
          System.out.println(arra[w]);
          w++;
          j="";
       }else{
     j=j+g.charAt(r); 
       }

}
Scanner in = new Scanner (System.in);
String g = "";
String j ="";
int y ;
int q=0;
int i=0;
int w = 0;

System.out.println("inset your input separated by spaces");  
  g = in.nextLine();

while(i<g.length()){
   if((g.charAt(i))==(' ')){
   q++;
}
i++;
}
 int a [] = new int [q+1];

   for(int r =0;r<g.length();r++) {

       if(g.charAt(r)==(' ')){
          a[w]=Integer.parseInt(j);
          System.out.println(a[w]);
         w++;
          j="";
       }else{
     j=j+g.charAt(r); 
       }


   }
    a[w]=Integer.parseInt(j);
System.out.println(a[w]);