โœ๐Ÿป~ 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);
	}