package condition;
public class Main03_1 {
public static void main(String[] args) {
//1. 만약 3000원 이상의 돈을 가지고 있으면 "택시를 타고 가라"를 출력, 그렇지 않으면 "걸어가라"를 출력
int money = 2000;
if ( money >=3000) {
System.out.println("택시");
}else {
System.out.println("걸어가라");
}
System.out.println("--------------");
//2. 만약 3000원 이상 있거나, 카드가 있다면 "택시를 타고 가라"를 출력, 그렇지 않다면 "걸어가라"를 출력
int money2 = 1000;
boolean hasCard = false;
if (money2 >= 3000 || hasCard ) {
System.out.println("택시");
} else {
System.out.println("걸어가라");
}
System.out.println("--------------");
//3. int a = 숫자; 어떤 특정 정수값 a가 짝수이면 "짝수", 홀수이면 "홀수"를 출력
int a = 5;
if ( a%2 == 0) {
System.out.println("짝수");
} else {
System.out.println("홀수");
}
System.out.println("--------------");
//4. 서로 다른 특정 정수값 a1 = 10, b1 = 20, c1 = 9이 있다.
// 이 중에서 최대값을 구하여라
int a1 = 10, b1 = 20, c1 = 9;
int max = 0;
if (a1 > b1 && a1 > c1 ) {
max = a1;
} else {
if(b1 > c1) {
max = b1;
} else {
max = c1;
}
}
System.out.println(max);
System.out.println("--------------");
// 5. 국어, 영어 수학 점수의 평균이 95점 이상이면 "장학생" 출력
int kor = 95, eng = 100, mat = 100;
int avg = (kor + eng + mat) /3 ;
System.out.println(avg);
if (avg >= 95) {
System.out.println("장학생");
}
System.out.println("--------------");
//6. 숫자가 3이면 "안녕"이 세줄, 2이면 "안녕"이 두줄, 1이면 "안녕"이 한줄, 그 외에는 "잘가" 출력 (switch문)
int num = 3;
switch (num) {
case 3:
System.out.println("안녕");
case 2:
System.out.println("안녕");
case 1:
System.out.println("안녕");
break;
default :
System.out.println("잘가");
}
System.out.println("--------------");
//7. 12시보다 작으면 오전, 크면 "오후"로 출력 (삼항 연산자)
int hour = 16;
String ampm = hour < 12 ? "오전" : "오후";
System.out.println(ampm);
}
}
package multiex;
public class Main01 {
public static void main(String[] args) {
int point = 78;
if (point > 70 && point <= 80 ) {
System.out.println("C+");
} else if ( point < 74 ) {
System.out.println("C-");
} else {
System.out.println("C0");
}
}
}
package multiex;
public class Main02 {
public static void main(String[] args) {
// 1부터 100 사이의 정수 중에 짝수의 합과, 홀수의 합을 각각 구하여라
int a ;
int result1 = 0, result2 = 0;
for (a=0; a<101; a++) {
if (a%2==0) {
result1 += a;
}
}
System.out.println("짝수의 합 : " + result1);
for (a=0; a<101; a++) {
if (a%2 != 0) {
result2 += a;
}
}
System.out.println("홀수의 합 : " + result2);
int sum = result1 + result2;
System.out.println("총합 : " + sum);
}
}
package multiex;
public class Main01 {
public static void main(String[] args) {
// 1부터 100 사이의 정수 중에 짝수의 합과, 홀수의 합을 각각 구하여라
int a ;
int result1 = 0, result2 = 0;
for (a=0; a<101; a++) {
if (a%2==0) {
result1 += a;
}
}
System.out.println("짝수의 합 : " + result1);
for (a=0; a<101; a++) {
if (a%2 != 0) {
result2 += a;
}
}
System.out.println("홀수의 합 : " + result2);
int sum = result1 + result2;
System.out.println("총합 : " + sum);
/*
* int sum1 = 0;
* int sum2 = 0;
*
* for ( int i=1; i<=100; i++ ) {
* // 조건식
* if ( i%2 == 0 ) { //짝수
* sum1 += i;
* } else { //홀수
* sum2 += i;
* }
* }
* System.out.println("짝수의 합 : " + sum1);
* System.out.println("홀수의 합 : " + sum2);
*/
}
}
package multiex;
public class Main02 {
public static void main(String[] args) {
// 1 ~ 100 중에서 홀수들의 합
// 무한 루프, continue; break; 사용해서 홀수들의 합 출력
int sum = 0;
int i = 0;
while (true) {
i++;
if ( i%2 == 0 ) {
continue;
}
if ( i>100 ) {
break;
}
sum += i;
}
System.out.println("홀수의 합 : " + sum);
}
}
package multiex;
public class Main03 {
public static void main(String[] args) {
// 1. 구구단 2단 출력
int gugu = 2;
for (int i=1; i<10; i++) {
System.out.println("gugu * " + i + " = " + gugu*i);
}
// 2. while문으로만
int treeHit = 0;
while (treeHit < 10 ) {
treeHit++;
System.out.println("나무를 " + treeHit +"번 찍었습니다.");
if (treeHit == 10 ) {
System.out.println("나무 넘어갑니다.");
}
}
}
}
package multiex;
public class Main04 {
public static void main(String[] args) {
// 1. 이중 for문을 사용해서 구구단 2단 ~ 9단까지의 결과값 출력
for(int i=2; i<=9; i++) { //단에 대한 for문
for(int j=1; j<10; j++) {
int result = i * j;
System.out.println(result);
}
System.out.println("-------------");
}
System.out.println("================");
// 2. 이중 while문을 사용해서 구구단 2단 ~ 9단까지의 결과값 출력
int i=2;
int j=1;
while (i <=9 ) {
while ( j < 10 ) {
System.out.println(i*j);
j++;
}
System.out.println("----------");
i++;
j=1; //중요
}
}}
package multiex;
public class Main01 {
public static void main(String[] args) {
// 1. * 8행8열
for( int i = 0; i<=7; i++ ) {//행
for(int j=0; j<=7; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("===============");
// 2. * 8개에서 하나씩 감소
for (int i=0; i<=7; i++) {
for (int j=i; j<=7; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("===============");
// 3. * 하나에서 하나씩 증가
for(int i=7; i>=0; i--) {
for (int j=i; j<=7; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}