Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

290570

05.09 과제 본문

카테고리 없음

05.09 과제

qordpsem 2024. 5. 9. 17:22

#문제1

 

 

#작성 코드

import java.util.Scanner;
class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int num;
		double sum; 
		System.out.print("물건을 몇 개 구매하셨습니까? ==>");
		num = sc.nextInt();
		sum = num*100;

		if(num >= 10){
			sum = sum*0.9;
		}
		System.out.print(sum+"원");
	}
}

 

 

#결과

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

##수정

 

가격이 소수점 단위로 나올일이 없기 때문에 int로 바꿔줌

 

import java.util.Scanner;
class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int num, sum;
		System.out.print("물건을 몇 개 구매하셨습니까? ==>");
		num = sc.nextInt();
		sum = num*100;

		if(num >= 10){
			sum = (int)(sum*0.9);
		}
		System.out.print(sum+"원");
	}
}

double sum; 을     ==>    int sum;    으로만 수정했다가

해당 오류가 떠서

sum = (int)(sum*0.9);   로 수정하여 int로 형변환 시켜줬다.

 

다시 생각해보니 sum = num*90 으로만 수정해도 됐을것 같다.

 

 

#결과


 

 

#문제2

 

 

#작성 코드

import java.util.Scanner;
class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		String result="미당첨";
		int num;
		System.out.print("1~10 중 번호 하나를 고르세요 ==>");
		num = sc.nextInt();

		if(num>10 || num<1){
			result = "1~10 중에서만 선택가능합니다.";
		}else if(num == 2){
			result = "1등에 당첨되었습니다.";
		}else if(num == 5){
			result = "2등에 당첨되었습니다.";
		}else if(num == 7){
			result = "3등에 당첨되었습니다.";
		}
		System.out.print(result);
	}
}

 

 

#결과


 

 

#문제3

 

 

#작성 코드

import java.util.Scanner;

class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		double height, weight, standard;
		String result = "표준";

		System.out.print("키 몇? ==> ");
		height = sc.nextDouble();
		System.out.print("몸무게 몇? ==> ");
		weight = sc.nextDouble();
		
		standard = (height - 100)*0.9;
		if(weight > standard){
			result = "과체중";
		}else if(weight < standard){
			result = "저체중";
		}
		System.out.print("당신은 "+result+"입니다.");
	}
}

 

 

#결과


 

 

#문제4

 

 

#작성 코드

import java.util.Scanner;

class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		int a,b,c;
		System.out.print("첫번째 숫자 ==>");
		a = sc.nextInt();
		System.out.print("두번째 숫자 ==>");
		b = sc.nextInt();
		System.out.print("세번째 숫자 ==>");
		c = sc.nextInt();

		int result=a;

		if(a<b){
			if(a<c){
				result = a;
			}
		}else if(b<c){
			if(b<a){
				result = b;
			}
		}else if(c<a){
			if(c<b){
				result = c;
			}
		}
		System.out.print(result);
	}
}

 

 

#결과


 

 

#문제5

 

 

#작성 코드

import java.util.Scanner;

class D000{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		double x, result;

		System.out.print("x 값을 입력 ==> ");
		x = sc.nextDouble();

		if (x<=0) {
			result = x*x*x - 9*x +2;
		}else{
			result = 7*x +2;
		}
		System.out.print(result);
	}
}

 

 

#결과

 

0.2를 넣었는데 3.4 가 아니라 3.4000000000000004 가 나왔다..

검색해보니 이걸 부동소수점 오류라고 하는데, 컴퓨터가 십진법이 이진법을 쓰기 때문이라고 한다

해당 오류는 다음 글에서 자세히 다루고,,  코드도 마저 완성하는걸로..!