반복문 - for, 조건문 - if
public class prac {
public static int countFruit(String fruit) { List<String> fruits = new ArrayList<>(); fruits.add("감"); fruits.add("배"); fruits.add("감"); fruits.add("딸기"); fruits.add("수박"); fruits.add("메론"); fruits.add("수박"); fruits.add("딸기"); fruits.add("메론"); fruits.add("수박"); fruits.add("메론"); fruits.add("수박"); fruits.add("감"); int cnt = 0; for (int i=0; i<fruits.size(); i++) { String f = fruits.get(i); if (fruit == f ) { cnt++; } } return cnt; } public static void main(String[] args) { int gam = countFruit("감"); System.out.println(gam); // 3 } }
public class prac {
public static void main(String[] args) { String title = "웹개발의 봄, Spring"; String tutor = "남병관"; int days = 35; Course course = new Course(title, tutor, days); System.out.println(course.title); System.out.println(course.tutor); System.out.println(course.days); } }
public class Course { public String title; public String tutor; public int days; // 기본 생성자 public Course() { } public Course(String title, String tutor, int days) { this.title = title; this.tutor = tutor; this.days = days; } }
public class prac {
public static void main(String[] args) { String title = "웹개발의 봄, Spring"; String tutor = "남병관"; int days = 35; Course course = new Course(title, tutor, days); course.setTitle(title); course.setTutor(tutor); course.setDays(days); System.out.println(course.getTitle()); System.out.println(course.getTutor()); System.out.println(course.getDays()); }
}
public class Course {
public String title; public String tutor; public int days; // 기본 생성자 public Course() { } public Course(String title, String tutor, int days) { this.title = title; this.tutor = tutor; this.days = days; } // Setter public void setTitle(String title) { this.title = title; } public void setTutor(String tutor) { this.tutor = tutor; } public void setDays(int days) { this.days = days; } // Getter public String getTitle() { return this.title; } public String getTutor() { return this.tutor; } public int getDays() { return this.days; }
}