如何将值从循环放到多个数组

时间:2013-12-16 08:51:02

标签: java arrays

我有这样一个循环(这是我的其他代码):

for (int i = 2; i < 257; i = i * 2) {
  V -= i + 0;
  System.out.println("Suma: " +V+ " atemus: "+i);
  ///////////////////
  int X [][] = new int [8][16]; //multi array where i need to put "i" values
}

以及你在代码中看到的有多个数组:

int X [][] = new int [8][16];

如何将i的值放入数组并将其打印到屏幕中?

好吧,我把我的整个代码:

import java.util.Scanner;


public class IV_darbas {
    public static void main(String[] arguments) {

        int r, H, R, V, x, k, z;

        Scanner ivestis = new Scanner(System.in);
        boolean pabaiga = false;
        while (!pabaiga) {
            System.out.println("");
            System.out.println("Si programa leis skaiciuoti turi");
            System.out.println("Jei noresi testi spausk bet kuri klavisa");
            System.out.println("Kai noresi baigti ivesk zodi: pabaiga ");

            String first = "testi";

            // Ivedi "R" reiksme
            Scanner one = new Scanner(System.in);
            System.out.println("Ivesk reiksme: R");
            R = one.nextInt();

            // Ivedi "H" reiksme
            Scanner two = new Scanner(System.in);
            System.out.println("Ivesk reiksme: H");
            H = two.nextInt();

            // Ivedi "R" reiksme
            Scanner three = new Scanner(System.in);
            System.out.println("Ivesk reiksme: r");
            r = three.nextInt();

            V = (int) ((Math.PI * Math.pow(H, 4) * (Math.pow(R, 2)
                    + Math.pow(r, 2) + Math.pow(r, 5) * Math.pow(r, 3))) / 3);

            System.out.println("Tavo gautas turis: " + V);
            z = V * (-1);
            System.out.println("Tavo gautas turis(su priesingu zenklu):" + z);

            // Ivedamas papildomas skaicius
            Scanner four = new Scanner(System.in);
            System.out.println("Ivesk dar viena skaiciu: ");
            x = four.nextInt();
            k = x * z;

            // salygos if sakinys
            System.out.println("Pakeisto ir papildomo skaciaus sandauga: " + k);
            if (k > 0) {
                System.out.println("gautas skaicius " + k + " yra teigiamas");
            } else if (k < 0) {
                System.out.println("gautas skaicius " + k + " yra neigiamas");
            } else if (k == 0) {
                System.out.println("gautas skaicius " + k + " yra neutralus");

            }

            System.out.println("");
            System.out.println("Sumavimas. Prie gauto rezultato: " + V);
            for (int i=2; i<257; i=i*2 ){
                  V += i+0;
                  System.out.println("Suma: " +V+ " pridejus: "+i);

            }



            V = (int) ((Math.PI * Math.pow(H, 4) * (Math.pow(R, 2)
                    + Math.pow(r, 2) + Math.pow(r, 5) * Math.pow(r, 3))) / 3);

            System.out.println("");
            System.out.println("Atimtis. Prie gauto rezultato: " + V);
            for (int i=2; i<257; i=i*2 ){
                  V -= i+0;
                  System.out.println("Suma: " +V+ " atemus: "+i);
                  ///////////////////
                  int X [][] = new int [8][16];


            }


            System.out.println("");
            System.out.println("Ar testi? (n/y)");
            first = ivestis.next();
            if (first.equalsIgnoreCase("Pabaiga")) {
                System.out.println("Darbas baigtas!");
                break;


            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

目前还不清楚你要做什么,这是你可能想要做的一个例子。

// declared before any loop so it is in scope after the loop
int[][] values = new int[8][16];
int i = 1;
for(int x = 0; x < values.length; x++) {
    for(int y = 0; y < values[x].length; y++) {
        values[x][y] = i;
        i += 2;
    }
}
// to print the values
for(int x = 0; x < values.length; x++) {
    for(int y = 0; y < values[x].length; y++) {
        System.out.print(values[x][y] + " ");
    }
    System.out.println();
}

答案 1 :(得分:0)

如果您正在询问如何向数组中添加内容,则可以这样:

X[n] = i; 

其中n是数组中的索引。但正如有人已经说过你需要在循环之外声明你的数组,否则它将无法访问。