1. 국어 영어 수학 0~100사이여야 한다.
총점 평균
등급 A 90~100
B 80~90
C 70~80
D 60~70
F 0~60
* 0~100이 아닐때
결과 폼
이름 국어 영어 수학 총점 평균 등급
이말자 90 100 90 280 93.33 A
1-1) if
public class Ex1_1 {
public static void main(String[] args) {
String name = "이말자";
int kor = 90;
int eng = 100;
int math = 90;
int sum = kor + eng + math;
double average = sum / 3.;
String resultString = "";
if (average >100) {
System.out.println("잘못된 입력입니다.");
return;
}else if (average >= 90) {
resultString = "A";
} else if (average >= 80) {
resultString = "B";
} else if (average >= 70) {
resultString = "C";
} else if (average >= 60) {
resultString = "D";
} else if (average < 60) {
resultString = "F";
}
System.out.printf("이름 국어 영어 수학 총합 평균 등급 \n%s %d %d %d %d %.2f %s",
name, kor, eng, math,
sum, average, resultString);{
}
}
}
1-2) switch~case 사용
public class Ex1_2 {
public static void main(String[] args) {
String name = "이말자";
int kor = 90;
int eng = 100;
int math =100;
int sum = kor + eng + math;
double average = sum / 3.;
String grade = "입력오류";
switch((int)average/10) {
case 9 :
grade = "A";
break;
case 8 :
grade = "B";
break;
case 7 :
grade = "C";
break;
case 6 :
grade = "D";
break;
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :
grade = "F";
break;
}
System.out.printf("이름 국어 영어 수학 총합 평균 등급 \n%s %d %d %d %d %.2f %s",
name, kor, eng, math,
sum, average, grade);
//https://coding-factory.tistory.com/130형변환 캐스팅 방법
}
}
2. ★를 다음과 같이 출력한다.
2-1) ★★★★★★
★★★★★
★★★★
★★★
★★
★
public class Ex2_1 {
public static void main(String[] args) {
for(int count = 1; count <= 6; count++) {
String stars = "";
for(int col = count; col <= 6; col++) {
stars += "★";
}
System.out.println(stars);
}
}
}
2-2) ★
★★
★★★
★★★★
★★★★★
★★★★★★
public class Ex2_2 {
public static void main(String[] args) {
for(int count = 1; count <= 6; count++) {
String star = "";
for(int col = 1; col <= count; col++) {
star += "★";
}
System.out.println(star);
}
}
}
3. 다음과 같이 출력
3-1)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
public class Ex3_1 {
public static void main(String[] args) {
int[][] arr = new int[5][5];
int num = 1;
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[0].length; j++) {
arr[i][j] = num;
System.out.print(arr[i][j]+" ");
num++;
}
System.out.println();
}
}
}
3-2)
A~z까지 출력
옆으로 출력 개행 하지 말고
public class Ex3_2 {
public static void main(String[] args) {
char aString = 65;
while(true) {
if(aString == 91)
aString = 97;
String str = String.valueOf(aString);
System.out.print(str);
aString++;
if(aString > 122)
break;
}
}
}
3-3)A~z까지 출력
ASC||코드에서 대문자와 소문자 사이에 있는 특수문자를 빼고 출력해라.
A~Z출력하고
개행하고
a~z출력
public class Ex3_3 {
public static void main(String[] args) {
char aString = 65;
while(true) {
if(aString == 91)
aString = 97;
String str = String.valueOf(aString);
System.out.print(str);
aString++;
if(aString > 90)
if(aString < 97)
System.out.println("");
if(aString > 122)
break;
}
}
}
3-4) ABCDEF
GHIJKL
MNOPQR
STUVWX
YZ
abcdef
ghijkl
mnopqr
stuvwx
yz
public class Ex3_4 {
public static void main(String[] args) {
char[] arr = new char[53];
int j = 65;
for(int i = 1; i<53; i++) {
arr[i-1] = (char)j;
System.out.print(arr[i-1]);
j++;
if(j == 91)
j =97;
if(i%5 == 0) System.out.println("");
if(j > 91)
if(j < 98)
System.out.println("");
if(j > 122)
break;
}
}
}
3-5) 구구단 4단 출력
4단
4 X 1 = 4
.
.
.
.
4 X 9 = 36
public class Ex3_5 {
public static void main(String[] args) {
int i = 4;
System.out.println("["+i+" 단]");
for(int j = 1; j <=9 ; j++) {
System.out.println(i+"X"+j+"="+(i*j));
}
}
}
3-6) 구구단을 출력하세요
2단
3단
4단
.
.
.
.
9단
public class Ex3_6 {
public static void main(String[] args) {
for(int i = 2; i <= 9; i++) {
System.out.print("["+i+" 단]\t");
for (int j = 1; j <= 9; j++) {
System.out.print(i+"x"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
3-7) 구구단을 출력하세요
2단 3단 4단 ~~~~~~ 9단
2X1=2 3X1=3 4X1=4 9X1=9
.
.
.
.
2X9=18 3X9=27 4X9=36 9X9=81
public class Ex3_7 {
public static void main(String[] args) {
for(int i = 2; i <= 9; i++) {
System.out.print("["+i+" 단]\t");
}
System.out.println();
for(int i = 1; i <= 9; i++) {
for (int j = 2; j <= 9; j++) {
System.out.print(j+"x"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
3-8) 구구단을 출력하세요
2단 3단
2X1=2 3X1=3 .
.
.
.
2X9=18 3X9=27
4단 5단
4X1=4 5X1=5
.
.
.
4X9=36 5X9=45
.
.
.
9단까지 표현.
public class Ex3_8 {
public static void main(String[] args) {
for (int i = 2; i <= 9; i += 2) {
System.out.println("["+i+"단]\t\t"+"["+(i+1)+"단]");
for(int j = 1; j <= 9; j++) {
System.out.printf("%d x %d = %d\t", i,j,i*j);
System.out.printf("%d x %d = %d\t", i+1, j, (i+1)*j);
System.out.println();
}
System.out.println();
}
}
}
3-9) 201 202 203 204
101 102 103 104
출력
public class Ex3_9 {
public static void main(String[] args) {
int a = 300;
for(int i = 0; i < 2; i++) {
for(int j = 1; j < 5; j++) {
System.out.print(++a + "\t");
}
a = a - 104;
System.out.println();
}
}
}
3-10) 4! 을 구하라.
4x3x2x1
public class Ex3_10 {
public static void main(String[] args) {
int n = 4;
int k = 1;
for(int i = n; i >= 1; i--) {
k = k*i;
}
System.out.println(n + "! = "+ k);
}
}
3-11) 345,780원
10만원권 3장
5만원권 0장
1만원권 4장
5천원권 1장
1천원권 0장
5백원권 1개
1백원권 2개
5십원권 1개
1십원권 3개
public class Ex3_11 {
public static void main(String[] args) {
int t = 345780;
int i = t / 100000;
t = t % 100000;
if(t > 0) System.out.println("10만원권 : "+i);
i = t / 50000;
t = t % 50000;
if(t > 0) System.out.println("5만원권 : "+i);
i = t / 10000;
t = t % 10000;
if(t > 0) System.out.println("1만원권 : "+i);
i = t / 5000;
t = t % 5000;
if(t > 0) System.out.println("5천원권 : "+i);
i = t / 1000;
t = t % 1000;
if(t > 0) System.out.println("1천원권 : "+i);
i = t / 500;
t = t % 500;
if(t > 0) System.out.println("5백원권 : "+i);
i = t / 100;
t = t % 100;
if(t > 0) System.out.println("1백원권 : "+i);
i = t / 50;
t = t % 50;
if(t > 0) System.out.println("50원권 : "+i);
i = t / 10;
t = t % 10;
if(t >= 0) System.out.println("10원권 : "+i);
}
}
3-12) 생년월일 이용
현재 년월일 이용
몇 일 살았나 계산
단, 원시적으로. api사용하지 않고
결과폼
오늘은 2020.01.23 입니다 당신의 생일은 2020.01.20 이므로 3일살았습니다.
import java.util.*;
import java.text.*;
public class Ex3_12 {
public static void main(String[] args) {
Date today = Calendar.getInstance().getTime();
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
Scanner sc = new Scanner(System.in);
int currYear = Integer.parseInt(yearFormat.format(today));
int currMonth = Integer.parseInt(monthFormat.format(today));
int currDay = Integer.parseInt(dayFormat.format(today));
int birthYear = 0;
int birthMonth = 0;
int birthDay = 0;
int dayCount = 0;
System.out.print("태어난 년을 입력하세요 : ");
birthYear = sc.nextInt();
System.out.print("태어난 웡을 입력하세요 : ");
birthMonth = sc.nextInt();
System.out.print("태어난 일을 입력하세요 : ");
birthDay = sc.nextInt();
dayCount = currDay - birthDay;
dayCount = dayCount + ((currMonth * 30) - (birthMonth * 30));
dayCount = dayCount + ((currYear * 365) - (birthYear * 365));
System.out.println("당신의 생일은 "+birthYear+"/"+birthMonth+"/"+birthDay+" 이므로 "+dayCount+"일 살았습니다.");
}
}
'JAVA' 카테고리의 다른 글
this, this(), super, super() (1) | 2020.05.08 |
---|---|
접근제어자 (0) | 2020.05.08 |
반복문 (1) | 2020.04.29 |
제어문(feat. 삼항연산자) (1) | 2020.04.28 |
별찍기, 구구단, 화폐단위 계산 (1) | 2020.04.28 |