본문 바로가기
웹 풀스택 스터디(뉴렉처-학원)--> 이거 꼭 보세요

9. for,while문 문제 보충

by 백엔드개발자0107 2021. 8. 11.
package ex4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class hw6_answer {

	public static void main(String[] args) throws IOException {

		
		File datacopy = new File("res/data-copy.txt");
		
		FileInputStream fis = new FileInputStream(datacopy);
		
		File datacopy2 = new File("res/data-copy2.txt");
		
		FileOutputStream fos = new FileOutputStream(datacopy2);
		
		Scanner fscan = new Scanner(fis);
		
		PrintStream fout = new PrintStream(fos);
		
		
		int count = 0;
		
		
		
		for(int i =0;fscan.hasNext();i++) {
			fscan.next();
			
//			//[59 65 90 80...]
//			fscan.next();//59가 남겨진다.
//			
//			//[65 90 80...]
//			fscan.next();//65가 남겨진다.
//			
//			//[90 80...]
//			fscan.next();//90가 남겨진다.
			
			count++;
			
		}
		
		
	
		
		fscan.close();
		fout.close();
		fis.close();
		fos.close();
		
		System.out.println("finish");
		
	}

}​
package ex4;

public class hw3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(int i = 0 ;i<11;i++) {
			
			for(int j = 0;j<11;j++) {
				
			if(j==i||j==-i+10) 
				System.out.print("*");
			else 
				System.out.print(" ");
		
			}
		System.out.println();

	}
			
			System.out.println();
		}
	}

자, 이런식으로 해서

*         *

 *       * 

  *     *  

   *   *   

    * *    

     *     

    * *    

   *   *   

  *     *  

 *       * 

*         *

 

이런꼴의 별 형태를 출력하게끔 할수 있다.!

 

이 과정을 기억하자

 

package ex4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class hw6 {

	public static void main(String[] args) throws IOException {

		
		File datacopy = new File("res/data-copy.txt");
		
		FileInputStream fis = new FileInputStream(datacopy);
		
		File datacopy2 = new File("res/data-copy2.txt");
		
		FileOutputStream fos = new FileOutputStream(datacopy2);
		
		Scanner fscan = new Scanner(fis);
		
		PrintStream fout = new PrintStream(fos);
		
		
		int total = 0;
		
		
		
		for(int i =0;fscan.hasNext();i++) {
			
			String x_ = fscan.next();
			
			
			int x = Integer.parseInt(x_);
			
			total += x;
			
			
			fout.println("result:");
			fout.printf("i is %d\n",total);		
		}
		
		
	
		
		fscan.close();
		fout.close();
		fis.close();
		fos.close();
		
		System.out.println("finish");
		
	}

}

 

위에 자바코드 정답은 아래와 같다..!

 

결과값이 11로 잘 나온다..!