为什么以下打印老板而不是低音?
String boss = "boss";
char[] array = boss.toCharArray();
for(char c : array)
{
if (c== 'o')
c = 'a';
}
System.out.println(new String(array)); //How come this does NOT print out bass?It prints boss.
Run Code Online (Sandbox Code Playgroud) 为什么认为不起作用?它只是打印零.然而,当我使用索引值为"i"的普通for循环并在循环体内使用"a [i]"时,它可以工作.
问题不在于打印循环,因为它不会打印值,即使是正常的for循环也是如此.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[] a = new int[5];
for (int i : a)
{
System.out.println("Enter number : ");
i=s.nextInt();
}
System.out.println("\nThe numbers you entered are : \n");
for (int i : a)
{
System.out.println(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)