โ๐ป~ 11.10
[0615] ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ(continue๋ฌธ, break๋ฌธ) ๋จ๊ณ๋ณ ์์
Nsso
2023. 6. 17. 13:50
๐ง ๋จ๊ณ๋ณ ์ค๊ฐ ์์
1. res/exam.list ํ์ผ์ ์ฑ์ ์ด ๋ค์๊ณผ ๊ฐ์ด ์ ์ฅ๋์ด ์์๋, ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ ์ ์ฒด ๊ฐ ์ถ๋ ฅํ๊ธฐ
30 49 38 49 50 80 90 100 70 50 60 74 50 33 50 65 76 69
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Loop {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("res/exam.list");
Scanner fscan = new Scanner(fis);
while (fscan.hasNext()) {
// hasNext๋ ๋ค์ ๊ฐ์ ์กด์ฌ์ฌ๋ถ๋ฅผ ํ์ธํ ํ ๊ฐ์ด ์กด์ฌํ ์ ๋ฐ์์ค๋ ๋ฉ์๋์ด๊ธฐ์ while๋ฌธ์ ์ฌ์ฉ
String word = fscan.next();
int score = Integer.parseInt(score);
System.out.printf("%d ", score);
}
}
}
// ์ถ๋ ฅ๊ฐ : 30 49 38 49 50 80 90 100 70 50 60 74 50 33 50 65 76 69
2. ํ์ผ์์ ์ฑ์ ์ 5๋ฒ๊น์ง ์ฝ์ด์ ๋ค์์ฒ๋ผ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ
30 49 38 49 50
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Loop {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("res/exam.list");
Scanner fscan = new Scanner(fis);
for (int i = 0; fscan.hasNext(); i++) {
String word = fscan.next();
int score = Integer.parseInt(word);
if (i >= 5) {
break;
}
System.out.printf("%d ", score);
}
}
}
// ์ถ๋ ฅ๊ฐ : 30 49 38 49 50
๐์ ์๋ ์ฝ๋๋ฅผ ๋ณด๋ฉด์ hasNext ๋ฉ์๋๋ฅผ for๋ฌธ ์์ ๋ฃ์ ์ ์๋ค๋๊ฒ ๋๋ผ์ ์ ..
3. ํ์ผ์์ ์ฑ์ ์ 5๋ฒ๋ถํฐ ์ฝ์ด์ ๋ค์์ฒ๋ผ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ
50 80 90 100 70 50 60 74 50 33 50 65 76 69
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Loop {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("res/exam.list");
Scanner fscan = new Scanner(fis);
for(int i=0; fscan.hasNext(); i++) {
String word = fscan.next();
int score = Integer.parseInt(word);
if(i<4)
continue;
System.out.printf("%d " , score);
}
}
}
// ์ถ๋ ฅ๊ฐ : 50 80 90 100 70 50 60 74 50 33 50 65 76 69
4 . ํ์ผ์์ ์ฑ์ ์ ์ฝ๋ค๊ฐ 100์ ์ ๋ง๋๋ฉด ๊ทธ ์ดํ์ ์ฑ์ ์ ๋ชจ๋ ์ถ๋ ฅํ์์ค (100 ๋ฏธํฌํจ)
70 50 60 74 50 33 50 65 76 69
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Loop {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("res/exam.list");
Scanner fscan = new Scanner(fis);
//4-1
for (int i = 0; fscan.hasNext(); i++) {
String word = fscan.next();
int score = Integer.parseInt(word);
while (score == 100 && fscan.hasNext()) {
String word1 = fscan.next();
int score1 = Integer.parseInt(word1);
System.out.printf("%d ", score1);
}
}
//4-2
for (boolean button = false; fscan.hasNext();) {
int score = fscan.nextInt();
if(score ==100)
button = true;
if(!button || score ==100)
continue;
System.out.printf("%d ", score);
}
// ์ถ๋ ฅ๊ฐ : 70 50 60 74 50 33 50 65 76 69
5. ํ์ผ์์ ์ฑ์ ์ ์ฝ๋ค๊ฐ 100์ ์ ๋ง๋๋ฉด ๊ทธ ์ดํ์ ์ฑ์ ์ ๋ชจ๋ ์ถ๋ ฅํ์์ค (100 ํฌํจ)
100 70 50 60 74 50 33 50 65 76 69
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Loop {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("res/exam.list");
Scanner fscan = new Scanner(fis);
for (Boolean button = false; fscan.hasNext();) {
int score = fscan.nextInt();
if(score == 100)
button = true;
if(!button)
continue;
System.out.printf("%d " , score);
}