1)接java练习 四、判断语句 switch……case 地址:http://blog.csdn.net/u013871927/article/details/70156369
2)for循环语法讲解地址:http://blog.csdn.net/u013871927/article/details/70052950
3)在src目录下创建Student.java文件
4)打开Student.java文件,在文件中编辑以下代码:
public class Student{
private String name; //姓名
private int age; //年龄
private int sex; //性别
private int score; //分数
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public int getSex(){
return sex;
}
public void setSex(int sex){
this.sex=sex;
}
public int getScore(){
return score;
}
public void setScore(int score){
this.score = score;
}
public Student(){
}
public Student(String name,int age,int sex.int score){
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",性别:"+this.sex+",分数:"+this.score;
}
}
5)在src目录下创建Constant.java文件
6)打开Constant.java文件,在文件中编辑以下代码:
public class Constant{
//男
public static final int MAN = 1;
//女
public static final int WOMAN = 2;
}
7)在src目录下创建ForPractice.java文件
8)打开ForPractice.java文件,在文件中编辑以下代码:
import java.util.List;
public class ForPractice{
private TernaryPractice ternary = new TernaryPractice();
public void practice(List
if(students!=null && !students.isEmpty()){
System.out.println("for循环开始");
for(int i=0;i
System.out.println(students.get(i).toString()+","+ternary.practice(students.get(i).getScore()));
}
System.out.println("for循环结束");
System.out.println("增强for循环开始");
for(Student student : students){
System.out.println(student.toString()+","+practice(student.getScore()));
}
System.out.println("增强for循环结束");
}
}
}
9)打开StartMain.java文件,在里面添加以下代码:
List
students.add(new Student("小明",12,Constant.MAN,65));
students.add(new Student("小强",12,Constant.MAN,75));
students.add(new Student("小刚",12,Constant.MAN,85));
students.add(new Student("小红",12,Constant.WOMAN,95));
students.add(new Student("小兰",12,Constant.WOMAN,55));
students.add(new Student("小芳",12,Constant.WOMAN,100));
//循环语句,for循环练习
ForPractice forPractice = new ForPractice();
forPractice.practice(students);
10)运行cmd打开命令行,在命令行进入到src文件夹,然后执行命令javac -d ..\bin *.java
11)在命令行进入到bin文件夹,然后执行命令java StartMain,得到以下结果即为正确:
代码我已经上传到csdn上,下载地址:http://download.csdn.net/download/u013871927/9813006