public class DoWhileMain {
public static void main(String[] args) {
int su = 0;
String str = "Hello World";
//선 처리 후 비교
do {
System.out.println(str); // 무조건 한 번 실행했기 때문에 일반 while보다 한 번 더 출력하게 된다.
}
while(su++ < 5);
System.out.println("============");
//선 비교 후 처리
int su2 = 0;
while(su2++ < 5) {
System.out.println(str);
}
}
}
public class ScoreMain {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int kor, eng, math, sum;
char grade;
float avg; //실수 실수 연산하기!
do {
System.out.print("국어 : ");
kor = input.nextInt();
}
while ( kor< 0 || kor >100 );
do {
System.out.print("영어 : ");
eng = input.nextInt();
}
while ( eng < 0 || eng >100 );
do {
System.out.print("수학 : ");
math = input.nextInt();
}
while ( math < 0 || math >100 );
//총점 구하기
sum = kor + eng + math ;
//평균 구하기
avg = sum / 3.0f ;
//등급 구하기
switch((int) (avg / 10)) {
case 10:
case 9:
grade = 'A'; break;
case 8:
grade = 'B'; break;
case 7:
grade = 'C'; break;
case 6:
grade = 'D'; break;
default:
grade = 'F';
}
//성적표 출력하기
System.out.println("총점은 " + sum+ "입니다.");
System.out.printf("평균은 %.2f입니다.%n" , avg);
System.out.printf("%c 학점입니다." , grade);
input.close();
}
}
public class BreakMain01 {
public static void main(String[] args) {
// break 는 switch 블럭을 빠져나갈 때, 반복문에서 특정 조건일 때 반복문을 빠져나가는 용도로 사용함
int n = 1;
while (n<=10) {
System.out.println(n);
n++;
if (n == 8)
break;
}
}
}
public class BreakMain02 {
public static void main(String[] args) {
// 다중 반복문에서 break 사용하기
for(int i = 0; i < 3; i ++){
for (int j = 0; j < 5; j++) {
if ( j == 3) {
/*
* 특정 조건일 때 반복문을 빠져나감
* 다중 반복문일 때 전체 반복문을 빠져나가는 것이 아니라
* break가 포함되어있는 반복문만 빠져나간다.
*/
break;
}
System.out.println( i + "," + j);
}
}
}
}
public class BreakMain03 {
public static void main(String[] args) {
// 다중 반복문에서 break 사용하기
exit_for : // break label
for(int i = 0; i < 3; i ++){
for (int j = 0; j < 5; j++) {
if ( j == 3) {
break exit_for;
}
System.out.println( i + "," + j);
}
}
}
}
public class ContinueMain {
public static void main(String[] args) {
for(int i = 0; i <= 10; i++) {
//특정 조건일 때 수행문의 실행을 멈추고 다음 반복 회차로 건너뜀
if(i%3==0) {
//3의 배수
continue;
}
System.out.println(i);
}
}
}
======== 현재 자판기 정보 ===========
커피 양 :
프림 양:
설탕 양 :
자판기 보유 동전 금액 : 90
투입한 동전 금액 : 500원
============================
public class CoffeeMain02 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int price = 400; // 커피 가격
//커피 한 잔에 들어갈 양
int content_coffee = 5;
int content_cream = 3;
int content_sugar = 1;
int coffee = 10 , cream = 10 , sugar = 10; // 보유 커피, 프림, 설탕
int change;
int payment; // 고객이 낸 동전
int amount = 0; //투입한 금액이 누적되는 곳
int coin = 1000; // 자판기 보유 동전
while(true) {
System.out.println("==== 커피 자판기 ====");
System.out.print(" 1 . 커피 2 . 종료 >");
int num = input.nextInt();
if(num==1) {
System.out.print("동전을 넣으세요 (커피값" + price + "원):");
payment = input.nextInt();
//거스름돈 계산
change = payment - price;
if(coffee < content_coffee) {
System.out.println("커피가 부족합니다.");
continue;
}
if(cream < content_cream) {
System.out.println("크림이 부족합니다.");
continue;
}
if(sugar < content_sugar) {
System.out.println("설탕이 부족합니다.");
continue;
}
if ( payment < price) {
System.out.println("투입한 동전이 부족합니다.");
continue;
}
if ( coin < change) {
System.out.println("거스름돈이 부족합니다.");
continue;
}
//커피 구매 가능하기 때문에 연산
coffee -= content_coffee; //커피 차감
cream -= content_cream; //커피 차감
sugar -= content_sugar; //커피 차감
coin -= change; //거스름돈 차감
amount += payment; // 투입 금액 누적
System.out.printf("거스름돈 : %,d원%n" , change);
System.out.println("맛 좋은 커피가 준비되었습니다.");
System.out.println("====== 현재 자판기 정보 ======");
System.out.printf("커피 : %d%n" , coffee);
System.out.printf("프림 : %d%n" , cream);
System.out.printf("설탕 : %d%n" , sugar);
System.out.printf("자판기 보유 동전 금액 : %d%n" , coin);
System.out.printf("투입된 동전 금액 : %d%n", amount);
System.out.println("=====================");
}
else if (num == 2) {
System.out.println("자판기 이용이 종료되었습니다.");
System.out.println("다음에도 이용해 주세요!");
break;
}
else
System.out.println("잘못 입력하였습니다.");
}
input.close();
}
}
public class ArrayMain01 {
public static void main(String[] args) {
//배열 선언
char [] ch; // char [] => 배열의 자료형
//배열 생성
ch = new char [4]; // char [4] => 배열의 길이를 의미함 방이 4개!
//마지막 index의 수는 (길이 - 1) 을 구하면 알 수 있음
//배열 초기화
ch[0] = 'J';
ch[1] = 'a';
ch[2] = 'v';
ch[3] = 'a';
// 배열의 요소 출력
System.out.println(ch[0]);
System.out.println(ch[1]);
System.out.println(ch[2]);
System.out.println(ch[3]);
//반복문을 이용한 배열의 요소 출력
for (int i = 0; i < 4; i++) {
//4는 배열의 길이
System.out.println("ch[" + i + "] : " + ch[i]);
}
//배열의 선언 및 생성
int[] it = new int[6];
// 배열의 선언 및 생성(명시적 배열 생성), 초기화
char[] ch2 = new char[] {'J','a','v','a'};
//배열의 선언 및 생성(암시적 배열 생성), 초기화
char[] ch3 = {'자', '바'};
}
}
public class ArrayMain02 {
public static void main(String[] args) {
//정수형 배열 선언 및 생성
//배열을 생성하면 배열의 요소로 기본값이 저장 됨
// 정수형 배열을 0을 기본값으로 배열을 생성
int[] array = new int[5];
//반복문을 이용한 요소의 출력
for(int i = 0; i < array.length; i ++) {
System.out.println(" array[" +i+ "]:" + array[i]); // 변수를 메모리에 생성하면 데이터가 없다 그러나 배열은 존재함
}
System.out.println("===========");
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
//array[5] = 60;
//없는 인덱스를 호출하면 컴파일은 되지만 실행시 오류 발생
for(int i = 0; i < array.length; i ++) {
System.out.println(" array[" +i+ "]:" + array[i]); // 변수를 메모리에 생성하면 데이터가 없다 그러나 배열은 존재함
}
}
}
public class Arraymain03 {
public static void main(String[] args) {
//배열 선언 및 생성 (암시적 배열 생성), 초기화
int [] array = {10,20,30,40,50};
//반복문을 이용한 배열의 요소 출력
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
System.out.println("\n------------------------");
//확장 for문을 이용한 배열의 요소 출력
for(int num : array) {
System.out.print(num + "\t");
}
}
}
public class ArrayMain04 {
public static void main(String[] args) {
int sum = 0;
float avg = 0.0f;
//배열 선언, 생성, 초기화
int[] score = {100,88,88,100,90};
//총점 구하기
for( int i = 0; i < score.length; i++ ) {
sum += score[i];
}
avg = (float) sum / score.length;
System.out.println(sum);
System.out.println(avg);
}
}
public class ArrayMain05 {
public static void main(String[] args) {
int [] score = {79, 88 , 91 , 33, 100, 55, 95};
int max = score [0] ; //최대값이 저장되는 변수
int min = score [0]; //최소값이 저장되는 변수
for (int i = 1; i < score.length; i++ ) {
if(score[i] > max) {
max = score[i];
}
else if (score [i] < min) {
min = score[i];
}
}
System.out.println(max);
System.out.println(min);
}
}
public class ArrayMain06 {
public static void main(String[] args) {
// 문자열 배열
String [] array = new String [3];
array [0] = "JAVA" ;
array [1] = "Oracle";
array [2] = "jsp";
//반복문을 잉요한 배열의 요소 출력
for(int i = 0 ; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("==============");
//확장 for문을 이요한 배열의 요소 출력
for(String str : array) {
System.out.println(str);
}
}
}
public class ArrayMain07 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
String[] course = {"국어" , "영어", "수학"};
int [] score = new int[course.length]; // 0 : 국어 1: 영어 3: 수학
int sum = 0;
float avg =0.0f;
// 반복문을 이용하여 입력 처리
for(int i = 0; i < score.length; i++) {
//입력값을 0~100 만 가능하도록 조건을 만들어준다.
do {
System.out.print(course[i] + "=");
score[i] = input.nextInt();
}
while(score[i]<0 || score[i]>100);
sum += score[i];
}
// 평균 구하기
avg = sum / (float) (course.length);
System.out.println();
//과목의 점수 출력
for(int i = 0; i<score.length; i++) {
//과목 명 ,점수
System.out.printf("%s = %d%n", course[i], score[i]);
}
System.out.printf("총점은 %d입니다.%n",sum);
System.out.printf("평균은 %.2f입니다.", avg);
input.close();
}
}