Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 사전학습
- 퍼셉트론
- html 기초
- 미디어쿼리
- CSS
- java
- 파이썬
- 푸리에 변환
- BorderLayout
- iframe 태그
- Codility
- 메서드
- HTML
- inline
- FFT
- 상속
- Database
- ObjectOutputStream
- 반응형웹
- Position
- FlowLayout
- FileWriter
- oracle
- 반응형 웹 프로젝트
- 예제
- css 기초
- g검정
- rnn
- GridLayout
- html 프로젝트
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 4일차 본문

예제
다음을 입력받아 삼각형넓이를 계산하시오
삼각형넓이 = (밑변 * 높이) / 2
Class name : Prac1.java
<실행 결과>
--입력--
**** 삼각형의 넓이 구하기 ****
밑변 : 10
높이 : 3
--출력--
넓이 : XX.XX → 소수점 2자리까지 출력하시오.
import java.util.Scanner;
public class Prac1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double w, h;
System.out.println("--입력--");
System.out.print("밑변 : ");
w = sc.nextDouble();
System.out.print("높이 : ");
h = sc.nextDouble();
System.out.println();
System.out.println("--출력--");
System.out.printf("넓이 : %.2f", w * h / 2);
}
}
printf로 특정 소수점까지 표현하기
: %.nf (n은 원하는 소수점 자릿수), 소수 n째자리까지 반올림.
- 소수점 둘째자리까지
public class Exam1 {
public static void main(String[] args) {
double a = 0.2563;
double b = 3.2544789;
System.out.printf("a x b 를 소수점 둘째자리까지만 출력하면 : %.2f", a*b); // 0.83
}
}
- 소수점 출력하지 않음
public class Exam1 {
public static void main(String[] args) {
double a = 0.2563;
double b = 3.2544789;
System.out.printf("a x b 를 소수점 둘째자리까지만 출력하면 : %.0f", a*b); // 1
}
}
예제
이름이 L(name)인 사람의 기본급(basePay)이 2500000일때 3.3% 세금(tax)과 월급(salary)을 계산하시오.
(단, 데이터는 반드시 변수에 저장)
세금 = 기본급 * 3.3% [ 3.3% → 0.033 ]
월급 = 기본급 - 세금
Class Name : Prac2.java
<실행 결과>
이름 입력 : L
기본급 입력 : 2500000
*** L의 월급 ***
기본급 : 2500000원
세금 : 82500원
월급 : 2417500원
import java.util.Scanner;
public class Prac2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char name;
int basePay;
double tax, salary;
System.out.print("이름 입력 : ");
name = sc.next().charAt(0);
System.out.print("기본급 입력 : ");
basePay = sc.nextInt();
tax = basePay * 0.033;
salary = basePay - tax;
System.out.println();
System.out.println("*** " + name + "의 월급 ***");
System.out.println("기본급 : " + basePay + "원");
System.out.printf("세금 : %.0f원\n", tax);
System.out.printf("월급 : %.0f원", salary);
}
}
예제
금액(돈)이 5679원일때 아래와 같이 출력하시오.
→ 금액은 10000원 미만으로 입력받기
Class Name : Prac3.java
<실행결과>
금액 입력 : 5679
금액 : 5679원
천원 : 5장
백원 : 6개
십원 : 7개
일원 : 9개
import java.util.Scanner;
public class Prac3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money;
System.out.print("금액 입력 : ");
money = sc.nextInt();
System.out.println();
System.out.println("금액 : " + money + "원");
System.out.println("천원 : " + money/1000 + "장");
System.out.println("백원 : " + (money%1000)/100 + "개");
System.out.println("십원 : " + (money%100)/10 + "개");
System.out.println("일원 : " + (money%10) + "개");
}
}
증감 연산자(++, --)
- 전위 연산(prefix)
: 연산자(++, --)가 피연산자(n) 보다 앞에 위치하는 경우. 1을 증가(감소)하고 증가(감소)된 값 반환
ex) ++n, --n
- 후위 연산(postfix)
: 연산자(++, --)가 피연산자(n) 보다 뒤에 위치하는 경우. 1을 증가(감소)하고 증가(감소) 전의 값 반환
ex) n++, n--
public class Exam5 {
public static void main(String[] args) {
// 후위 연산 : a++, a--
int a1 = 100;
int x1 = 1;
int y1 = a1+x1++;
System.out.println("x1 = " + x1); // 2
System.out.println("y1 = " + y1); // 101
// 전위 연산 : ++a, --a
int a2 = 100;
int x2 = 1;
int y2 = a2 + ++x2;
System.out.println("x2 = " + x2); // 2
System.out.println("y2 = " + y2); // 102
}
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 6, 7일차 (1) | 2022.09.22 |
|---|---|
| [Java/Eclipse] Java 5일차 (0) | 2022.09.20 |
| [Java/Eclipse] Java 3일차 (0) | 2022.09.16 |
| [Java/Eclipse] Java 2일차 (0) | 2022.09.15 |
| [Java/Eclipse] Java 1일차 (0) | 2022.09.14 |