반응형

# 앱 실행중 데이터 입력

## Scanner 이용하여 입력받기

import java.util.Scanner;

publuc class Main {
	public static void main(String[] args) {
    
    	Scanner sc = Scanner(System.in);
        
        int num = sc.nextInt();
        
        System.out.println(i * 1000);
        sc.close();
    }
}

## hasNextInt 이용하여 입력받기

  • while문을 이용해서 반복적으로 입력받기.
  • 사용자가 입력한 값이 int 형인경우 true, int 형이 아닌 경우 false를 리턴하여 프로그램이 종료된다.
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
  
    Scanner sc = new Scanner(System.in);
    
    while (sc.hasNextInt()) {
    	System.out.println(sc.nextInt()*1000);
    }
	sc.close();
  }
}

 

# 여러 형태의 입출력

## 파일을 이용하여 입력받기

import java.util.Scanner;
import java.io.*;

class Main {
  public static void main(String[] args) {
    try {
        File file = new File("input.txt");
        //input.txt에 입력된 값을 받는다.
        Scanner sc = new Scanner(file);
        while(sc.hasNextInt()) {
            System.out.println(sc.nextInt()*1000);
        }
        sc.close();
    } catch(FileNotFoundException e){
        e.printStackTrace();
    }
  }
}
반응형

+ Recent posts