gpt4 book ai didi

java - 如何将循环中的值放入多数组

转载 作者:行者123 更新时间:2023-12-01 13:42:28 25 4
gpt4 key购买 nike

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

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;


}

}
}
}

最佳答案

目前还不清楚您想要做什么,这里是您可能想要做什么的示例。

// 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();
}

关于java - 如何将循环中的值放入多数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com