Java 我的阵营里发生了奇怪的事情
好的,我将展示我的代码,我的输入和输出,非常奇怪的是数组的值似乎从一行到下一行发生了变化Java 我的阵营里发生了奇怪的事情,java,arrays,Java,Arrays,好的,我将展示我的代码,我的输入和输出,非常奇怪的是数组的值似乎从一行到下一行发生了变化 import java.io.*; class chefAndNewRecipe { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader r = new BufferedReader(new FileReader("/ho
import java.io.*;
class chefAndNewRecipe
{
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
BufferedReader r = new BufferedReader(new FileReader("/home/jer/Documents/chef.txt"));
int testCases = Integer.parseInt(r.readLine());
int numGuesses =0 ;
for (int i=0; i<testCases; i++)
{
int ingredients = Integer.parseInt(r.readLine());
String quantity = r.readLine();
String arr[] = quantity.split(" ");
int[] numIngredients = new int[arr.length];
for (int j =0; j< ingredients; j++)
{
String temp = arr[i];
numIngredients[i] = Integer.parseInt(temp);
System.out.println("This is numIngredients index: " + j + " and value " +numIngredients[i]);//print array location and value
}
System.out.println("numIngredients[0]:" + numIngredients[0]); // should be 2 and is
System.out.println("numIngredients[1]:" + numIngredients[1]); // should be 2 and is 0??
for (int k = 0; k< numIngredients.length; k++)
{
if (numIngredients[k] <2)
{
System.out.println("Value of numIngredients[k]: " + numIngredients[k]);// print value of numIngredients
System.out.println("-1");
}
else
{
numGuesses += numIngredients[k];
}
}
System.out.println(numGuesses);
}
}
}
import java.io.*;
chefAndNewRecipe类
{
公共静态void main(字符串[]args)引发IOException
{
//TODO自动生成的方法存根
BufferedReader r=新的BufferedReader(新的文件阅读器(“/home/jer/Documents/chef.txt”);
int testCases=Integer.parseInt(r.readLine());
整数=0;
对于(int i=0;i即使对于循环变量,使用长变量名也很有用-您似乎在使用i
而不是j
:
for (int j = 0; j < arr.length; j++) // <== possibly arr.length is what you need.
{
String temp = arr[j]; // <=== was i, same next line
numIngredients[j] = Integer.parseInt(temp);
System.out.println(
"This is numIngredients index: " + j + //<== j this line
" and value " + numIngredients[j]); // <== again, was using [i]
}
for(int j=0;j