public class Practice01StringPrint {
public static void main(String[] args) {
// TODO 연습문제 풀이
/*
* 문자열 변수 str을 선언과 동시에 “HELLO JAVA”값으로 초기화 하고 아래와 같이 변수 str의 값을 출력하는 코드를 작성하시오.
*/
String str;
str = "Hello Java";
System.out.println(str);
/*
* System.out의 함수
* println(); // print line-feed = 출력 후 줄바꿈
* print(); // 출력 후 줄바꿈을 하지 않음
* printf(); // 지정된 패턴(format)에 따라 출력
*/
/*
* escape sequence(이스케이프 문자)
* \'
* \"
* \n
* \t
* \\
*/
System.out.println("\"최기근\"님의 문서를 인용");
System.out.print("Hello ");
System.out.print("Java! \n");
System.out.print("안녕하세요!");
// printf - 형식화된 지시어를 통해 출력
// 줄바꿈이 이루어지지 않음
// 형식으로 지정된 지시어의 수 만큼 값을 타입에 맞게 전달 해야함.
// "%d" - format안에서 %기호를 이용하여 지시어를 지정
// 지시자
// %d - int(decimal) 10진법의 정수 형식
// %b - boolean
// %o - 8진법으로 표현
// %x - 16진법으로 출력
// %f - float(부동소수점) - 실수
// %c - Character - 문자
// %s - string 문자열
// %n - 줄바꿈(line-feed, carriage return)
// %d, %f, %s를 많이 쓰는 편
int score = 70;
String name = "철수";
System.out.println(name+"님의 점수는"+score);
System.out.println(); //매개변수가 없으면 줄바꿈만 출력
// System.out.print();
System.out.printf("%s님의 점수는 %d입니다.",name,score);
//사용할 수 있는 범위 digit
// 정수 및 실수를 표현 하는 방식 제공
/*
* %[-][0][n][.m][,n]
* %[-] : 전체 자리수가 지정되어 있을 때 왼쪽으로 정렬
* 없을 경우 오른쪽으로 정렬
* ex) %-5d, 1000 == 1000[]
* %[n] : 출력할 전체 자리수를 지정
* ex) %5d, 1000 ==[]1000
* %[0] : 전체 자리수가 지정되어 있을 때 빈자리를 공백이 아닌 0으로 채움
* ex) %05d, 1000 == 01000
* %[.m] : 소수점 아래 자리수를 지정.
* 잘리는 소수점 자리는 반올림
* ex) %.2f , 1234.123 == 1234.12
* ex) %.2f , 1234.127 == 1234.13
* %[,n] : 정수의 자리수를 ,로 표현
* ex) %,20d 100000000 == 100,000,000
*
* *[-][0] 은 정수 에서만 사용가능(%d)
*/
String book = "혼자 공부하는 자바";
int price = 10000000;
System.out.printf("%n%s의 교재는 %,020d입니다. %n",book,price);
System.out.printf("%n%s의 교재는 %20d입니다. %n",book,price);
// 소수점 자리
float number = 3.141592f;
System.out.printf("원주율 파이는 %f입니다. %n",number);
System.out.printf("원주율 파이는 %.2f입니다.\n",number);
System.out.printf("원주율 파이는 %.3f입니다.\n",number);
//번외 - 오류출력
System.err.println("니가 잘못했네.");
}
}
//import java.util.Scanner;
import java.util.*;
public class Practice02Scanner {
public static void main(String[] args) {
// 사용자에게 console을 통하여 지정된 타입의 값을 입력받는 class
// System.in == 연결된 운영체제의 command line을 통해 입력
// java.util.Scanner sc = new java.util.Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.println("정수를 입력해 주세요 >");
boolean isUse = sc.hasNextInt(); // has : 입력받은 값을 가지고 검증하는 절차
int result = 0;
if(isUse) {
// 정수 입력
result = sc.nextInt();
}else {
// 정수가 아님.
sc.next(); //사용자가 입력한 값 문자열 형태로 빼기. -> 다음 실행문에 영향 가지 않기 위해서.
}
System.out.printf("입력받은 정수값 %d \n",result);
System.out.println("실수를 입력해 주세요 > ");
double d = sc.nextDouble();
System.out.printf("입력받은 실수값 %f \n",d);
// System.out.println("단어를 입력해주세요.");
// String word = sc.next();
// System.out.printf("입력받은 단어 %s %n",word);
//
//
// System.out.println("문장을 입력해주세요 > ");
// String strs = sc.nextLine();
// System.out.println(strs);
//next, nextLine 두개 같이 쓰면 뒤에꺼 실행 안되니까 동시에 사용하는 경우 스캐너 따로 따로 만들어줘야함.
System.out.println("실행 여부를 작성해 주세요 > ");
boolean isRun = sc.nextBoolean();
System.out.println("실행여부확인 : "+isRun);
System.out.println("Main Method End");
}//main end
}
import java.util.Scanner;
public class Practice03Scanner {
public static void main(String[] args) {
/*
* - 실수를 저장하는 radius 변수선언 (반지름)
- 실수를 저장하는 area 변수 선언 (원의면적)
- 사용자에게 실수를 입력 받는다 – 값을 받아서 radius 에 대입(Scanner 이용)
- area 변수에 사용자에게 입력받은 radius를 이용하여 원의 면적을 구하여 값을 대입
- area 값을 출력한다. (원의 면적 : 반지름 * 반지름 * 원주율(3.14))
*/
double radius, area;
System.out.println("반지름을 입력해 주세요 >");
// ctrl + shift + o //자동임폴트
Scanner sc = new Scanner(System.in);
radius = sc.nextDouble();
double pi = 3.14;
area = radius * radius * pi;
System.out.println(area);
//1~10까지의 합을 출력
int sum = 1+2+3+4+5+6+7+8+9+10;
System.out.println("1 2 3 4 5 6 7 8 9 10");
System.out.println("합: " + sum);
// 1~20까지의 정수의 합을 출력
// n = n + m
sum = 0;
// for - 반복횟수가 정해져 있을 때
// while - 반복문의 종료 시점을 알 수 없을 때
// 1에서 시작하여 20까지의 수를 1씩 증가시키면서 도출
// for(초기화식 ; 탈출 조건식; 증감식)
//for(int i = 1; i<11; i++) {
for(int i=1; i <=20; i++) {}
//for문이 끝나더라도 i값 사용해야 할 때 이렇게 사용 가능.
int i = 1;
for(;i<=20;i++) {
}
//무한 반복되는 실행문
// for(;;) {}
for(i=1; i<21; i++) {
System.out.print(i+ " ");
sum = sum+i;
}
System.out.println();
System.out.println("합은 : "+sum);
//1~100까지의 정수 중 홀수의 합을 구하시오.
int odd = 0;
for(i = 1; i <=100; i++) {
if(i%2==1) odd+=1;
}
System.out.println("홀수의 총합 : " + odd);
//2번째 방법
odd = 0;
for(i=1;i<101;i+=2) {
odd+=i;
}
System.out.println("홀수의 총합 : " + odd);
}
}
import java.util.Scanner;
public class Practice04Control {
public static void main(String[] args) {
// 사다리꼴의 넓이 구하기(윗변+밑변)*높이 / 2
// 소수점 자리까지 출력
int top = 5;
int bottom = 10;
int height = 7;
// 나누기 연산의 좌항과 우항 둘 중 하나라도 실 수 타입이어야
// 실수로 결과가 도출됨.
double area = (top + bottom) * height / 2.0;
System.out.println("사다리꼴의 넓이 : "+ area);
//급여명세서
//import
Scanner sc = new Scanner(System.in);
System.out.println("사원명을 입력해주세요 > ");
String 사원명 = sc.next();
System.out.println("시급을 입력해주세요 > ");
int 시급 = sc.nextInt();
System.out.println("근무시간을 입력해주세요 > ");
int 근무시간 = sc.nextInt();
int 급여금액 = 시급 * 근무시간;
int 공제금액 = 급여금액 * 3 / 100;
공제금액 = (int)(급여금액 * 0.03);
int 실지급액 = 급여금액 - 공제금액;
System.out.println("=========급여명세서=========");
System.out.println("사원명 : " + 사원명);
System.out.println("급여금앱 : " + 급여금액);
System.out.println("공제금액 : " + 공제금액);
System.out.println("실지급액 : " + 실지급액);
// 입력받은 두개의 정수 값 중 큰 수를 출력
System.out.println("첫번째 정수 : ");
int first = sc.nextInt();
System.out.println("두번째 정수 : ");
int second = sc.nextInt();
if(first > second) {
System.out.println("큰 수는 : "+ first);
}else if(first < second) {
System.out.println("큰 수는 : "+second);
}else {
System.out.println("두 수의 크기가 같습니다.");
}
}
}
import java.util.Scanner;
public class Practice05Average {
public static void main(String[] args) {
// 5개의 정수를 입력받아 sum이라는 변수에 저장
// 누적 지정된 점수를 통해 학생들의 평균 출력
int sum = 0;
int studentCount = 5;
// studentCount = 6; // 재할당 불가
// Scanner sc = new Scanner(System.in);
//import
Scanner sc = new Scanner(System.in);
for(int i =0;i < studentCount;i++) {
System.out.println("성적을 입력하시오 :");
sum += sc.nextInt();
}
System.out.printf("평균성적은 %d입니다.",(sum/studentCount));
// System.out.println("성적을 입력하시오 :");
// sum += sc.nextInt();
//
}
}
import java.util.Scanner;
public class Practice06Score {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = 0;
boolean isRun = true; // while 문 탈출 flag
while(isRun) {
System.out.println("점수를 입력해 주세요 > ");
if(!sc.hasNextInt()) {
//isRun = false; // 반복문 안의 내용이 실행 보장이 되어야 할 때 flag사용해서 모든 연산을 하고 프로그램 종료하게 함.
break; // 즉시탈출해야할때.
/*
sc.next();
continue; // continue만나면 아랫쪽은 수행하지 않고 반복문으로 돌아가는 것.
*/
}
score = sc.nextInt();
// 조건문 : if, switch
score = score / 10;
switch(score) {
//break 안걸리면 계속 출력되니까 아래처럼 10일때 코드 단축 가능
case 10 : case 9 :
System.out.println("A학점");
break;
case 8 :
System.out.println("B학점");
break;
case 7 :
System.out.println("C학점");
break;
case 6 :
System.out.println("D학점");
break;
default :
System.out.println("F학점");
}
}// end score while
//gugudan 출력
for(int j = 9; j>1;j--) {
for(int i = 9; i> 1; i--) {
System.out.printf("%d * %d = %d\t",j,i,(j*i));
}
System.out.println();
}
}//end main
}
import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //import
//while 탈출 flag
boolean isRun = true;
// 사용자의 예금을 저장할 변수
int money = 0;
//while문 이름 지정
giguen : while(isRun) {
System.out.println("=========================");
System.out.println("1.예금 |2.출금|3.잔액확인|4.종료");
System.out.println("=========================");
System.out.println("번호를 입력해 주세요(1~4) > ");
int selectNo = sc.nextInt();
switch(selectNo) {
case 1:
System.out.println("예금");
System.out.println("입금하실 금액을 입력하세요");
int deposit = sc.nextInt();
money = money + deposit;
// money += deposit;
System.out.println(deposit+"원이 입금 완료되었습니다.");
break;
case 2:
System.out.println("출금");
System.out.println("출금하실 금액을 입력해 주세요.");
int withdrawal = sc.nextInt();
if(withdrawal > money ) {
System.err.println("출금 금액이 예금된 금액보다 클 수 없습니다.");
continue giguen; // 지정된 이름의 반복문 위치로 돌아간다.
}
money -= withdrawal;
System.out.println(withdrawal+"원이 출금 되었습니다.");
break;
case 3:
System.out.println("잔액확인");
System.out.printf("잔액 : %d원 입니다. \n",money);
break;
case 4:
System.out.println("종료");
// isRun = false; //switch문이라서 break로는 종료가 안됨.
break giguen; // 지정된 이름의 반복문을 종료한다.
default :
System.out.println("사용할 수 없는 기능입니다.");
System.out.println("다시 입력해주세요.");
}
}
}
}
'Java' 카테고리의 다른 글
04.21 - 메소드, 클래스 (0) | 2023.04.21 |
---|---|
04.20 - 배열 (0) | 2023.04.20 |
선생님 새로 오심 (0) | 2023.04.17 |
2023.04.14 null값 처리, (0) | 2023.04.14 |
2023.04.13. 배열, 메소드 (0) | 2023.04.13 |