[local]1[/local]两个数同名咯,改一下就行,代码如下
import java.util.Scanner;
class Student{
public String [] name = new String[5];
public void addName(String n){
for(int i=0; i < name.length; i++){//错
if(name[i] == null) //错
name[i] = n; //错
break;
}
}
public void showName(){
for(int i=0; i<name.length; i++)
System.out.print(name[i]+" ");
}
}
public class App5_7{
public static void main(String[] args){
Student st = new Student();
Scanner input = new Scanner(System.in);
for(int i=0; i<5; i++){
System.out.println("请输入学生姓名");
String newName = input.next();
st.addName(newName);
}
st.showName();
}
}
import java.util.Scanner;
class Student{
public String [] name = new String[5];
public void addName(String n){
for(int i=0; i < name.length; i++){//错
if(name[i] == null) //错
name[i] = n; //错
break;
}
}
public void showName(){
for(int i=0; i<name.length; i++)
System.out.print(name[i]+" ");
}
}
public class App5_7{
public static void main(String[] args){
Student st = new Student();
Scanner input = new Scanner(System.in);
for(int i=0; i<5; i++){
System.out.println("请输入学生姓名");
String newName = input.next();
st.addName(newName);
}
st.showName();
}
}