Java 为什么println没有';不显示我的数组 publicstaticvoiddisplayArray(int选项卡[]){ int i; 字符串choix;//选择 System.out.println(“\n要查看从第一个位置或最后一个位置输入的值吗?”); System.out.println(“第一个带“P”,最后带“D”); choix=sc1.nextLine();//sc1用于nextLine,sc用于nextInt以避免缓冲问题。 if(choix==“p”| | choix==“p”) {for(i=0;i
我假设它是用Java编写的。 比较字符串时,请使用Java 为什么println没有';不显示我的数组 publicstaticvoiddisplayArray(int选项卡[]){ int i; 字符串choix;//选择 System.out.println(“\n要查看从第一个位置或最后一个位置输入的值吗?”); System.out.println(“第一个带“P”,最后带“D”); choix=sc1.nextLine();//sc1用于nextLine,sc用于nextInt以避免缓冲问题。 if(choix==“p”| | choix==“p”) {for(i=0;i,java,arrays,println,Java,Arrays,Println,我假设它是用Java编写的。 比较字符串时,请使用 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 firs
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);
}
}
而不是==
它应与以下方面合作:
string.equals(String)
当您比较
对象
s时,==
不仅比较它们的值,还比较给定的对象。例如:
if(choix.equals("p") || choix.equals("P"))
if(choix.equals("D") || choix.equals("d"))
由于equals
比较对象是否相似,=
比较对象是否相同。此外,如果在字符串中使用引号,则需要使用\“
,因为如果只使用“
然后关闭字符串,导致错误。因此,您应该执行以下操作:
String a = "foo";
String b = "foo";
a == b; //false
a.equals(b); //true
publicstaticvoiddisplayArray(int选项卡[]){
int i;
字符串choix;//选择
System.out.println(“\n要查看从第一个位置或最后一个位置输入的值吗?”);
System.out.println(“第一个是“P”,最后一个是“D”);
choix=sc1.nextLine();//sc1用于nextLine,sc用于nextInt以避免缓冲问题。
if(choix.equals(“p”)| | choix.equals(“p”))
{for(i=0;i您的代码不工作,因为您的一个打印错误:System.out.println(“磁带'P'代表第一个,而'D'代表最后一个”);
/k是数组的最大值(最大索引)
…好的,那么您在哪里设置了k
?感谢您的解释,很抱歉我在复制程序时出错了,我键入了““而不是println中的“”,程序现在可以正常工作了,谢谢。”
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);
}
}