java 方阵程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43546325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:31:43  来源:igfitidea点击:

Square Array Program

javaarraysnetbeans

提问by Jess Wittman

Using java, I am supposed to create a program that stores the square of the numbers 0, 1, 2 & 9 in an ArrayListof 10 elements.

使用 java,我应该创建一个程序,将数字 0、1、2 和 9 的平方存储在ArrayList10 个元素中。

I have created part of the code that displays the numbers and its squares but the program goes straight down with all the numbers and does not look organized. Can someone help me write it out like like this instead:

我已经创建了显示数字及其平方的部分代码,但程序直接显示所有数字并且看起来没有条理。有人可以帮我把它写成这样:

number: 0 square: 0

数量:0 方:0

number: 1 square: 1

数量:1 方:1

number: 2 square: 4

数量:2 方:4

Code

代码

public static void main(String[] args) {
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (int value : temp) {
        System.out.println(value);
    }

    for (int i = 0; i < temp.length; i++) {
        temp[i] = (int) Math.pow(temp[i], 2);
    }

    for (int value : temp) {
        System.out.println(value);
    }
}

回答by YCF_L

You need just one loop like this :

你只需要一个这样的循环:

public static void main(String[] args) {
    int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (int i = 0; i < temp.length; i++) {
        System.out.println(temp[i] + "\t" + (int)Math.pow(temp[i], 2));
    }
}

OutPut

输出

0   0
1   1
2   4
3   9
4   16
5   25
6   36
7   49
8   64
9   81


If you want to store your results in an ArrayListyou can use :

如果要将结果存储在ArrayList您可以使用:

List<int[]> array = new ArrayList<>();//create a List which take an array of int

int arr[] = new int[2];//create a temporary array of 2 elements

for (int i = 0; i < temp.length; i++) {
    System.out.println("Number: " + temp[i] + " \tSquare: " + (int) Math.pow(temp[i], 2));
    arr[0] = temp[i];//add your number to your array pos 1
    arr[1] = (int) Math.pow(temp[i], 2);//add the power to the 2ed position
    array.add(arr);//add your array to your list

}

回答by TheTinyCat

Try this :

试试这个 :

public static void main(String[] args) {
  int[] temp = {0, 1,2,3,4,5,6,7,8,9};  

  for (int value : temp) {  
         System.out.println("number : "value);  
  }  

  for (int i = 0; i < temp.length; i++) {  
    temp[i] = (int) Math.pow(temp[i], 2);  
  }  

  for (int value : temp) {  
    System.out.println(" square : " + value + "\n");
  }
}

回答by Hani

public static void main(String[] args) {
   int[] temp = {0, 1,2,3,4,5,6,7,8,9};  

     // here you are printing all your numbers in the array, so the output will be: 
     // 0
     // 1
     // ...
     // 9
     for (int value : temp) {
         System.out.println(value);
     }  


     // after that, you calculate and store them in the same array; so your array now look like this:
     // [0,1,4,9,...,81]
     for (int i = 0; i < temp.length; i++) {
         temp[i] = (int) Math.pow(temp[i], 2);
     }  


     // here you are printing again your array
     // 0
     // 1
     // 4
     // ...
     // 81
     for (int value : temp) {
         System.out.println(value);
     } 
 }  

to get your desired outcome, you have to calculate the number before printing everything... one option will be to create another array

为了获得您想要的结果,您必须在打印所有内容之前计算数字......一个选择是创建另一个数组

int[] square = new int[9];

calculate in the first for loop and save the result in the square array and print them, something like this:

在第一个 for 循环中计算并将结果保存在方形数组中并打印它们,如下所示:

for (int i = 0; i < temp.length; i++) {
    square[i] = (int) Math.pow(temp[i],2);
    System.out.println("number " + temp[i] + " square: " + square[i]);
}

回答by KevinO

Assuming the original question which referred to an ArrayListwas correct (the OP's example only had an array), and assuming the results are supposed to be stored in the array (per the question), then the following will work:

假设引用 an 的原始问题ArrayList是正确的(OP的示例只有一个数组),并假设结果应该存储在数组中(根据问题),那么以下内容将起作用:

public static void main(String[] args)
{
  // instantiate ArrayList
  List<Double> array = new ArrayList<>();

  // load the values
  for (int i = 0; i < 10; ++i) {
    array.add(i, Math.pow(i, 2));
  }

  // output
  for (int i = 0; i < array.size(); ++i) {
    System.out.println(String.format("%2d\t%3.0f", i, array.get(i)));
  }
}

Output:

输出:

0     0
1     1
2     4
3     9
4    16
5    25
6    36
7    49
8    64
9    81