From bbcaea5124a6249bd66dc528b762715bb74d47fe Mon Sep 17 00:00:00 2001 From: ggomabbang Date: Sat, 8 Jul 2023 17:12:57 +0900 Subject: [PATCH 1/3] 34 practice sunny --- .../sunny/Chapter3/ArrayListPractice.java | 94 ++++++++++++ .../sunny/Chapter3/CastingFinalPractice.java | 60 ++++++++ .../Practice/sunny/Chapter3/EnumPractice.java | 39 +++++ .../Practice/sunny/Chapter3/MapPractice.java | 35 +++++ .../sunny/Chapter3/NumBoolPractice.java | 72 ++++++++++ .../Practice/sunny/Chapter3/SetPractice.java | 51 +++++++ .../sunny/Chapter3/StringPractice.java | 134 ++++++++++++++++++ .../Practice/sunny/Chapter4/ForPractice.java | 59 ++++++++ .../Practice/sunny/Chapter4/IfPractice.java | 68 +++++++++ .../sunny/Chapter4/SwitchCasePractice.java | 52 +++++++ .../sunny/Chapter4/WhilePractice.java | 48 +++++++ src/main/java/Save/Chapter3/Q03.java | 2 + 12 files changed, 714 insertions(+) create mode 100644 src/main/java/Practice/sunny/Chapter3/ArrayListPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/CastingFinalPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/EnumPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/MapPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/NumBoolPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/SetPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter3/StringPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter4/ForPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter4/IfPractice.java create mode 100644 src/main/java/Practice/sunny/Chapter4/SwitchCasePractice.java create mode 100644 src/main/java/Practice/sunny/Chapter4/WhilePractice.java diff --git a/src/main/java/Practice/sunny/Chapter3/ArrayListPractice.java b/src/main/java/Practice/sunny/Chapter3/ArrayListPractice.java new file mode 100644 index 0000000..b94ed41 --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/ArrayListPractice.java @@ -0,0 +1,94 @@ +package Practice.sunny.Chapter3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; + +public class ArrayListPractice { + + public static void main(String[] args) { + + // Array + int[] odds = {1, 3, 5, 7, 9}; + String[] weeks = {"월", "화", "수", "목", "금", "토", "일"}; + + // String[] weeks = new String[]; -> compile error + String[] weeks2 = new String[7]; + weeks2[0] = "월"; + weeks2[1] = "화"; + weeks2[2] = "수"; + weeks2[3] = "목"; + weeks2[4] = "금"; + weeks2[5] = "토"; + weeks2[6] = "일"; + + // indexing + System.out.println(weeks[3]); + + // length of array + for (int i = 0; i < weeks.length; i++) { + System.out.println(weeks[i]); + } + + // ArrayIndexOutOfBoundsException + // System.out.println(weeks[7]); + + // ArrayList + ArrayList pitches = new ArrayList(); + + // add + pitches.add("138"); + pitches.add("129"); + pitches.add("142"); + pitches.add(0, "133"); + pitches.add(2, "133"); + + // get + System.out.println(pitches.get(1)); + + // size + System.out.println(pitches.size()); + + // contains + System.out.println(pitches.contains("142")); + + // remove + System.out.println(pitches.remove("129")); + System.out.println(pitches.remove(0)); + + // Generics + // ArrayList pitches2 = new ArraList(); + ArrayList pitches2 = new ArrayList<>(); + + ArrayList pitches3 = new ArrayList(); + pitches3.add("138"); + pitches3.add("129"); + String one = (String) pitches3.get(0); + String two = (String) pitches3.get(1); + + ArrayList pitches4 = new ArrayList<>(); + pitches4.add("138"); + pitches4.add("129"); + String three = pitches4.get(0); + String four = pitches4.get(1); + + // asList + String[] data = {"138", "129", "142"}; + ArrayList pitches5 = new ArrayList<>(Arrays.asList(data)); + System.out.println(pitches5); + + ArrayList pitches6 = new ArrayList<>(Arrays.asList("138", "129", "142")); + System.out.println(pitches6); + + // String.join + String result = String.join(",", pitches6); + System.out.println(result); + + String result2 = String.join(",", data); + System.out.println(result2); + + // sort + pitches6.sort(Comparator.naturalOrder()); + System.out.println(pitches6); + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/CastingFinalPractice.java b/src/main/java/Practice/sunny/Chapter3/CastingFinalPractice.java new file mode 100644 index 0000000..c3efdba --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/CastingFinalPractice.java @@ -0,0 +1,60 @@ +package Practice.sunny.Chapter3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CastingFinalPractice { + + public static void main(String[] args) { + + // String to Integer + String num1 = "123"; + int n1 = Integer.parseInt(num1); + System.out.println(n1); + + // Integer to String + int n2 = 123; + String num2 = "" + n2; + System.out.println(num2); + + int n3 = 123; + String num3 = String.valueOf(n3); + String num4 = Integer.toString(n3); + System.out.println(num3); + System.out.println(num4); + + // String to Float + String num5 = "123.456"; + double d = Double.parseDouble(num5); + System.out.println(d); + + // Integer to Float + int n4 = 123; + double d1 = n4; + System.out.println(d1); + + // Float to Integer + double d2 = 123.456; + int n5 = (int) d2; + System.out.println(n5); + + // NumberFormatException Error + /* + String num = "123.456; + int n = Integer.parseInt(num); + */ + + // Final + final int n6 = 123; + // n6 = 456; -> Compile Error + + // Final List + final ArrayList a = new ArrayList<>(Arrays.asList("a", "b")); + // a = new ArrayList<>(Arrays.asList("c", "d")); -> Compile Error + + // List.of + final List b = List.of("a", "b"); + a.add("c"); + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/EnumPractice.java b/src/main/java/Practice/sunny/Chapter3/EnumPractice.java new file mode 100644 index 0000000..c330a0c --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/EnumPractice.java @@ -0,0 +1,39 @@ +package Practice.sunny.Chapter3; + +public class EnumPractice { + + enum CoffeeType { + AMERICANO, + ICE_AMERICANO, + CAFE_LATE + } + + ; + + public static void main(String[] args) { + + // print + System.out.println(CoffeeType.AMERICANO); + System.out.println(CoffeeType.ICE_AMERICANO); + System.out.println(CoffeeType.CAFE_LATE); + + // for loop + for (CoffeeType type : CoffeeType.values()) { + System.out.println(type); + } + + // Why use Enum + /* + int countSellCoffee(int type){ + ... + } + int americano = countSellCoffee(1); + int result = countSellCoffee(99); -> error + + int countSellCoffee(CoffeeType type){ + ... + } + int americano = countSellCoffee(CoffeeType.AMERICANO); + */ + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/MapPractice.java b/src/main/java/Practice/sunny/Chapter3/MapPractice.java new file mode 100644 index 0000000..8aa708d --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/MapPractice.java @@ -0,0 +1,35 @@ +package Practice.sunny.Chapter3; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class MapPractice { + + public static void main(String[] args) { + + // HashMap + // put + HashMap map = new HashMap<>(); + map.put("people", "사람"); + map.put("baseball", "야구"); + + // get + System.out.println(map.get("people")); + System.out.println(map.get("java")); + System.out.println(map.getOrDefault("java", "자바")); + + // containsKey + System.out.println(map.containsKey("people")); + + // remove + System.out.println(map.remove("people")); + + // size + System.out.println(map.size()); + + // keySet + System.out.println(map.keySet()); + List keyList = new ArrayList<>(map.keySet()); + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/NumBoolPractice.java b/src/main/java/Practice/sunny/Chapter3/NumBoolPractice.java new file mode 100644 index 0000000..cdab0af --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/NumBoolPractice.java @@ -0,0 +1,72 @@ +package Practice.sunny.Chapter3; + +import java.nio.file.FileSystemNotFoundException; + +public class NumBoolPractice { + + public static void main(String[] args) { + + // Integer + int age = 10; + long countOfstar = 8764827384923849L; + + // Float + float pi = 3.14F; + double morePi = 3.14159265358979323846; + double d1 = 123.4; + double d2 = 1.234e2; + + // Oct, Hex + int octal = 023; // Decimal : 19 + int hex = 0xC; // Decimal : 12 + + // Add, Subtract, Multiply, Divide + int a = 10; + int b = 5; + System.out.println(a + b); // print 15 + System.out.println(a - b); // print 5 + System.out.println(a * b); // print 50 + System.out.println(a / b); // print 2 + System.out.println(7 % 3); // print 1 + System.out.println(3 % 7); // print 3 + + // Increment, Decrement + int i = 1; + int j = 10; + i++; + j--; + System.out.println(i); // print 1 + System.out.println(j); // print 9 + + i = 0; + System.out.println(i++); // print 0 + System.out.println(i); // print 1 + + i = 0; + System.out.println(++i); // print 1 + System.out.println(i); // print 1 + + // Boolean + boolean isSuccess = true; + boolean isTest = false; + + // Boolean operation + // 2 > 1 -> true + // 1 == 2 -> false + // 3 % 2 == 1 -> true + // "3".equals("2") -> false + + // Conditional + int base = 180; + int height = 185; + boolean isTall = height > base; + + if (isTall) { + System.out.println("키가 큽니다."); // print 키가 큽니다. + } + + i = 3; + boolean isOdd = i % 2 == 1; + System.out.println(isOdd); // print true + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/SetPractice.java b/src/main/java/Practice/sunny/Chapter3/SetPractice.java new file mode 100644 index 0000000..1c6ccf3 --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/SetPractice.java @@ -0,0 +1,51 @@ +package Practice.sunny.Chapter3; + +import java.util.Arrays; +import java.util.HashSet; + +public class SetPractice { + + public static void main(String[] args) { + + // HashSet + HashSet set = new HashSet<>(Arrays.asList("H", "e", "l", "l", "o")); + System.out.println(set); + + HashSet s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6)); + HashSet s2 = new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9)); + + // Intersection + HashSet intersection = new HashSet<>(s1); + intersection.retainAll(s2); + System.out.println(intersection); + + // Union + HashSet union = new HashSet<>(s1); + union.addAll(s2); + System.out.println(union); + + // Relative Complement + HashSet subtract = new HashSet<>(s1); + subtract.removeAll(s2); + System.out.println(subtract); + + // Add + HashSet set2 = new HashSet<>(); + set2.add("Jump"); + set2.add("to"); + set2.add("Java"); + System.out.println(set2); + + // AddAll + HashSet set3 = new HashSet<>(); + set3.add("Jump"); + set3.addAll(Arrays.asList("To", "Java")); + System.out.println(set3); + + // Remove + HashSet set4 = new HashSet<>(); + set4.remove("To"); + System.out.println(set4); + + } +} diff --git a/src/main/java/Practice/sunny/Chapter3/StringPractice.java b/src/main/java/Practice/sunny/Chapter3/StringPractice.java new file mode 100644 index 0000000..013b3d8 --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter3/StringPractice.java @@ -0,0 +1,134 @@ +package Practice.sunny.Chapter3; + +public class StringPractice { + + public static void main(String[] args) { + + // char + char a1 = 'a'; + char a2 = 97; + char a3 = '\u0061'; + + System.out.println(a1); + System.out.println(a2); + System.out.println(a3); + + // String + String a = "Happy Java"; + String b = "a"; + String c = "123"; + /* + String a = new String("Happy Java"); + String b = new String("a"); + String c = "123"; + */ + + // Primitive + boolean result = true; + Boolean result2 = true; + + char d = 'A'; + Character d2 = 'A'; + + int i = 100000; + Integer i2 = 100000; + + // String Method + // equals + String e = "hello"; + String f = "java"; + String g = "hello"; + System.out.println(e.equals(f)); + System.out.println(e.equals(g)); + + String h = "hello"; + String j = new String("hello"); + System.out.println(h.equals(j)); // print true + System.out.println(h == j); // print false + + // indexOf + String k = "Hello Java"; + System.out.println(k.indexOf("Java")); // print 6 + + // contains + System.out.println(k.contains("Java")); // print true + + // charAt + System.out.println(k.charAt(6)); // print J + + // replaceAll + System.out.println(k.replaceAll("Java", "World")); // print "Hello World" + + // substring + System.out.println(k.substring(0, 4)); + + // toUpperCase + System.out.println(k.toUpperCase()); + + // split + String l = "a:b:c:d"; + String[] result3 = a.split(":"); // result3 = {"a", "b", "c", "d"} + + // Formatting + System.out.println(String.format("I eat %d apples.", 3)); + + System.out.println(String.format("I eat %s apples.", "five")); + + int number = 3; + System.out.println(String.format("I ate %d apples.", number)); + + number = 10; + String day = "three"; + System.out.println(String.format("I ate %d apples. so I was sick for %s days.", number, day)); + + System.out.println(String.format("I have %s apples", 3)); + System.out.println(String.format("rate is %s", 3.234)); + + System.out.println(String.format("Error is %d%%", 98)); + //System.out.println(String.format("Error is %d%", 98)); + + System.out.println(String.format("%10s", "hi")); + System.out.println(String.format("%-10sjane.", "hi")); + + System.out.println(String.format("%.4f", 3.42134234)); + System.out.println(String.format("%10.4f", 3.42134234)); + + // System.out.printf + System.out.printf("I eat %d apples.", 3); + + // StringBuffer + // append + StringBuffer sb = new StringBuffer(); + sb.append("hello"); + sb.append(" "); + sb.append("jump to java"); + String result4 = sb.toString(); + System.out.println(result4); + + String result5 = ""; + result5 += "hello"; + result5 += " "; + result5 += "jump to java"; + System.out.println(result5); + + // StringBuilder + StringBuilder sbu = new StringBuilder(); + sbu.append("hello"); + sbu.append(" "); + sbu.append("jump to java"); + String result6 = sbu.toString(); + System.out.println(result6); + + // insert + StringBuffer sb1 = new StringBuffer(); + sb1.append("jump to java"); + sb1.insert(0, "hello "); + System.out.println(sb1.toString()); + + // substring + StringBuffer sb2 = new StringBuffer(); + sb2.append("Hello jump to java"); + System.out.println(sb.substring(0, 4)); + + } +} diff --git a/src/main/java/Practice/sunny/Chapter4/ForPractice.java b/src/main/java/Practice/sunny/Chapter4/ForPractice.java new file mode 100644 index 0000000..b2f4ff7 --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter4/ForPractice.java @@ -0,0 +1,59 @@ +package Practice.sunny.Chapter4; + +import java.util.ArrayList; +import java.util.Arrays; + +public class ForPractice { + + public static void main(String[] args) { + + // For Loop + String[] numbers = {"one", "two", "three"}; + for (int i = 0; i < numbers.length; i++) { + System.out.println(numbers[i]); + } + + // For Example + int[] marks = {90, 25, 67, 45, 80}; + for (int i = 0; i < marks.length; i++) { + if (marks[i] >= 60) { + System.out.println((i + 1) + "번 학생은 합격입니다."); + } else { + System.out.println((i + 1) + "번 학생은 불합격입니다."); + } + } + + // Continue For + for (int i = 0; i < marks.length; i++) { + if (marks[i] < 60) { + continue; + } + System.out.println((i + 1) + "번 학생 축하합니다. 합격입니다."); + } + + // Double For + for (int i = 2; i < 10; i++) { + for (int j = 1; j < 10; j++) { + System.out.print(i * j + " "); + } + System.out.println(""); + } + + // For Each + /* + for (type var: iterate){ + body-of-loop + } + */ + String[] numbers2 = {"one", "two", "three"}; + for (String number : numbers2) { + System.out.println(number); + } + + // For Each Arraylist + ArrayList numbers3 = new ArrayList<>(Arrays.asList("one", "two", "three")); + for (String number : numbers3) { + System.out.println(number); + } + } +} diff --git a/src/main/java/Practice/sunny/Chapter4/IfPractice.java b/src/main/java/Practice/sunny/Chapter4/IfPractice.java new file mode 100644 index 0000000..8819343 --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter4/IfPractice.java @@ -0,0 +1,68 @@ +package Practice.sunny.Chapter4; + +import java.util.ArrayList; + +public class IfPractice { + + public static void main(String[] args) { + + // IF Example + boolean money = true; + if (money) { + System.out.println("택시를 타고 가라"); + } else { + System.out.println("걸어가라"); + } + + // Comparison Operator + int x = 3; + int y = 2; + System.out.println(x > y); + System.out.println(x < y); + System.out.println(x == y); + System.out.println(x != y); + + // IF Example with Comparison Operator + int money2 = 2000; + if (money2 >= 3000) { + System.out.println("택시를 타고 가라"); + } else { + System.out.println("걸어가라"); + } + + // And, Or, Not + int money3 = 2000; + boolean hasCard = true; + if (money3 >= 3000 || hasCard) { + System.out.println("택시를 타고 가라"); + } else { + System.out.println("걸어가라"); + } + + // contains + ArrayList pocket = new ArrayList(); + pocket.add("paper"); + pocket.add("handphone"); + pocket.add("money"); + + if (pocket.contains("money")) { + System.out.println("택시를 타고 가라"); + } else { + System.out.println("걸어가라"); + } + + // Else If + boolean hascard2 = true; + ArrayList pocket2 = new ArrayList<>(); + pocket2.add("paper"); + pocket2.add("handphone"); + + if (pocket2.contains("money")) { + System.out.println("택시를 타고 가라"); + } else if (hascard2) { + System.out.println("택시를 타고 가라"); + } else { + System.out.println("걸어가라"); + } + } +} diff --git a/src/main/java/Practice/sunny/Chapter4/SwitchCasePractice.java b/src/main/java/Practice/sunny/Chapter4/SwitchCasePractice.java new file mode 100644 index 0000000..3ce6f5a --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter4/SwitchCasePractice.java @@ -0,0 +1,52 @@ +package Practice.sunny.Chapter4; + +public class SwitchCasePractice { + + public static void main(String[] args) { + // Switch + int month = 8; + String monthString = ""; + switch (month) { + case 1: + monthString = "January"; + break; + case 2: + monthString = "Febuary"; + break; + case 3: + monthString = "March"; + break; + case 4: + monthString = "April"; + break; + case 5: + monthString = "May"; + break; + case 6: + monthString = "June"; + break; + case 7: + monthString = "July"; + break; + case 8: + monthString = "August"; + break; + case 9: + monthString = "September"; + break; + case 10: + monthString = "October"; + break; + case 11: + monthString = "November"; + break; + case 12: + monthString = "December"; + break; + default: + monthString = "Invalid month"; + break; + } + System.out.println(monthString); + } +} diff --git a/src/main/java/Practice/sunny/Chapter4/WhilePractice.java b/src/main/java/Practice/sunny/Chapter4/WhilePractice.java new file mode 100644 index 0000000..7daabcc --- /dev/null +++ b/src/main/java/Practice/sunny/Chapter4/WhilePractice.java @@ -0,0 +1,48 @@ +package Practice.sunny.Chapter4; + +public class WhilePractice { + + public static void main(String[] args) { + + // While + int treeHit = 0; + while (treeHit < 10) { + treeHit++; + System.out.println("나무를 " + treeHit + "번 찍었습니다."); + if (treeHit == 10) { + System.out.println("나무 넘어갑니다."); + } + } + + // Infinity Loop + /* + while(true){ + System.out.println("Ctrl-C를 눌러야 while문을 빠져 나갈 수 있습니다."); + } + */ + + // Break While + int coffee = 10; + int money = 300; + + while (money > 0) { + System.out.println("돈을 받았으니 커피를 줍니다."); + coffee--; + System.out.println("남은 커피의 양은 " + coffee + "입니다."); + if (coffee == 0) { + System.out.println("커피가 다 떨어졌습니다. 판매를 중지합니다."); + break; + } + } + + // Continue While + int a = 0; + while (a < 10) { + a++; + if (a % 2 == 0) { + continue; + } + System.out.println(a); + } + } +} diff --git a/src/main/java/Save/Chapter3/Q03.java b/src/main/java/Save/Chapter3/Q03.java index ce82b4c..b4a450b 100644 --- a/src/main/java/Save/Chapter3/Q03.java +++ b/src/main/java/Save/Chapter3/Q03.java @@ -9,5 +9,7 @@ public static void main(String[] args) { String personNumber = "881120-1068234"; // TODO: 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력하기 + System.out.println(personNumber.substring(0, 5)); + System.out.println(personNumber.substring(7, 13)); } } From 96677cd865b012320713a34f72e865157cfed6e0 Mon Sep 17 00:00:00 2001 From: 02ggang9 <02ggang9@gmail.com> Date: Sat, 8 Jul 2023 18:42:57 +0900 Subject: [PATCH 2/3] problem34-ggang9 --- .../Problem/ggang9/Chapter1/HelloWorld.java | 7 ++++ .../java/Problem/ggang9/Chapter3/Q01.java | 17 ++++++++ .../java/Problem/ggang9/Chapter3/Q02.java | 18 +++++++++ .../java/Problem/ggang9/Chapter3/Q03.java | 19 +++++++++ .../java/Problem/ggang9/Chapter3/Q04.java | 15 +++++++ .../java/Problem/ggang9/Chapter3/Q05.java | 15 +++++++ .../java/Problem/ggang9/Chapter3/Q06.java | 23 +++++++++++ .../java/Problem/ggang9/Chapter3/Q07.java | 23 +++++++++++ .../java/Problem/ggang9/Chapter3/Q08.java | 21 ++++++++++ .../java/Problem/ggang9/Chapter3/Q09.java | 22 +++++++++++ .../java/Problem/ggang9/Chapter3/Q10.java | 39 +++++++++++++++++++ .../java/Problem/ggang9/Chapter4/Q01.java | 25 ++++++++++++ .../java/Problem/ggang9/Chapter4/Q02.java | 20 ++++++++++ .../java/Problem/ggang9/Chapter4/Q03.java | 22 +++++++++++ .../java/Problem/ggang9/Chapter4/Q04.java | 14 +++++++ .../java/Problem/ggang9/Chapter4/Q05.java | 18 +++++++++ 16 files changed, 318 insertions(+) create mode 100644 src/main/java/Problem/ggang9/Chapter1/HelloWorld.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q01.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q02.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q03.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q04.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q05.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q06.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q07.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q08.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q09.java create mode 100644 src/main/java/Problem/ggang9/Chapter3/Q10.java create mode 100644 src/main/java/Problem/ggang9/Chapter4/Q01.java create mode 100644 src/main/java/Problem/ggang9/Chapter4/Q02.java create mode 100644 src/main/java/Problem/ggang9/Chapter4/Q03.java create mode 100644 src/main/java/Problem/ggang9/Chapter4/Q04.java create mode 100644 src/main/java/Problem/ggang9/Chapter4/Q05.java diff --git a/src/main/java/Problem/ggang9/Chapter1/HelloWorld.java b/src/main/java/Problem/ggang9/Chapter1/HelloWorld.java new file mode 100644 index 0000000..aa2985f --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter1/HelloWorld.java @@ -0,0 +1,7 @@ +package Problem.ggang9.Chapter1; + +public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello World"); + } +} diff --git a/src/main/java/Problem/ggang9/Chapter3/Q01.java b/src/main/java/Problem/ggang9/Chapter3/Q01.java new file mode 100644 index 0000000..bf18ac5 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q01.java @@ -0,0 +1,17 @@ +package Problem.ggang9.Chapter3; + +public class Q01 { + + /** + * 평균점수 구하기 + */ + public static void main(String[] args) { + int korean = 80; + int english = 75; + int math = 55; + + System.out.println((korean + english + math) / 3); + + // TODO: 평균 점수를 구해서 출력하기 + } +} \ No newline at end of file diff --git a/src/main/java/Problem/ggang9/Chapter3/Q02.java b/src/main/java/Problem/ggang9/Chapter3/Q02.java new file mode 100644 index 0000000..bd6de3b --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q02.java @@ -0,0 +1,18 @@ +package Problem.ggang9.Chapter3; + +public class Q02 { + + /** + * 홀수 짝수 판별 + */ + public static void main(String[] args) { + int num = 13; + if (num % 2 == 1) { + System.out.println("add"); + } else { + System.out.println("even"); + } + + // TODO: num이 홀수라면 odd를, 짝수라면 even을 출력하기 + } +} diff --git a/src/main/java/Problem/ggang9/Chapter3/Q03.java b/src/main/java/Problem/ggang9/Chapter3/Q03.java new file mode 100644 index 0000000..9ccaedb --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q03.java @@ -0,0 +1,19 @@ +package Problem.ggang9.Chapter3; + +public class Q03 { + + /** + * 주민등록번호 나누기 + */ + public static void main(String[] args) { + String personNumber = "881120-1068234"; + + String a = personNumber.substring(0, 6); + String b = personNumber.substring(7, 14); + System.out.println(a); + System.out.println(b); + + // TODO: 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력하기 + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q04.java b/src/main/java/Problem/ggang9/Chapter3/Q04.java new file mode 100644 index 0000000..ed0a737 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q04.java @@ -0,0 +1,15 @@ +package Problem.ggang9.Chapter3; + +public class Q04 { + + /** + * 주민등록번호 인덱싱 + */ + public static void main(String[] args) { + String personNumber = "881120-1068234"; + System.out.println(personNumber.charAt(7)); + + // TODO: 주민등록번호에서 성별을 나타내는 숫자(뒷자리의 맨 첫번째 숫자)를 출력하기 + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q05.java b/src/main/java/Problem/ggang9/Chapter3/Q05.java new file mode 100644 index 0000000..eeaf538 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q05.java @@ -0,0 +1,15 @@ +package Problem.ggang9.Chapter3; + +public class Q05 { + + /** + * 문자열 바꾸기 + */ + public static void main(String[] args) { + String a = "a:b:c:d"; + System.out.println(a.replace(":","#")); + + // TODO: replace 함수를 사용하여 a#b#c#d로 바꿔서 출력해 보자. + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q06.java b/src/main/java/Problem/ggang9/Chapter3/Q06.java new file mode 100644 index 0000000..44a6af4 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q06.java @@ -0,0 +1,23 @@ +package Problem.ggang9.Chapter3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; + +import static java.util.Collections.*; + +public class Q06 { + + /** + * 리스트 역순 정렬하기 + */ + public static void main(String[] args) { + ArrayList myList = new ArrayList<>(Arrays.asList(1, 3, 5, 4, 2)); + System.out.println(myList); // [1, 3, 5, 4, 2]가 출력됨. + + myList.sort(reverseOrder()); + System.out.println(myList); + // TODO: 리스트를 [5, 4, 3, 2, 1]로 만들어서 출력하기 + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q07.java b/src/main/java/Problem/ggang9/Chapter3/Q07.java new file mode 100644 index 0000000..24b2d8e --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q07.java @@ -0,0 +1,23 @@ +package Problem.ggang9.Chapter3; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Q07 { + + /** + * 리스트를 문자열로 만들기 + */ + public static void main(String[] args) { + ArrayList myList = new ArrayList<>(Arrays.asList("Life", "is", "too", "short")); + System.out.println(myList); // [Life, is, too, short] 출력됨. + + String a = ""; + for (String str : myList) { + a += str + " "; + } + a = a.stripTrailing(); + System.out.println(a); + // TODO: 리스트를 "Life is too short" 문자열로 만들어 출력해 보자. + } +} diff --git a/src/main/java/Problem/ggang9/Chapter3/Q08.java b/src/main/java/Problem/ggang9/Chapter3/Q08.java new file mode 100644 index 0000000..1fe70d7 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q08.java @@ -0,0 +1,21 @@ +package Problem.ggang9.Chapter3; + +import java.util.HashMap; + +public class Q08 { + + /** + * 맵에서 값 추출하기 + */ + public static void main(String[] args) { + HashMap grade = new HashMap<>(); + grade.put("A", 90); + grade.put("B", 80); + grade.put("C", 70); + + System.out.println(grade.remove("B")); + + // TODO: 다음의 맵 grade에서 "B'에 해당되는 값을 추출해 보자. (추출하고 나면 grade에는 "B"에 해당하는 아이템이 사라져야 한다.) + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q09.java b/src/main/java/Problem/ggang9/Chapter3/Q09.java new file mode 100644 index 0000000..bb35cb0 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q09.java @@ -0,0 +1,22 @@ +package Problem.ggang9.Chapter3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Q09 { + + /** + * 중복 숫자 제거하기 + */ + public static void main(String[] args) { + ArrayList numbers = new ArrayList<>(Arrays.asList(1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5)); + System.out.println(numbers); // [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5] 출력됨. + + Set set = new HashSet<>(numbers); + System.out.println(set); + // TODO: numbers 리스트에서 중복 숫자를 제거해 보자. + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter3/Q10.java b/src/main/java/Problem/ggang9/Chapter3/Q10.java new file mode 100644 index 0000000..883ff04 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter3/Q10.java @@ -0,0 +1,39 @@ +package Problem.ggang9.Chapter3; + +import java.util.HashMap; + +import static Problem.ggang9.Chapter3.Coffee.*; + +enum Coffee { + AMERICANO(3000), + ICE_AMERICANO(4000), + CAFE_LATTE(5000); + + private final int coffeePrice; + Coffee(int coffeePrice) { + this.coffeePrice = coffeePrice; + } + public int getCoffeePrice() { + return coffeePrice; + } +} +public class Q10 { + + /** + * 다음은 커피의 종류(1: 아메리카노, 2:아이스 아메리카노, 3:카페라떼)를 입력하면 커피의 가격을 알려주는 프로그램이다. 위 코드에서 1, 2, 3과 같은 매직넘버를 제거하여 프로그램을 개선해보자. + */ + static void printCoffeePrice(Coffee type) { + HashMap priceMap = new HashMap<>(); + priceMap.put(AMERICANO, AMERICANO.getCoffeePrice()); // 1: 아메리카노 + priceMap.put(ICE_AMERICANO, ICE_AMERICANO.getCoffeePrice()); // 2: 아이스 아메리카노 + priceMap.put(CAFE_LATTE, CAFE_LATTE.getCoffeePrice()); // 3: 카페라떼 + int price = priceMap.get(type); + System.out.println(String.format("가격은 %d원 입니다.", price)); + } + + public static void main(String[] args) { + printCoffeePrice(AMERICANO); // "가격은 3000원 입니다." 출력 +// printCoffeePrice(99); // NullPointerException 발생 + } +} + diff --git a/src/main/java/Problem/ggang9/Chapter4/Q01.java b/src/main/java/Problem/ggang9/Chapter4/Q01.java new file mode 100644 index 0000000..6341983 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter4/Q01.java @@ -0,0 +1,25 @@ +package Problem.ggang9.Chapter4; + +public class Q01 { + + /** + * 조건문의 참과 거짓 + */ + public static void main(String[] args) { + String a = "write once, run anywhere"; + if (a.contains("wife")) { + System.out.println("wife"); + } else if (a.contains("once") && !a.contains("run")) { + System.out.println("once"); + } else if (!a.contains("everywhere")) { + System.out.println("everywhere"); + } else if (a.contains("anywhere")) { + System.out.println("anywhere"); + } else { + System.out.println("none"); + } + + // TODO: 코드의 출력 결과가 무엇일지 생각해보기 + // everywhere? + } +} diff --git a/src/main/java/Problem/ggang9/Chapter4/Q02.java b/src/main/java/Problem/ggang9/Chapter4/Q02.java new file mode 100644 index 0000000..ebfbf6e --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter4/Q02.java @@ -0,0 +1,20 @@ +package Problem.ggang9.Chapter4; + +public class Q02 { + + /** + * 3의 배수의 합 + */ + public static void main(String[] args) { + // TODO: while 문을 사용해서 1부터 1000까지의 자연수 중 3의 배수의 합을 구하기 + int i = 0; + int sum = 0; + while(i <= 1000) { + if (i % 3 == 0) { + sum += i; + } + i++; + } + System.out.println(sum); + } +} diff --git a/src/main/java/Problem/ggang9/Chapter4/Q03.java b/src/main/java/Problem/ggang9/Chapter4/Q03.java new file mode 100644 index 0000000..5a76200 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter4/Q03.java @@ -0,0 +1,22 @@ +package Problem.ggang9.Chapter4; + +public class Q03 { + + /** + * 별 표시하기 + */ + public static void main(String[] args) { + // TODO: while문 또는 for 문을 사용하여 다음과 같이 별(*)을 표시하는 프로그램을 작성해 보자. + + //* + //** + //*** + //**** + //***** + String star = "*"; + for (int i=0; i<5; i++) { + System.out.println(star); + star += "*"; + } + } +} diff --git a/src/main/java/Problem/ggang9/Chapter4/Q04.java b/src/main/java/Problem/ggang9/Chapter4/Q04.java new file mode 100644 index 0000000..a59f128 --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter4/Q04.java @@ -0,0 +1,14 @@ +package Problem.ggang9.Chapter4; + +public class Q04 { + + /** + * 1부터 100까지 출력하기 + */ + public static void main(String[] args) { + // TODO: for문을 사용해 1부터 100까지의 숫자를 출력해 보자. + for (int i=1; i<101; i++) { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/src/main/java/Problem/ggang9/Chapter4/Q05.java b/src/main/java/Problem/ggang9/Chapter4/Q05.java new file mode 100644 index 0000000..bcb4d3e --- /dev/null +++ b/src/main/java/Problem/ggang9/Chapter4/Q05.java @@ -0,0 +1,18 @@ +package Problem.ggang9.Chapter4; + +public class Q05 { + + /** + * 평균점수 구하기 + */ + public static void main(String[] args) { + int[] marks = {70, 60, 55, 75, 95, 90, 80, 80, 85, 100}; + + // TODO: for each 문을 사용하여 A 학급의 평균 점수를 구해 보자. + int sum = 0; + for (int score : marks) { + sum += score; + } + System.out.println(sum / marks.length); + } +} From 0ee8b7c78aa16a8c27a0be04395151fd5783ff94 Mon Sep 17 00:00:00 2001 From: ggomabbang Date: Sat, 8 Jul 2023 21:01:58 +0900 Subject: [PATCH 3/3] problem34-sunny --- src/main/java/Problem/sunny/Chapter3/Q01.java | 3 ++- src/main/java/Problem/sunny/Chapter3/Q02.java | 5 ++++ src/main/java/Problem/sunny/Chapter3/Q03.java | 2 ++ src/main/java/Problem/sunny/Chapter3/Q04.java | 1 + src/main/java/Problem/sunny/Chapter3/Q05.java | 2 ++ src/main/java/Problem/sunny/Chapter3/Q06.java | 3 +++ src/main/java/Problem/sunny/Chapter3/Q07.java | 2 ++ src/main/java/Problem/sunny/Chapter3/Q08.java | 2 ++ src/main/java/Problem/sunny/Chapter3/Q09.java | 3 +++ src/main/java/Problem/sunny/Chapter3/Q10.java | 23 +++++++++++++------ src/main/java/Problem/sunny/Chapter4/Q01.java | 1 + src/main/java/Problem/sunny/Chapter4/Q02.java | 9 ++++++++ src/main/java/Problem/sunny/Chapter4/Q03.java | 5 ++++ src/main/java/Problem/sunny/Chapter4/Q04.java | 3 +++ src/main/java/Problem/sunny/Chapter4/Q05.java | 7 ++++++ 15 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/main/java/Problem/sunny/Chapter3/Q01.java b/src/main/java/Problem/sunny/Chapter3/Q01.java index 6117f91..6041f75 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q01.java +++ b/src/main/java/Problem/sunny/Chapter3/Q01.java @@ -9,7 +9,8 @@ public static void main(String[] args) { int korean = 80; int english = 75; int math = 55; - // TODO: 평균 점수를 구해서 출력하기 + float result = (korean + english + math) / 3; + System.out.println(result); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q02.java b/src/main/java/Problem/sunny/Chapter3/Q02.java index 7eda8d0..ff3b622 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q02.java +++ b/src/main/java/Problem/sunny/Chapter3/Q02.java @@ -9,5 +9,10 @@ public static void main(String[] args) { int num = 13; // TODO: num이 홀수라면 odd를, 짝수라면 even을 출력하기 + if ((num % 2) == 1) { + System.out.printf("odd"); + } else { + System.out.println("even"); + } } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q03.java b/src/main/java/Problem/sunny/Chapter3/Q03.java index 6ca3e45..e2a9cb6 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q03.java +++ b/src/main/java/Problem/sunny/Chapter3/Q03.java @@ -9,5 +9,7 @@ public static void main(String[] args) { String personNumber = "881120-1068234"; // TODO: 주민등록번호를 연월일(YYYYMMDD) 부분과 그 뒤의 숫자 부분으로 나누어 출력하기 + System.out.println(personNumber.substring(0, 6)); + System.out.println(personNumber.substring(7, 14)); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q04.java b/src/main/java/Problem/sunny/Chapter3/Q04.java index ab712d8..319f5d3 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q04.java +++ b/src/main/java/Problem/sunny/Chapter3/Q04.java @@ -9,5 +9,6 @@ public static void main(String[] args) { String personNumber = "881120-1068234"; // TODO: 주민등록번호에서 성별을 나타내는 숫자(뒷자리의 맨 첫번째 숫자)를 출력하기 + System.out.println(personNumber.charAt(7)); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q05.java b/src/main/java/Problem/sunny/Chapter3/Q05.java index 4a0af52..2b0414b 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q05.java +++ b/src/main/java/Problem/sunny/Chapter3/Q05.java @@ -9,5 +9,7 @@ public static void main(String[] args) { String a = "a:b:c:d"; // TODO: replace 함수를 사용하여 a#b#c#d로 바꿔서 출력해 보자. + String b = a.replaceAll(":", "#"); + System.out.println(b); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q06.java b/src/main/java/Problem/sunny/Chapter3/Q06.java index c1c888a..fbfab79 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q06.java +++ b/src/main/java/Problem/sunny/Chapter3/Q06.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; public class Q06 { @@ -13,5 +14,7 @@ public static void main(String[] args) { System.out.println(myList); // [1, 3, 5, 4, 2]가 출력됨. // TODO: 리스트를 [5, 4, 3, 2, 1]로 만들어서 출력하기 + myList.sort(Comparator.reverseOrder()); + System.out.println(myList); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q07.java b/src/main/java/Problem/sunny/Chapter3/Q07.java index 405168e..e579ff9 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q07.java +++ b/src/main/java/Problem/sunny/Chapter3/Q07.java @@ -13,5 +13,7 @@ public static void main(String[] args) { System.out.println(myList); // [Life, is, too, short] 출력됨. // TODO: 리스트를 "Life is too short" 문자열로 만들어 출력해 보자. + String a = String.join(" ", myList); + System.out.println(a); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q08.java b/src/main/java/Problem/sunny/Chapter3/Q08.java index 7bde89d..7566b9e 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q08.java +++ b/src/main/java/Problem/sunny/Chapter3/Q08.java @@ -14,5 +14,7 @@ public static void main(String[] args) { grade.put("C", 70); // TODO: 다음의 맵 grade에서 "B'에 해당되는 값을 추출해 보자. (추출하고 나면 grade에는 "B"에 해당하는 아이템이 사라져야 한다.) + grade.remove("B"); + System.out.println(grade); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q09.java b/src/main/java/Problem/sunny/Chapter3/Q09.java index 650150e..7d3c0ca 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q09.java +++ b/src/main/java/Problem/sunny/Chapter3/Q09.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; public class Q09 { @@ -13,5 +14,7 @@ public static void main(String[] args) { System.out.println(numbers); // [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5] 출력됨. // TODO: numbers 리스트에서 중복 숫자를 제거해 보자. + HashSet nums = new HashSet<>(numbers); + System.out.println(nums); } } diff --git a/src/main/java/Problem/sunny/Chapter3/Q10.java b/src/main/java/Problem/sunny/Chapter3/Q10.java index c0e704a..63fa69f 100644 --- a/src/main/java/Problem/sunny/Chapter3/Q10.java +++ b/src/main/java/Problem/sunny/Chapter3/Q10.java @@ -7,17 +7,26 @@ public class Q10 { /** * 다음은 커피의 종류(1: 아메리카노, 2:아이스 아메리카노, 3:카페라떼)를 입력하면 커피의 가격을 알려주는 프로그램이다. 위 코드에서 1, 2, 3과 같은 매직넘버를 제거하여 프로그램을 개선해보자. */ - static void printCoffeePrice(int type) { - HashMap priceMap = new HashMap<>(); - priceMap.put(1, 3000); // 1: 아메리카노 - priceMap.put(2, 4000); // 2: 아이스 아메리카노 - priceMap.put(3, 5000); // 3: 카페라떼 + enum CoffeeType { + AMERICANO, + ICE_AMERICANO, + CAFE_LATTE + } + + ; + + static void printCoffeePrice(CoffeeType type) { + HashMap priceMap = new HashMap<>(); + priceMap.put(CoffeeType.AMERICANO, 3000); // 1: 아메리카노 + priceMap.put(CoffeeType.ICE_AMERICANO, 4000); // 2: 아이스 아메리카노 + priceMap.put(CoffeeType.CAFE_LATTE, 5000); // 3: 카페라떼 int price = priceMap.get(type); System.out.println(String.format("가격은 %d원 입니다.", price)); } public static void main(String[] args) { - printCoffeePrice(1); // "가격은 3000원 입니다." 출력 - printCoffeePrice(99); // NullPointerException 발생 + printCoffeePrice(CoffeeType.AMERICANO); // "가격은 3000원 입니다." 출력 + printCoffeePrice(CoffeeType.CAFE_LATTE); + //printCoffeePrice(99); // NullPointerException 발생 } } diff --git a/src/main/java/Problem/sunny/Chapter4/Q01.java b/src/main/java/Problem/sunny/Chapter4/Q01.java index 585500c..b9a410d 100644 --- a/src/main/java/Problem/sunny/Chapter4/Q01.java +++ b/src/main/java/Problem/sunny/Chapter4/Q01.java @@ -20,5 +20,6 @@ public static void main(String[] args) { } // TODO: 코드의 출력 결과가 무엇일지 생각해보기 + // everywhere } } diff --git a/src/main/java/Problem/sunny/Chapter4/Q02.java b/src/main/java/Problem/sunny/Chapter4/Q02.java index 4eecf3c..b7273de 100644 --- a/src/main/java/Problem/sunny/Chapter4/Q02.java +++ b/src/main/java/Problem/sunny/Chapter4/Q02.java @@ -7,5 +7,14 @@ public class Q02 { */ public static void main(String[] args) { // TODO: while 문을 사용해서 1부터 1000까지의 자연수 중 3의 배수의 합을 구하기 + int i = 1; + int sum = 0; + while (i <= 1000){ + if ((i%3) == 0){ + sum += i; + } + i++; + } + System.out.println(sum); } } diff --git a/src/main/java/Problem/sunny/Chapter4/Q03.java b/src/main/java/Problem/sunny/Chapter4/Q03.java index 8b84fb9..7f4aab0 100644 --- a/src/main/java/Problem/sunny/Chapter4/Q03.java +++ b/src/main/java/Problem/sunny/Chapter4/Q03.java @@ -7,7 +7,12 @@ public class Q03 { */ public static void main(String[] args) { // TODO: while문 또는 for 문을 사용하여 다음과 같이 별(*)을 표시하는 프로그램을 작성해 보자. + String stars = ""; + for (int i=0; i<5; i++){ + stars += "*"; + System.out.println(stars); + } //* //** //*** diff --git a/src/main/java/Problem/sunny/Chapter4/Q04.java b/src/main/java/Problem/sunny/Chapter4/Q04.java index d6b33ab..c1575d1 100644 --- a/src/main/java/Problem/sunny/Chapter4/Q04.java +++ b/src/main/java/Problem/sunny/Chapter4/Q04.java @@ -7,5 +7,8 @@ public class Q04 { */ public static void main(String[] args) { // TODO: for문을 사용해 1부터 100까지의 숫자를 출력해 보자. + for (int i=1; i<=100; i++){ + System.out.println(i); + } } } diff --git a/src/main/java/Problem/sunny/Chapter4/Q05.java b/src/main/java/Problem/sunny/Chapter4/Q05.java index 8bbe869..03b45a6 100644 --- a/src/main/java/Problem/sunny/Chapter4/Q05.java +++ b/src/main/java/Problem/sunny/Chapter4/Q05.java @@ -9,5 +9,12 @@ public static void main(String[] args) { int[] marks = {70, 60, 55, 75, 95, 90, 80, 80, 85, 100}; // TODO: for each 문을 사용하여 A 학급의 평균 점수를 구해 보자. + int sum = 0; + float result = 0; + for (int score : marks){ + sum += score; + } + result = sum / 10; + System.out.println(result); } }