๋ฐ๋ณต์ ์ฌ๋ฌ๋ฒ ๋ฐ๋ณตํ ๋์๋ ๋ฐ๋ณต๋ฌธ ์์ ๋ฐ๋ณต๋ฌธ์, ์ฆ ์ค์ฒฉ ๋ฐ๋ณต๋ฌธ์ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค. 3์ผ์ฐจ์๋ ์ค๋ช ํ๋ฏ, ๋ด๋ถ ๋ฐ๋ณต๋ฌธ, ์ธ๋ถ ๋ฐ๋ณต๋ฌธ์ด ์์ด '์ธ๋ถ ๋ฐ๋ณต๋ฌธ ํ์ * ๋ด๋ถ ๋ฐ๋ณต๋ฌธ ํ์' ๋งํผ ๋ฐ๋ณต์ด ์งํ ๋๋ค. ์๋์ ์ฝ๋๋ ์ค์ฒฉ while๋ฌธ๊ณผ ์ค์ฒฉ for๋ฌธ์ ์์์ด๋ค.
// ๐1๋ถํฐ 3๊น์ง 10๋ฒ ๋ฐ๋ณตํ์ฌ ์ถ๋ ฅ
public class Main{
public static void main(String[] args){
int i = 1;
while(i <= 10) {
i++;
int j = 1;
while(j <= 3) {
System.out.println(j);
j++;
}
}
}
}
// ๐ ์ค์ฒฉ while๋ฌธ ์ฌ์ฉํ์ฌ ๊ณ๋จ์ ์ถ๋ ฅ
public class Main{
public static void main(String[] args){
int i = 1;
while(i <= 5) {
int j = 1;
i++;
while(j < i) {
System.out.print("*");
j++;
}
System.out.println();
}
}
}
// ๐ ์ค์ฒฉ for๋ฌธ ์ฌ์ฉํ์ฌ ๊ณ๋จ์ ์ถ๋ ฅ
public class Main{
public static void main(String[] args){
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) { // i๊ฐ ๋ฐ๋ณต๋๋ ํ์ ๋งํผ j๊ฐ ์ถ๋ ฅ๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์
// i <= j ๋ผ๋ ์กฐ๊ฑด์์ ๋ง๋ค์ด์ค
System.out.print("*"); // *์ ์ค๋ฐ๊ฟ ๋์ง ์๊ณ ๋์์ผ ํ๊ธฐ ๋๋ฌธ์ print
}
System.out.println();
}
}
}
๊ฒฐ๊ณผ:
*
**
***
****
*****
// ๐ ์ค์ฒฉ while๋ฌธ ์ฌ์ฉํ์ฌ ์ญ๊ณ๋จ ์ถ๋ ฅ
public class Main{
public static void main(String[] args){
int i = 5;
while(i >= 1) {
int j = 5;
i--;
while(j > i) {
System.out.print("*");
j--;
}
System.out.println();
}
}
}
// ๐ ์ค์ฒฉ for๋ฌธ ์ฌ์ฉํ์ฌ ์ญ๊ณ๋จ ์ถ๋ ฅ
public class Main{
public static void main(String[] args){
for(int i = 5; i >= 1; i --) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
๊ฒฐ๊ณผ:
*****
****
***
**
*
ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค๊ฐ ์ด๋ค ์๋ ๋ฌธ์๋ฅผ ์ ๋ ฅ ๋ฐ์์ ์ถ๋ ฅํ๊ณ ์ถ์ ๋์๋ scanner๋ฅผ ์ฌ์ฉํด์ฃผ๋ฉด ๋๋ค. Scanner ํด๋์ค๋ ๊ฐ์ฅ ๋ํ์ ์ธ ์ ๋ ฅ ๋ฐฉ๋ฒ์ด๋ค. ๋ฐ์ดํฐ ํ์ ์ ๋ฐ๋ผ ์ ๋ ฅํ๋ ์ฝ๋๋ ์กฐ๊ธ์ฉ ๋ฌ๋ผ์ง๋ฉฐ(nextLine(), next(), nextInt() ๋ฑ), ํด๋์ค๋ import ํด์ฃผ์ด์ผ ํ๋ค.
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("์ ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int num = scanner.nextInt();
System.out.println("์
๋ ฅํ์ ์ ์๋ " + num + "์
๋๋ค.");
}
}
// ๐ ๋ฌธ์ 1๊ฐ ์
๋ ฅ ๋ฐ์์ ์ถ๋ ฅ(char)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("๋ฌธ์ 1๊ฐ ์
๋ ฅ: ");
char x = (char) scanner.next().charAt(0);
System.out.println("์
๋ ฅํ ๋ฌธ์: " + x);
}
}
- ์๋ฐ์ scanner์๋ char์ ์
๋ ฅ ๋ฐ์ง ์์ผ๋ฏ๋ก string์ผ๋ก ์
๋ ฅ ๋ฐ์์ scanner.next().charAt(0)์ผ๋ก ์ฒซ๋ฒ์งธ ๋ฌธ์๋ฅผ ๋์
ํด์ค
// ๐ ๋ฌธ์์ด ์
๋ ฅ ๋ฐ์์ ์ถ๋ ฅ
import java.util.Scanner;
public class Main{
public static <str> void main(String[] args){
str s;
Scanner scanner = new Scanner(System.in);
System.out.println("๋ฌธ์๋ฅผ ์
๋ ฅํ์ธ์: ");
s = (str) scanner.nextLine();
System.out.println("์
๋ ฅํ ๋ฌธ์: " + s);
}
}
// ๐ ์ ์์ ๋ฌธ์์ด์ ๊ฐ์ด ์
๋ ฅ ๋ฐ์์ ์ถ๋ ฅ
import java.util.Scanner;
public class Main{
public static <str> void main(String[] args){
str s;
int i;
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.println("์ด๋ฆ์ ์
๋ ฅํ์ธ์: ");
s = (str) scanner1.next();
System.out.println("๋์ด๋ฅผ ์
๋ ฅํ์ธ์: ");
i = scanner2.nextInt();
System.out.println("์ด๋ฆ: " + s);
System.out.println("๋์ด: " + i);
}
}
์์ ํ๋์ ๋ณ์์๋ ์ด๋ ํ ๋ฐ์ดํฐ ํ์ ์ด๋ ๋ฑ 1๊ฐ๋ง ์ ์ฅํ ์ ์๋ ํน์ ๊ณต๊ฐ์ ๋งํ ๊ฒ์ด์๋ค. ๊ทธ๋ ๋ค๋ฉด ํ๋์ ๋ณ์์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ๊ฐ ์ ์ฅํด์ผ ํ ๋์๋ ์ด๋ป๊ฒ ํด์ผํ ๊น? ๋ฐ๋ก '๋ฐฐ์ด'์ ์ ์ฅํด์ผ ํ๋ค. ๋ฐฐ์ด์ ๋ณ์์ ๋ฌ๋ฆฌ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ ์๋ ์ ์ฅ ๊ณต๊ฐ์ ๋งํ๋ค. ์ ์ ๋ฐฐ์ด์ ์๋ก ๋ค์ด๋ณด์.
int arr[] = new int [๋ฐฐ์ด ํฌ๊ธฐ];
// ์๋ฃํ ๋ฐฐ์ด์ด๋ฆ ์ ๊ท ์๋ฃํ
์์ ๊ฐ์ด ์ ์ธํด์ค ๋ฐฐ์ด์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ง ์ ํด์ ธ์๊ณ ๋ฐ์ดํฐ ๊ฐ์ ์๋ฌด๊ฒ๋ ๋ค์ด์์ง ์์ ๋น ๋ฐฐ์ด์ด๋ค. ์ด์ ๋ฐฐ์ด์ ํฌ๊ธฐ์ ๋ฐ์ดํฐํ์ ๋ง๊ฒ ๋ฐฐ์ด์ ๋์ ํด์ฃผ๋ฉด ๋๋ค.
int arr[] = new int[5]; // ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ 5์ธ ์ ์ ๋ฐฐ์ด arr์
arr[0] = 1; // 1๋ถํฐ 5๊น์ง ๋์
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
// ๐ ํ ๋ฒ์ ๋์
arr[] = {1, 2, 3, 4, 5};
๋ฐฐ์ด์๋ ์ง์ ํ ๋ฐฐ์ด ํฌ๊ธฐ๋งํผ์ ์ ์ฅ ๊ณต๊ฐ์ ์์ฑํ๊ณ ๊ทธ ์ ์ฅ ๊ณต๊ฐ์ด ์๋ ์์น ๊ฐ(์ฃผ์)์ arr ๋ณ์์ ๋์ ํ๋ค. arr ๋ณ์๋ ๊ทธ ์ฃผ์ ๊ฐ์ ํตํด์ ๋ฐฐ์ด์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
public class Main {
public static void main(String[] args) {
int[] arr = new int[5]; // ๋ฐฐ์ด ํฌ๊ธฐ๊ฐ 5์ธ ๋ฐฐ์ด ์์ฑ
int k = 10;
int hap = 0;
for(int i = 1; i < 5; i++) {
k += 10; // ๋ฐฐ์ด์ 10๋ถํฐ 50๊น์ง 10์ฉ ๋์ด๋๋๋ก ๋์
arr[i] = k;
hap += arr[i]; // hap ๋ณ์์ ๋ณ์์ ์ดํฉ ๋์
}
int avg = hap / arr.length; // avg ๋ณ์์ ์ดํฉ/๋ณ์๊ธธ์ด ๋์
System.out.println("๋ฐฐ์ด arr์ ์ดํฉ: " + hap);
System.out.println("๋ฐฐ์ด arr์ ํ๊ท : " + avg);
}
}
โ
๋ฐฐ์ด arr์ ์ดํฉ: 140
๋ฐฐ์ด arr์ ํ๊ท : 28