你好,斯塔克弗勒社区.

我正在编写一个Java程序,它使用数组和12个整数输入,然后找出偶数、赔率和负数,并将它们分成3个不同的array.

该程序看起来似乎应该按预期进行编译和运行,但是它在增强的for循环中给出了一个错误,说是variable i is already defined in method main(java.lang.String[])

我已经看过这篇文章了,它对我的for-loop问题没有帮助.

节目内容如下:

public static void main(String[] args){
Scanner in = new Scanner(System.in);
      
int [] twelveInt = new int [12];
      
int countEven = 0;
      
int countOdd = 0;
      
int countNeg = 0;
      
int i = 0;
      
for (i = 0; i < twelveInt.length; i++) {
    System.out.println("Enter the #" + (i + 1) + " integer.");
    twelveInt [i] = in.nextInt();
  
    if (twelveInt[i] % 2 == 0){
        countEven++;
    }
    if (twelveInt[i] % 2 != 0){
        countOdd++;
    }
    if (twelveInt[i] < 0){
        countNeg++;
    }          
}

    
int [] evens = new int [countEven];    
int [] odds = new int [countOdd];
int [] negatives = new int [countNeg];
    

countEven = 0;
countOdd = 0;
countNeg = 0;
    

for (int i : twelveInt) {
        if (i % 2 == 0){
            evens[countEven++] = i;
        }
        if (i % 2 != 0){
            odds[countOdd++] = i;
        }
        if (i < 0){
            negatives[countNeg++] = i;
        }          
    }
          
System.out.println("Here are the Even numbers you entered");
System.out.println(Arrays.toString(evens));
      
System.out.println("Here are the Odd numbers you entered");
System.out.println(Arrays.toString(odds));
      
System.out.println("Here are the Negative numbers you entered");
System.out.println(Arrays.toString(negatives));

对于这个奇怪的错误,我们非常感谢您的任何帮助.

推荐答案

int i = 0;
      
for (i = 0; i < twelveInt.length; i++) {

将其更改为:

for (int i = 0; i < twelveInt.length; i++) {

Java相关问答推荐

Selenium Java:无法访问IFRAME内部的元素

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

Select 按位运算序列

在spring—data中自动发现native—sql查询期间遇到重复的SQL别名[id]

@org.springframework.beans.factory.annotation.Autowired(required=true)-注入点有以下注释:-SpringBoot

流迭代列表<;对象>;上的NoSuchElementException

我需要生成一个文件来整合每个特性执行的所有JSON结果

在Java中,如何按一个属性升序,然后按另一个属性降序对对象列表进行排序?

暂停计时器

Spring-Boot Kafka应用程序到GraalVM本机映像-找不到org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier

如何在JavaFX循环中完美地制作一个AudioClip/MediaPlayer?

如何配置空手道以使用FeignClient或RestTemplate代替ApacheHttpClient

将双倍转换为百分比

如何在IntelliJ IDEA的Build.sbt中添加外部JAR文件?

在整数列表中查找和可被第三个整数整除的对时出现无法解释的RunTimeError

未调用OnBackPressedCallback-Activitiy立即终止

为什么Instant没有从UTC转换为PostgreSQL的时区?

当我将鼠标悬停在javafxTextArea上时,如何更改鼠标光标?

java中的网上购物车解析错误

为什么Java编译器为没有参数的方法(getter方法)创建桥接方法