Java 数组存储值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19400958/
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
Java Array Storing Values
提问by user2768501
I'm trying to store the values inputted on the for-loop structure that I will need for later use, but it can only be recognized inside the for loop structure. I need the stored values to be recognize for the rest of the program but somehow I'm getting an error "cannot find symbol"
我正在尝试存储在 for 循环结构上输入的值以供以后使用,但它只能在 for 循环结构内识别。我需要为程序的其余部分识别存储的值,但不知何故我收到错误“找不到符号”
public class TryArray {
public static void main(String args[]) throws IOException {
BufferedReader inpt = new BufferedReader(new InputStreamReader(System. in ));
int[] arrayCrit = new int[5];
String[] crits = new String[5];
for (int i = 0; i < crits.length; i++) {
System.out.print("Criteria: ");
crits[i] = inpt.readLine();
for (int j = 0; j < arrayCrit.length; j++) {
System.out.print("Percentage: ");
arrayCrit[j] = Integer.parseInt(inpt.readLine());
}
}
System.out.println(crits[i] + arrayCrit[j])
}
}
Edit: Yes, I can move the print output inside the for-loop, but I need to input all the values first before showing all of it. Please I need help.
编辑:是的,我可以在 for 循环内移动打印输出,但我需要先输入所有值,然后才能显示所有值。请我需要帮助。
采纳答案by Prabhakaran Ramaswamy
Hope this will help.
希望这会有所帮助。
public static void main(String args[]) throws IOException {
BufferedReader inpt = new BufferedReader(new InputStreamReader(
System.in));
int[] arrayCrit = new int[5];
String[] crits = new String[5];
for (int i = 0; i < crits.length; i++) {
System.out.print("Enter Criteria: ");
crits[i] = inpt.readLine();
System.out.print("Enter Percentage: ");
arrayCrit[i] = Integer.parseInt(inpt.readLine());
}
// Printing the values
for (int i = 0; i < crits.length; i++) {
System.out.println("Criteria :" + crits[i] + " Percentage: "
+ arrayCrit[i]);
}
}
回答by X-Pippes
to show all you have to make another loop
to read, after the insertion
显示所有您必须loop
在插入后重新阅读
import java.io.*;
public class TryArray{
public static void main(String args[])throws IOException{
BufferedReader inpt = new BufferedReader (new InputStreamReader(System.in));
int [] arrayCrit = new int [5];
String [] crits = new String [5];
for(int i=0; i<crits.length; i++){
System.out.print("Criteria: ");
crits[i]=inpt.readLine();
for (int j=0; j<arrayCrit.length; j++){
System.out.print("Percentage: ");
arrayCrit[j]=Integer.parseInt(inpt.readLine());
}
}
for(int i=0; i<crits.length; i++){
for (int j=0; j<arrayCrit.length; j++){
System.out.println(crits[i] + arrayCrit[j])
}
}
}
}
or I misunderstood your question?
或者我误解了你的问题?
回答by luanjot
You have to define i and j outside the loop. Otherwise, you can only use them inside the loop:
您必须在循环外定义 i 和 j。否则,您只能在循环内使用它们:
int i;
int j;
int[] arrayCrit = new int[5];
String[] crits = new String[5];
for (i = 0; i < crits.length; i++) {
System.out.print("Criteria: ");
crits[i] = inpt.readLine();
for (j = 0; j < arrayCrit.length; j++) {
System.out.print("Percentage: ");
arrayCrit[j] = Integer.parseInt(inpt.readLine());
}
}
回答by SudoRahul
What you're currently doing will overwrite the values entered for the previous criteria when you get the input for the next. I believe you want to have a 2d
array for arrayCrit
to store all the percentages for each criteria. Only in such a scenario it makes sense to have 2 for
loops.
当您获得下一个条件的输入时,您当前所做的将覆盖为前一个条件输入的值。我相信你想要一个2d
数组arrayCrit
来存储每个标准的所有百分比。只有在这种情况下,有 2 个for
循环才有意义。
String[] crits = new String[5];
int[][] arrayCrit = new int[5][5]; // The 2d array
for (int i = 0; i < crits.length; i++) {
System.out.print("Criteria: ");
crits[i] = inpt.readLine();
for (int j = 0; j < arrayCrit[i].length; j++) {
System.out.print("Percentage: ");
arrayCrit[i][j] = Integer.parseInt(inpt.readLine()); // Inputting different set of percentage for each criteria
}
}
// Displaying each criteria with its own percentages
for (int i = 0; i < crits.length; i++) {
System.out.print("For Criteria: " + crits[i]+" ---> ");
for (int j = 0; j < arrayCrit[i].length; j++) {
System.out.print("Percentage " + j + " : " + arrayCrit[i][j] + "\t");
}
System.out.println();
}
回答by Aniket Kulkarni
1) Add
1) 添加
import java.io.*;
2)You are using nested for loops
2)您正在使用嵌套的 for 循环
for(int i=0; i<crits.length; i++){ //this will execute 5 times
System.out.print("Criteria: ");
crits[i]=inpt.readLine();
for (int j=0; j<arrayCrit.length; j++){ //this will execute 5 times for each i = 25 times
System.out.print("Percentage: ");
arrayCrit[j]=Integer.parseInt(inpt.readLine());
}
}
Remove nested for-loop as
删除嵌套的 for 循环为
for(int i=0; i<crits.length; i++){ //this will execute 5 times
System.out.print("Criteria: ");
crits[i]=inpt.readLine();
}
for (int j=0; j<arrayCrit.length; j++){ //this will execute 5 times
System.out.print("Percentage: ");
arrayCrit[j]=Integer.parseInt(inpt.readLine());
}
3) can not find symbol
You want to print criteria and percentage at same time, as you have initialized both arrays with size 5, then you can directly print
3)can not find symbol
你想同时打印标准和百分比,因为你已经用大小5初始化了两个数组,然后你可以直接打印
for (int i = 0; i < 5; i++) {
System.out.println(crits[i] + arrayCrit[i]);
}
4)You can use following program which is enhanced version of your program
4)您可以使用以下程序,这是您程序的增强版本
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestArray {
public static void main(String args[]) throws IOException {
BufferedReader
inpt = new BufferedReader(new InputStreamReader(System. in ));
System.out.println("How many records?");//ask for how many records
int n = Integer.parseInt(inpt.readLine());// store in n
int[] arrayCrit = new int[n];//create array with size n
String[] crits = new String[n];
//**as you mentioned in edit you want to take all the input before printing**
for (int i = 0; i < n; i++)
{
System.out.print("Criteria: ");
crits[i] = inpt.readLine();
System.out.print("Percentage: ");
arrayCrit[i] = Integer.parseInt(inpt.readLine());
}
//print both arrays
System.out.println("Criteria \t Percentage");
for (int i = 0; i < n; i++)
{
System.out.println(crits[i] + "\t"+arrayCrit[i]);
}
}
}
Useful link
有用的链接