编程错误“XXX已在main(String [])中定义”

时间:2022-11-06 19:02:56

My assignment is to Write a program to do the following:

我的任务是编写程序来执行以下操作:

Ask the user to enter a positive integer, n, from 2 to 100.
Declare 3 arrays called x, xsquared, xcubed, where each array can store up to 101 values.

要求用户输入一个正整数n,从2到100.声明3个名为x,xsquared,xcubed的数组,其中每个数组最多可存储101个值。

  • In array x, store the values of i from 1 to n in cells i from 1 to n.
  • 在数组x中,将i的值从1到n存储在单元格i中从1到n。

  • In array xsquared, store the values of i^2 from 1 to n in cells i from 1 to n.
  • 在数组xsquared中,将i ^ 2的值从1到n保存在单元格i中从1到n。

  • In array xcubed, store the values of i^3 from 1 to n in cells i from 1 to n.
  • 在数组xcubed中,将i ^ 3的值从1到n保存在单元格i中从1到n。

Then use a “for” loop to compute the sums of the first n integers, n squared integers, and n cubed integers.

然后使用“for”循环计算前n个整数,n个平方整数和n个立方整数的和。

EG: If user enters n = 5, then 1 + 2 + 3 + 4 + 5 = 15; 1 + 4 + 9 + 16 + 25 = 55;
1 + 8 + 27 + 64 + 125 = 225

EG:如果用户输入n = 5,那么1 + 2 + 3 + 4 + 5 = 15; 1 + 4 + 9 + 16 + 25 = 55; 1 + 8 + 27 + 64 + 125 = 225

Finally, verify that for a given value of n, (n^2 + n + 1 )(n – 1) = n^3 - 1

最后,验证对于给定的n值,(n ^ 2 + n + 1)(n - 1)= n ^ 3 - 1

In my program I seem to be getting an error stating

在我的程序中,我似乎得到一个错误说明

i is already defined in main(String[])

My program does not seem to sum anything or square anything. Why?

我的程序似乎没有任何总结或任何方格。为什么?

import java.util.Scanner;

public class ArraySquareCube
{
  public static void main(String[] args)
  {
    double[] numbers = new double[101];
    double sum = 0;

    System.out.println("Enter a number from 0 through 100");
    Scanner in = new Scanner(System.in);
    int i = in.nextInt();

    for (int i : numbers)
    {
      sum = sum + i;
    }

    for (int i = 0; i < numbers.length; i++)
    {
      numbers[i] = i*i;
    }
  }
}

5 个解决方案

#1


2  

Did you read the error?

你读错了吗?

for (int i : numbers)

At this point, i is already defined in main(String[]). It was defined here:

此时,我已在main(String [])中定义。它在这里定义:

int i = in.nextInt();

If you can get the program to compile, @shantha points out that you're summing the numbers before you've initialized them from 0 to 100.

如果你可以编译程序,@ shantha指出你在将数字从0初始化为100之前对数字进行求和。

Note that 0 * 0 = 0, and 0 + x = x, so as a minor optimisation you can skip 0 and save yourself a double, and that new double[101] call will have a slightly nicer magic number in it.

注意0 * 0 = 0,0 + x = x,因此作为次要优化,你可以跳过0并保存自己的双倍,并且新的双[101]调用将具有更好的幻数。

#2


2  

The error is due to redefining the int i in those for loops. And I would like to point an issue of getting zero for your result after you fix the main issue.

该错误是由于在for循环中重新定义int i。在修复主要问题后,我想指出一个问题,即你的结果为零。

The sum is zero as you sum double array defined in

当你对在中定义的双数组求和时,总和为零

double[] numbers = new double[101];

It is because primitive type double is initialized to zero by default. If you print the each elements value in the array numbers then every elements value should be zero.

这是因为默认情况下原始类型double被初始化为零。如果在数组编号中打印每个元素值,则每个元素值应为零。

By default the elements, you refer numbers[i], are initialized to zero.

默认情况下,您引用数字[i]的元素被初始化为零。

So in the code

所以在代码中

for (int i : numbers)

The i here is zero for every item in numbers array. (as each double primitive initialized default to zero )

数字数组中的每个项目的i都为零。 (因为每个double基元初始化默认为零)

If you initialize the numbers array properly with values other than default value or zero you can see the summation of the array elements.

如果使用默认值或零以外的值正确初始化数字数组,则可以看到数组元素的总和。

Edited: If you change places of your for loop with each other the sum should work. Like :

编辑:如果您相互更改for循环的位置,则总和应该有效。喜欢 :

for (int i = 0; i < numbers.length; i++)
{
   numbers[i] = i*i;
}

for (int i : numbers)
{
    sum = sum + i;
}

#3


0  

You try to use i in for-each loop and in usual for-loop, but it is already defined in

你尝试在for-each循环和通常的for循环中使用i,但它已经定义了

int i = in.nextInt();

int i = in.nextInt();

so compiler argues.

所以编译器认为。

#4


0  

import java.util.Scanner;

public class ArraySquareCube {
public static void main(String[] args){
    double[] numbers = new double[101];
    double sum = 0;
    System.out.println("Enter a number from 0 through 100");
    Scanner in = new Scanner(System.in);
    int i = in.nextInt();
    for (int j = 0; j <= i; j++){
      sum = sum + j;
    }
    for (int c = 0; c < numbers.length; c++){
      numbers[c] = c*c;
    }
  }
}

I believe is what you are looking for.

我相信你正在寻找的东西。

#5


0  

  1. when compiling, there're two errors. First is the

    在编译时,有两个错误。首先是

    for(int i : numbers)
    
    ...
    
    for(int i = 0; ...)
    

    which indicates a duplicate variable of "i".

    表示“i”的重复变量。

    Second is the

    第二是

    for(int i : numbers)
    

    while "numbers" is array of double and "i" is int, which indicates a type mismatch.

    而“numbers”是double的数组,而“i”是int,表示类型不匹配。

    So the code could not even get compiled.

    所以代码甚至无法编译。

2 .You are required to declare 3 arrays. So just declare them. I just see 1 array in your code.

2,你需要申报3个阵列。所以只要声明它们。我只是在你的代码中看到1个数组。

3 .Use the integer that user input. Use another counter to help computing, not "i" itself.

3,使用用户输入的整数。使用另一个计数器来帮助计算,而不是“我”本身。

Try it yourself and come back if you encounter another problem.

如果遇到其他问题,请亲自尝试并回来。

PS: I suggest you use a good IDE such as Eclipse. It indicates you this kind of errors while editing.

PS:我建议你使用一个好的IDE,比如Eclipse。它表示编辑时出现此类错误。

#1


2  

Did you read the error?

你读错了吗?

for (int i : numbers)

At this point, i is already defined in main(String[]). It was defined here:

此时,我已在main(String [])中定义。它在这里定义:

int i = in.nextInt();

If you can get the program to compile, @shantha points out that you're summing the numbers before you've initialized them from 0 to 100.

如果你可以编译程序,@ shantha指出你在将数字从0初始化为100之前对数字进行求和。

Note that 0 * 0 = 0, and 0 + x = x, so as a minor optimisation you can skip 0 and save yourself a double, and that new double[101] call will have a slightly nicer magic number in it.

注意0 * 0 = 0,0 + x = x,因此作为次要优化,你可以跳过0并保存自己的双倍,并且新的双[101]调用将具有更好的幻数。

#2


2  

The error is due to redefining the int i in those for loops. And I would like to point an issue of getting zero for your result after you fix the main issue.

该错误是由于在for循环中重新定义int i。在修复主要问题后,我想指出一个问题,即你的结果为零。

The sum is zero as you sum double array defined in

当你对在中定义的双数组求和时,总和为零

double[] numbers = new double[101];

It is because primitive type double is initialized to zero by default. If you print the each elements value in the array numbers then every elements value should be zero.

这是因为默认情况下原始类型double被初始化为零。如果在数组编号中打印每个元素值,则每个元素值应为零。

By default the elements, you refer numbers[i], are initialized to zero.

默认情况下,您引用数字[i]的元素被初始化为零。

So in the code

所以在代码中

for (int i : numbers)

The i here is zero for every item in numbers array. (as each double primitive initialized default to zero )

数字数组中的每个项目的i都为零。 (因为每个double基元初始化默认为零)

If you initialize the numbers array properly with values other than default value or zero you can see the summation of the array elements.

如果使用默认值或零以外的值正确初始化数字数组,则可以看到数组元素的总和。

Edited: If you change places of your for loop with each other the sum should work. Like :

编辑:如果您相互更改for循环的位置,则总和应该有效。喜欢 :

for (int i = 0; i < numbers.length; i++)
{
   numbers[i] = i*i;
}

for (int i : numbers)
{
    sum = sum + i;
}

#3


0  

You try to use i in for-each loop and in usual for-loop, but it is already defined in

你尝试在for-each循环和通常的for循环中使用i,但它已经定义了

int i = in.nextInt();

int i = in.nextInt();

so compiler argues.

所以编译器认为。

#4


0  

import java.util.Scanner;

public class ArraySquareCube {
public static void main(String[] args){
    double[] numbers = new double[101];
    double sum = 0;
    System.out.println("Enter a number from 0 through 100");
    Scanner in = new Scanner(System.in);
    int i = in.nextInt();
    for (int j = 0; j <= i; j++){
      sum = sum + j;
    }
    for (int c = 0; c < numbers.length; c++){
      numbers[c] = c*c;
    }
  }
}

I believe is what you are looking for.

我相信你正在寻找的东西。

#5


0  

  1. when compiling, there're two errors. First is the

    在编译时,有两个错误。首先是

    for(int i : numbers)
    
    ...
    
    for(int i = 0; ...)
    

    which indicates a duplicate variable of "i".

    表示“i”的重复变量。

    Second is the

    第二是

    for(int i : numbers)
    

    while "numbers" is array of double and "i" is int, which indicates a type mismatch.

    而“numbers”是double的数组,而“i”是int,表示类型不匹配。

    So the code could not even get compiled.

    所以代码甚至无法编译。

2 .You are required to declare 3 arrays. So just declare them. I just see 1 array in your code.

2,你需要申报3个阵列。所以只要声明它们。我只是在你的代码中看到1个数组。

3 .Use the integer that user input. Use another counter to help computing, not "i" itself.

3,使用用户输入的整数。使用另一个计数器来帮助计算,而不是“我”本身。

Try it yourself and come back if you encounter another problem.

如果遇到其他问题,请亲自尝试并回来。

PS: I suggest you use a good IDE such as Eclipse. It indicates you this kind of errors while editing.

PS:我建议你使用一个好的IDE,比如Eclipse。它表示编辑时出现此类错误。