오늘의 코드
package edu.java.loop03;
import java.util.Scanner;
public class LoopMain03 {
public static void main(String[] args) {
System.out.println("JAVA 은행");
Scanner sc = new Scanner(System.in);
boolean run = true; // while문 종료 여부
int balance = 0;
while (run) {
System.out.println("----------------------------------");
System.out.println("1. 예금 | 2. 출금 | 3. 잔고 | 4. 종료");
System.out.println("----------------------------------");
System.out.println("선택>");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("예금액>");
int money = sc.nextInt();
balance += money;
break;
case 2:
System.out.println("출금액>");
money = sc.nextInt();
balance -= money;
break;
case 3:
System.out.println("잔고>" + balance);
break;
case 4:
System.out.println("종료합니다.");
run = false;
break;
default:
break;
}
}
} // end main()
} // end LoopMain03
package edu.java.array01;
// 배열
// - 같은 타입의 변수들을 하나의 목록으로 저장할 때 사용
// - 배열의 선언과 초기화
// 배열타입[] 배열이름 = new 배열타입[배열길이];
// 배열이믕[인덱스] = 값;
// or
// 배열타입 [] 배열이름 = {값1, 값2, ...}
// - 배열의 인덱스는 0부터 시작하고, 배열의 길이 -1에 끝남.
public class ArrayMain01 {
public static void main(String[] args) {
System.out.println("배열(array)");
int score1 = 100;
int score2 = 90;
int score3 = 80;
int score4 = 70;
System.out.println(score1);
System.out.println(score2);
System.out.println(score3);
System.out.println(score4);
System.out.println("==========");
int[] scores = new int[4];
scores[0] = 90;
scores[1] = 80;
scores[2] = 70;
scores[3] = 60;
for(int i = 0; i < 4; i++) {
System.out.println(scores[i]);
}
} // end main()
} // end ArrayMain01
package edu.java.array02;
public class ArrayMain02 {
public static void main(String[] args) {
System.out.println("배열의 선언, 초기화, 출력");
// int형 정수 10개를 저장할 수 있는 배열 선언
int[] array = new int[10];
System.out.println("배열의 길이 : " + array.length);
// 배열의 인덱스 : 0 ~ {length - 1)
// for문을 사용하여 배열에 1 ~ 10까지의 값을 저장
for(int i = 0; i < 10; i++) {
array[i] = i + 1;
}
// a[0] = 1;
// a[1] = 2;
// ...
// a[9] = 10;
// for문을 사용하여 배열의 모든 값을 출력
for(int i = 0; i < array.length; i++) {
System.out.println("a[" + i + "] = " + array[i]);
}
// for문을 사용하여 배열의 값을 내림차순(10, 9, .. 1)으로 출력
// 인덱스의 변화 9, 8, 7, ... 9
for(int i = array.length - 1 ; i >= 0; i--) {
System.out.println(array[i]);
}
for(int i = 0; i < array.length; i++) {
System.out.println(array[array.length -1 -i]);
}
int n = 0;
// for문을 사용하여 배열의 모든 값(원소)들을 더하기
for(int i = 0; i < array.length; i++) {
n += array[i];
}
System.out.println(n);
} // end main()
} // end ArrayMain02
package edu.java.array03;
import java.util.Scanner;
public class ArrayMain03 {
public static void main(String[] args) {
System.out.println("배열 연습");
Scanner sc = new Scanner(System.in);
// n명의 학생 점수를 입력받아
// 모든 점수, 총합, 평균을 출력(점수는 직접 입력)
// 뽀나스) 최대값, 최소값도 출력
System.out.print("n명 입력 : ");
int n = sc.nextInt();
int[] scores = new int[n];
int sum = 0;
for (int i = 0; i < scores.length; i++) {
System.out.printf((i + 1) + "번 학생의 점수입력 : %n");
scores[i] = sc.nextInt();
// 모든 점수의 합 출력
sum += scores[i];
}
for (int i = 0; i < scores.length; i++) {
// 모든 점수 출력
System.out.println("scores[" + i + "] = " + scores[i]);
}
// 평균 출력하기(소수점까지 표현)
// double avg = ((double) sum) / n;
double avg = (sum / (n * 1.0));
// 점수 최대값 출력
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < scores.length; i++) {
if (scores[i] > max) { // 배열에 저장된 값이 max보다 큰 경우
max = scores[i];
} else if (scores[i] < min) { // 배열에 저장된 값이 min보다 작은 경우
min = scores[i];
}
}
System.out.println("n명의 학생 점수의 총합 : " + sum);
System.out.printf("n명의 학생 점수의 평균 : %.2f%n", avg);
System.out.println("n명의 학생 점수의 최대값 : " + max);
System.out.println("n명의 학생 점수의 최소값 : " + min);
} // end main()
} // end ArrayMain03
package edu.java.array04;
public class ArrayMain04 {
public static void main(String[] args) {
System.out.println("매열 초기화 연습");
int[] english = {100, 90, 80};
System.out.println(english.length);
// for문을 사용한 출력
for(int i = 0; i < english.length; i++) {
System.out.println(english[i]);
}
// for-each 구문(향상된 for 구문)
// for (타입 변수이름 : 배열) {...}
// - 타입은 배열과 동일한 타입으로 선언
// - 배열에 있는 모든 데이터를 접근할 때 사용
for(int x : english) {
System.out.println(x);
}
} // end main()
} // end ArrayMain04
package edu.java.array05;
public class ArrayMain05 {
public static void main(String[] args) {
System.out.println("배열 연습1");
// char 26개를 저장할 수 있는 배열 선언
char[] alphas = new char[26];
// for문을 사용하여 배열에 'a' ~ 'z'까지 저장
for(char ch = 'a'; ch <= 'z'; ch++) {
alphas[ch - 'a'] = ch;
}
for(int i = 0; i < alphas.length; i++) {
alphas[i] = (char) ('a' + i);
}
for(char ch : alphas) {
System.out.print(ch + " ");
}
} // end main()
} // end ArrayMain05
package edu.java.array06;
public class ArrayMain06 {
public static void main(String[] args) {
System.out.println("배열 연습2");
boolean[] arr1 = {true, false, true, false};
for(boolean b : arr1) {
System.out.println(b);
}
System.out.println("===========");
// arr1 배열에 저장되어 있는 값이 true면
// ㄴ "참"이라는 문자열을 배열에 저장
// arr1 배열에 저장되어 있는 값이 false면
// ㄴ "거짓"이라는 문자열을 배열에 저장
String[] arr2 = new String[4];
for(int i = 0; i < arr1.length; i++) {
if(arr1[i] == true) {
arr2[i] ="참";
} else {
arr2[i] = "거짓";
}
}
for(String result : arr2) {
System.out.println(result);
}
} // end main()
} // end ArrayMain06
package edu.java.array07;
public class ArrayMain07 {
public static void main(String[] args) {
System.out.println("배열 연습3");
String test = "문자열입니다.";
System.out.println(test);
System.out.println(test.length());
for(int i = 0; i < test.length(); i++) {
System.out.println(test.charAt(i));
if(test.charAt(i) == '열') {
break;
}
}
// 문자열 배열 선언
String[] subjects = {"국어", "영어", "수학", "프로그래밍"};
// 모든 과목들의 문자열 길이를 출력
// 각 문자열에 "어"라는 글자가 포함되어 있는 경우만
// 문자열 길이를 출력하쇼.
for(int i = 0; i < subjects.length; i++) {
if(subjects[i].contains("어")) {
System.out.println("과목명 : " + subjects[i]);
System.out.println("문자열 길이 : " + subjects[i].length());
}
}
for(String str : subjects) {
if(str.equals("프로그래밍")) { // 왠만해선 equals를 사용하자
System.out.println("재밌다!");
System.out.println(str.length());
}
}
} // end main()
} // end ArrayMain07