为什么 println 不显示我的数组
Why println doesn't show my array
public static void displayArray(int tab[]){
int i;
String choix; // choice
System.out.println("\n you want to see the values you entred from first position or last ?");
System.out.println("tape "P" for first , and"D" for last);
choix=sc1.nextLine(); // sc1 for nextLine , sc for nextInt to avoid buffering problems .
if(choix=="p"||choix=="P")
{ for(i=0;i<k;i++) //k is the maximum of the array(max index)
System.out.println("T["+i+"]= "+tab[i]); // why this instruction doesn't work ??
}
if(choix=="D"||choix=="d")
{for(i=k-1;i>=0;i--)
System.out.println("T["+i+"]= "+tab[i]);// this one too doesn't work
}}
public static void main(String[] args) {
// TODO Auto-generated method;stub
int tab[]=new int[4];
System.out.println(readIntArray(tab));
displayArray(tab);
}
}
我不明白为什么 displayArray
不起作用,System.out.println
应该在检查条件后打印我的数组,但它不起作用。
我假设它在 Java 中。
比较字符串时使用
string.equals(String)
而不是==
它应该适用于:
if(choix.equals("p") || choix.equals("P"))
if(choix.equals("D") || choix.equals("d"))
当您比较 Object
时,==
不仅比较它们的值,还会比较给定的对象。例如:
String a = "foo";
String b = "foo";
a == b; //false
a.equals(b); //true
因为equals
比较的是对象是否相似,所以==
比较的是对象是否相同。此外,如果您在 String
中使用引号,则需要使用 \"
对它们进行转义,因为如果您只使用 "
,那么您将关闭 String
,结果在错误中。所以,你应该这样做:
public static void displayArray(int tab[]){
int i;
String choix; // choice
System.out.println("\n you want to see the values you entred from first position or last ?");
System.out.println("tape \"P\" for first , and\"D\" for last);
choix=sc1.nextLine(); // sc1 for nextLine , sc for nextInt to avoid buffering problems .
if(choix.equals("p")||choix.equals("P"))
{ for(i=0;i<k;i++) //k is the maximum of the array(max index)
System.out.println("T["+i+"]= "+tab[i]); // why this instruction doesn't work ??
//Your code did not even reach this point due to using unescape quotes inside a String and incorrect comparisons in your if
}
if(choix.equals("D")||choix.equals("d"))
{for(i=k-1;i>=0;i--)
System.out.println("T["+i+"]= "+tab[i]);// this one too doesn't work
//The reason is the very same as above
}}
public static void main(String[] args) {
// TODO Auto-generated method;stub
int tab[]=new int[4];
System.out.println(readIntArray(tab));
displayArray(tab);
}
}
此外,您需要构建代码,因为它很难阅读。
public static void displayArray(int tab[]){
int i;
String choix; // choice
System.out.println("\n you want to see the values you entred from first position or last ?");
System.out.println("tape "P" for first , and"D" for last);
choix=sc1.nextLine(); // sc1 for nextLine , sc for nextInt to avoid buffering problems .
if(choix=="p"||choix=="P")
{ for(i=0;i<k;i++) //k is the maximum of the array(max index)
System.out.println("T["+i+"]= "+tab[i]); // why this instruction doesn't work ??
}
if(choix=="D"||choix=="d")
{for(i=k-1;i>=0;i--)
System.out.println("T["+i+"]= "+tab[i]);// this one too doesn't work
}}
public static void main(String[] args) {
// TODO Auto-generated method;stub
int tab[]=new int[4];
System.out.println(readIntArray(tab));
displayArray(tab);
}
}
我不明白为什么 displayArray
不起作用,System.out.println
应该在检查条件后打印我的数组,但它不起作用。
我假设它在 Java 中。 比较字符串时使用
string.equals(String)
而不是==
它应该适用于:
if(choix.equals("p") || choix.equals("P"))
if(choix.equals("D") || choix.equals("d"))
当您比较 Object
时,==
不仅比较它们的值,还会比较给定的对象。例如:
String a = "foo";
String b = "foo";
a == b; //false
a.equals(b); //true
因为equals
比较的是对象是否相似,所以==
比较的是对象是否相同。此外,如果您在 String
中使用引号,则需要使用 \"
对它们进行转义,因为如果您只使用 "
,那么您将关闭 String
,结果在错误中。所以,你应该这样做:
public static void displayArray(int tab[]){
int i;
String choix; // choice
System.out.println("\n you want to see the values you entred from first position or last ?");
System.out.println("tape \"P\" for first , and\"D\" for last);
choix=sc1.nextLine(); // sc1 for nextLine , sc for nextInt to avoid buffering problems .
if(choix.equals("p")||choix.equals("P"))
{ for(i=0;i<k;i++) //k is the maximum of the array(max index)
System.out.println("T["+i+"]= "+tab[i]); // why this instruction doesn't work ??
//Your code did not even reach this point due to using unescape quotes inside a String and incorrect comparisons in your if
}
if(choix.equals("D")||choix.equals("d"))
{for(i=k-1;i>=0;i--)
System.out.println("T["+i+"]= "+tab[i]);// this one too doesn't work
//The reason is the very same as above
}}
public static void main(String[] args) {
// TODO Auto-generated method;stub
int tab[]=new int[4];
System.out.println(readIntArray(tab));
displayArray(tab);
}
}
此外,您需要构建代码,因为它很难阅读。