我正在尝试将输入的值存储在for-loop结构中,以供以后使用,但它只能在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])
}
}
编辑:是的,我可以在for循环中移动打印输出,但是我需要在显示所有值之前先输入所有值。我需要帮助。
答案 0 :(得分:2)
显示在插入
后,您必须再次阅读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])
}
}
}
}
或者我误解了你的问题?
答案 1 :(得分:2)
希望这会有所帮助。
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]);
}
}
答案 2 :(得分:0)
当您获得下一个标准的输入时,您当前正在执行的操作将覆盖为先前条件输入的值。我相信您希望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();
}
答案 3 :(得分:0)
1)添加
import java.io.*;
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());
}
}
将嵌套的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
您希望同时打印条件和百分比,因为您已初始化大小为5的两个数组,然后您可以直接打印
for (int i = 0; i < 5; i++) {
System.out.println(crits[i] + arrayCrit[i]);
}
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]);
}
}
}
有用的链接
答案 4 :(得分:-1)
你必须在循环外定义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());
}
}