๐ŸŒฑJava ๋ฌธ๋ฒ• ์ข…ํ•ฉ๋ฐ˜ 1์ฃผ์ฐจ ์ˆ™์ œ

์ด์œ ์ง„ยท2024๋…„ 7์›” 23์ผ

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
10/24

๐ŸŒท์š”๋ฆฌ ๋ ˆ์‹œํ”ผ ๋ฉ”๋ชจ์žฅ ๋งŒ๋“ค๊ธฐ

  • ๋‚ด๊ฐ€ ์ข‹์•„ํ•˜๋Š” ์š”๋ฆฌ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๊ณ  ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ์ถœ๋ ฅ

    ์ฒ˜์Œ ์š”๋ฆฌ ์ œ๋ชฉ์„ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๊ฐ’์„ ์ฃผ๊ณ  ์ถœ๋ ฅํ•˜๋Š”๋ฐ ๊ณ„์† ์˜ค๋ฅ˜๊ฐ€ ๋–ด๋‹ค. ๋ถ„๋ช… ๋ฌธ์ž์—ด๋„ ์ฐธ์กฐํ˜• ๋ณ€์ˆ˜๋ผ ํ–ˆ๋Š”๋ฐ,, ๋ฐฐ์—ด๋กœ ํ•ด์•ผํ•˜๋‚˜ ์‹ถ์–ด์„œ Array toString๋„ ํ•ด๋ณด๊ณ  ๋ณ€์ˆ˜ ์ด๋ฆ„๋„ ๋ฐ”๊ฟ”๋ณด๊ณ  ํ–ˆ๋Š”๋ฐ ์•ˆ๋˜๋Š”๊ฑฐ๋‹ค!! ์•Œ๊ณ ๋ณด๋‹ˆ string์„ ์†Œ๋ฌธ์ž๋กœ ์ž…๋ ฅํ•ด์„œ ์•ˆ๋˜๋Š” ๊ฑฐ์˜€๋‹ค,,, String ๋Œ€๋ฌธ์ž ๊ธฐ์–ตํ•˜์ž~

    public class Main {
    
        public static void main(String[] args) {
    
            String title = "๋ฐฑ์ข…์› ๋ผ์ง€๊ณ ๊ธฐ ๊น€์น˜์ฐŒ๊ฐœ ๋งŒ๋“ค๊ธฐ";
            System.out.println("[ " + title + " ]");
    
        }
    }
  • ์š”๋ฆฌ ๋ณ„์ ์„ 1~5 ์‚ฌ์ด์˜ ์†Œ์ˆ˜์ ์ด ์žˆ๋Š” ์‹ค์ˆ˜๋กœ ์ž…๋ ฅํ•˜๊ณ  ์†Œ์ˆ˜์ ์„ ์ œ์™ธํ•œ ์ •์ˆ˜๋กœ๋งŒ ์ถœ๋ ฅ

    ์ด๊ฑด ์‹ค์ˆ˜ํ˜•์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•˜๋ผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ํ•™์Šต์ž๋ฃŒ ๋ณด๊ณ  ์ฐธ๊ณ ํ•˜๋ฉด์„œ ํ–ˆ๋”๋‹ˆ ์ด๊ฑด ํ•œ๋ฒˆ์— ์„ฑ๊ณตํ–ˆ๋‹ค.

    public class Main {
    
        public static void main(String[] args) {
    
            String title = "๋ฐฑ์ข…์› ๋ผ์ง€๊ณ ๊ธฐ ๊น€์น˜์ฐŒ๊ฐœ ๋งŒ๋“ค๊ธฐ";
    
            double doubleScore = 3.5;
            int intScore;
            intScore = (int) doubleScore;
    
            System.out.println("[ " + title + " ]");
            System.out.println(intScore);
    
        }
    }
  • ๋ฐ”๋กœ ๋’ค์— ์ •์ˆ˜ ๋ณ„์ ์„ 5์  ๋งŒ์  ํผ์„ผํŠธ๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ ๊ฐ’์„ ์‹ค์ˆ˜๋กœ ์ถœ๋ ฅ

    ์ด๊ฒŒ ๋งž๋‚˜..? ์‹ถ์ง€๋งŒ ์ผ๋‹จ ๊ฒฐ๊ณผ๋Š” ๋งž๊ฒŒ ๋‚˜์˜จ๋‹ค!

    public class Main {
    
        public static void main(String[] args) {
    
            String title = "๋ฐฑ์ข…์› ๋ผ์ง€๊ณ ๊ธฐ ๊น€์น˜์ฐŒ๊ฐœ ๋งŒ๋“ค๊ธฐ";
    
            double doubleScore = 3.5;
            double percent = 60.0;
            int intScore;
            intScore = (int) doubleScore;
    
            System.out.println("[ " + title + " ]");
            System.out.println("๋ณ„์  : " + intScore + " (" + percent + "%)");
    
        }
    }
  • ์š”๋ฆฌ ๋ ˆ์‹œํ”ผ 10๋ฌธ์žฅ์„ ์ž…๋ ฅ ํ›„ ๋ชจ๋“  ๋ฌธ์žฅ ์•ž์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์—ฌ์„œ ๋ชจ๋‘ ์ถœ๋ ฅ

    ์ด๊ฒƒ๋„ String ๋ฌธ์ž์—ด๋กœ ๋ณ€์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ณ  ์ถœ๋ ฅํ•  ๋•Œ ์•ž์— ๋ฒˆํ˜ธ๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

    public class Main {
    
        public static void main(String[] args) {
    
            String title = "๋ฐฑ์ข…์› ๋ผ์ง€๊ณ ๊ธฐ ๊น€์น˜์ฐŒ๊ฐœ ๋งŒ๋“ค๊ธฐ";
    
            double doubleScore = 3.5;
            double percent = 60.0;  // ๋ฐฑ๋ถ„์œจ ๊ณ„์‚ฐ intScore * 100 / 5;
            int intScore;
            intScore = (int) doubleScore;
    
            String one = "๋ผ์ง€๊ณ ๊ธฐ๋Š” ํ•๋ฌผ์„ ๋นผ์ฃผ์„ธ์š”.";
            String two = "์ž˜์ต์€ ๊น€์น˜ ํ•œํฌ๊ธฐ๋ฅผ ๊บผ๋‚ด์„œ ์ž˜๋ผ์ฃผ์„ธ์š”.";
            String three = "๋ƒ„๋น„์— ๋“ค๊ธฐ๋ฆ„ ์ ๋‹นํžˆ ๋‘๋ฅด๊ณ  ๊น€์น˜๋ฅผ ๋„ฃ๊ณ  ๋ณถ์•„์ฃผ์„ธ์š”.";
            String four = "๋‹ค์ง„๋งˆ๋Š˜ ํ•œ์Šคํ‘ผ, ์„คํƒ• ํ•œ์Šคํ‘ผ ๋„ฃ์–ด์ฃผ์„ธ์š”.";
            String five = "์ข…์ด์ปต์œผ๋กœ ๋ฌผ 8์ปต ๋ถ€์–ด์„œ ์„ผ๋ถˆ์— ๋“์—ฌ์ฃผ์„ธ์š”.";
            String six = "ํ•๋ฌผ ๋บ€ ๋ผ์ง€๊ณ ๊ธฐ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”.";
            String seven = "๋œ์žฅ ๋ฐ˜์Šคํ‘ผ, ์–‘ํŒŒ ๋ฐ˜๊ฐœ, ์ฒญ์–‘๊ณ ์ถ” ํ•œ๊ฐœ๋ฅผ ์ฐ์–ด์„œ ๋„ฃ์–ด์ฃผ์„ธ์š”.";
            String eight = "๊ฐ„์žฅ ๋‘์Šคํ‘ผ๋ฐ˜, ์ƒˆ์šฐ์ “ ๋‘์Šคํ‘ผ, ๊ณ ์ถง๊ฐ€๋ฃจ ๋‘์Šคํ‘ผ๋ฐ˜ ๋„ฃ์–ด์ฃผ์„ธ์š”.";
            String nine = "์ค‘๋ถˆ๋กœ ์ค„์—ฌ์„œ ์˜ค๋ž˜ ๋“์—ฌ์ฃผ์„ธ์š”.";
            String ten = "๋งˆ์ง€๋ง‰์— ํŒŒ ์ญ์ญ ์ฐ์–ด์„œ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉด ๋ผ์š”";
    
            System.out.println("[ " + title + " ]");
            System.out.println("๋ณ„์  : " + intScore + " (" + percent + "%)");
            System.out.println("1. " + one);
            System.out.println("2. " + two);
            System.out.println("3. " + three);
            System.out.println("4. " + four);
            System.out.println("5. " + five);
            System.out.println("6. " + six);
            System.out.println("7. " + seven);
            System.out.println("8. " + eight);
            System.out.println("9. " + nine);
            System.out.println("10. " + ten);
    
        }
    }

โœจ ์ถœ๋ ฅ

๐Ÿ‰์ •๋‹ต

  • ์ด๋Ÿฐ ์ˆ˜๋ฐ•๊ฐ™์€... ๋‚œ ๊ฐ•์˜์—์„œ ๋ฐฐ์šด๋Œ€๋กœ ํ–ˆ๋Š”๋ฐ ์ •๋‹ต์€ Scanner๋ฅผ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ๊ฐ’์„ ์ง์ ‘ ๋ฐ›๋Š”๊ฑฐ์˜€๋‹ค,, (์•ˆ์•Œ๋ ค์คฌ๋Š”๋ฐ ์–ด์ผ€ํ•ด์š”ใ… ) ๋ฐฑ๋ถ„์œจ ๊ณ„์‚ฐ ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฑฐ์ง€ ํ–ˆ๋Š”๋ฐ ์ ์ˆ˜ * 100 / 5 ์ด๊ฑฐ์˜€๋‹ค.

    import java.util.Scanner;
    
    public class First_project {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String cookName = sc.nextLine();
            String recipe1 = sc.nextLine();
            String recipe2 = sc.nextLine();
            String recipe3 = sc.nextLine();
            String recipe4 = sc.nextLine();
            String recipe5 = sc.nextLine();
            String recipe6 = sc.nextLine();
            String recipe7 = sc.nextLine();
            String recipe8 = sc.nextLine();
            String recipe9 = sc.nextLine();
            String recipe10 = sc.nextLine();
            float score = sc.nextFloat();
    
            int scoreNumber = (int)score; // ๋ณ„์  int ํ˜• ๋ณ€ํ™˜
            double percentage = scoreNumber * 100 / 5; // ํผ์„ผํŠธ ๊ณ„์‚ฐ
    
            // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
            System.out.println(score);
            System.out.println("[ " + cookName + " ]");
            System.out.println("๋ณ„์  : " + scoreNumber + " (" + percentage + "%)");
            System.out.println("1. " + recipe1);
            System.out.println("2. " + recipe2);
            System.out.println("3. " + recipe3);
            System.out.println("4. " + recipe4);
            System.out.println("5. " + recipe5);
            System.out.println("6. " + recipe6);
            System.out.println("7. " + recipe7);
            System.out.println("8. " + recipe8);
            System.out.println("9. " + recipe9);
            System.out.println("10. " + recipe10);
        }
    }
    • Scannerํด๋ž˜์Šค๋Š” ๋‹ค์–‘ํ•œ ์ž…๋ ฅ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํŽธ๋ฆฌํ•œ ๋„๊ตฌ๋ผ๊ณ  ํ•œ๋‹ค.

      Scanner sc = new Scanner(System.in); โ†’ 'Scanner' ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ฝ˜์†” ์ž…๋ ฅ์„ ์ฝ์„ ์ค€๋น„

      • 'Scanner' : ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ์ž…๋ ฅ ์†Œ์Šค(์ฝ˜์†”=ํ‚ค๋ณด๋“œ, ํŒŒ์ผ, ๋ฌธ์ž์—ด ๋“ฑ)๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.
      • 'sc' : scanner๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€
      • 'new' : ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ, ๋ฉ”๋ชจ๋ฆฌ ํž™ ์˜์—ญ์— ์ƒˆ๋กœ์šด Scanner ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๊ณ , ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜
      • 'system.in' : ์ž๋ฐ”์˜ ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ 'InputStream'์„ ๋ฐ›์•„์„œ, ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ์ฝ๊ณ  ํŒŒ์‹ฑํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
profile
๐Ÿ™Œ์ค‘์š”ํ•œ๊ฑด ๊บพ์˜€๋Š”๋ฐ๋„ ๊ทธ๋ƒฅ ํ•˜๋Š” ๋งˆ์Œ

0๊ฐœ์˜ ๋Œ“๊ธ€