히치키치

[명품 자바 에센셜] 실습문제 2장 본문

명품 자바 에센셜

[명품 자바 에센셜] 실습문제 2장

히치키치 2021. 4. 12. 06:50

1번

import java.util.Scanner;

public class One{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("두 정수를 입력하세요>>");
        int x= sc.nextInt();
        int y= sc.nextInt();
        System.out.println(x+ "+" +y +"은 "+(x+y));
        sc.close();
    }

}

2번

import java.util.Scanner;

public class Two{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("몇 층인지 입력하세요>>");
        int x= sc.nextInt();
        System.out.println((x*5)+"m 입니다.");
        sc.close();
    }

}

3번

import java.util.Scanner;

public class Three{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("x 값을 입력하세요>>");
        int x= sc.nextInt();
        System.out.println("x="+x+", y="+(x*x-3*x+7));
        sc.close();
    }

}

4번

import java.util.Scanner;

public class Four{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("점 (x,y)의 좌표를 입력하세요>>");
        int x= sc.nextInt();
        int y= sc.nextInt();
        if ((x>=50 && x<=100)&&(y>=50 && y<=100)) {
            System.out.println("점(" + x + "," + y + ")" + "은 (50, 50)과 (100, 100)의 사각형 내에 있습니다.");
            }
        else {
            System.out.println("점(" + x + "," + y + ")" + "은 (50, 50)과 (100, 100)의 사각형 내에 있지 않습니다.");
        }
        sc.close();
    }

}

5번

import java.util.Scanner;

public class Five{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("논리 연산을 입력하세요>>");
        boolean a=sc.nextBoolean();
        String op=sc.next();
        boolean b=sc.nextBoolean();

        switch(op) {
        case "AND" : {System.out.println(a && b);break;}
        case "OR" : {System.out.println(a || b);break;}
        }

        sc.close();
    }

}

6번

import java.util.Scanner;

public class Six{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("돈의 액수를 입력하세요>>");
        int money=sc.nextInt();
        int rest=money;

        money/=50000;
        System.out.print("오만원"+money+"개, ");
        money=rest%50000;

        money/=10000;
        System.out.print("만원"+money+"개, ");
        money=rest%10000;

        money/=1000;
        System.out.print("천원"+money+"개, ");
        money=rest%1000;

        money/=500;
        System.out.print("500원"+money+"개, ");
        money=rest%500;

        money/=100;
        System.out.print("100원"+money+"개, ");
        money=rest%100;

        money/=10;
        System.out.print("10원"+money+"개, ");
        money=rest%10;

        System.out.print("1원"+money+"개");


        sc.close();
    }

}

7번

import java.util.Scanner;

public class Seven{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("학점을 입력하세요>>");
        String grade=sc.next();

        switch(grade) {
        case "A":case"B":{
            System.out.println("Excellent");
            break;
        }

        case "C":case"D":{
            System.out.println("Good");
            break;
        }
        case "F":{
            System.out.println("Bye");
            break;
        }
        default:{
            System.out.println("잘못된 성적 입력입니다.");
            break;
        }
    }



        sc.close();
    }

}

8번

import java.util.Scanner;

public class One{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("커피 주문하세요>>");
        String coffee=sc.next();
        int num=sc.nextInt();

        switch(coffee) {
        case "에스프레소":{
            System.out.println(2000*num+"원입니다.");
            break;}
        case "아메리카노":{
            System.out.println(2500*num+"원입니다.");
            break;}
        case "카푸치노":{
            System.out.println(3000*num+"원입니다.");
            break;}
        case "카페라떼":{
            System.out.println(3500*num+"원입니다.");
            break;}

    }



        sc.close();
    }

}
import java.util.Scanner;

public class One{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("커피 주문하세요>>");
        String coffee=sc.next();
        int num=sc.nextInt();


        if (coffee.equals("에스프레소")){
            System.out.println(2000*num+"원입니다.");}
        else if(coffee.equals("아메리카노")){
            System.out.println(2500*num+"원입니다.");}
        else if (coffee.equals("카푸치노")){
            System.out.println(3000*num+"원입니다.");}
        else if (coffee.equals("카페라떼")){
            System.out.println(3500*num+"원입니다.");}

        sc.close();
    }

}

9번

import java.util.Scanner;
public class nine {
    public static void main (String[] args) {
        System.out.print("1~99 사이의 정수를 입력하세요>>");
        Scanner scanner=new Scanner(System.in);
        int num=scanner.nextInt();
        int a= num/10;
        int b= num%10;
        System.out.print("박수");
        if ((a==3 || a==6 || a==9) || (b==3 || b==6 || b==9)) {
            System.out.print("짝");
            if ((a==3 || a==6 || a==9) && (b==3 || b==6 || b==9)) {
                System.out.print("짝");
            }
        }
        else {
            System.out.print("없음");
        }

    }

}

10번

import java.util.Scanner;
public class ten{
    public static void main(String[] args) {
        System.out.print("커피 주문하세요>>");
        Scanner scanner=new Scanner(System.in);

        String coffee=scanner.next();
        int num=scanner.nextInt();

        if (coffee.equals("에스프레소")){
            if (num>=10){
                System.out.print((int)(num*2000*0.95)); 
                //형 변환 주의!!-> 정수 형태로 할인된 값 출력되도록!!
            }
            else {
                System.out.print(num*2000);
            }
        }
        else if (coffee.equals("아메리카노")) {
            System.out.print(num*2500);
        }
        else if (coffee.equals("카푸치노")) {
            System.out.print(num*3000);
        }
        else if (coffee.equals("카페라떼")) {
            System.out.print(num*3500);
        }

        System.out.print("원입니다.");
    }
}

bonus1

import java.util.Scanner;
public class bonus{
    public static void main(String[] args) {
        System.out.println("식을 입력하세요>>");
        Scanner scanner=new Scanner(System.in);
        double op1=scanner.nextDouble();
        String op=scanner.next();
        double op2=scanner.nextDouble();
        double result=0;

        switch(op) {
        case "+" : { result=op1+op2;break;}
        case "-" : { result=op1-op2;break;}
        case "*" : { result=op1*op2;break;}
        case "/" : { 
            if (op2==0) {System.out.println("0으로 나눌 수 없습니다.");return;}
            result=op1/op2;
            break;}
        default:System.out.println("연산 기호가 잘못되었습니다.");
        }
        System.out.println("연산 결과 "+result);
    }
}


Comments