import java.util.Scanner;
public class ArrayString
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Please enter the strings");
int a=0;
for(int i=0; i<10; i++)
{
String[]str=new String[i];
}
for(int i=0; i<10; i++)
{
if(*str*.length[i]%2==0)
System.out.println(String.*str*[i]);
}
}
}
str无法解析为变量Java(33554515(str无法解析或不是字段Java(33554502(
import java.util.Scanner;
public class ArrayString
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Please enter the strings");
int a=0;
for(int i=0; i<10; i++) {
String[]str=new String[i]; // this only exists within the scope of this loop
}
for(int i=0; i<10; i++) {
if(*str*.length[i]%2==0)
System.out.println(String.*str*[i]);
}
}
}
首先,将数组声明为循环之外。
import java.util.Scanner;
public class ArrayString
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Please enter the strings");
int a=0;
String[] str = null;
for(int i=0; i<10; i++) {
str=new String[i];
}
for(int i=0; i<10; i++) {
if(str.length[i]%2==0)
System.out.println(String.*str*[i]);
}
}
}
这将同时编译和运行,但您将只有最后一个数组,因为每次都覆盖现有的数组。
编辑:我只是指范围问题,因为这是你列出的错误。我将改进下面的"固定"版本。
import java.util.Scanner;
public class ArrayString
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Please enter the strings");
int a=0;
String[] str = new String[10];
for(int i=0; i<10; i++) {
str[i] = "" + i; // actually add a value
}
for(int i=0; i<10; i++) {
if(i%2==0) // divide the index, not the value, since those are Strings and you're trying to access them wrong
System.out.println(str[i]);
}
}
}
您可以做的是创建一个包含数组的列表或数组,并对其进行迭代。