알고리즘/프로그래머스, 백준, 구름
[구름] 앱 실행중 데이터 입력(Scanner, hasNextInt), 여러형태로 입출력(파일로 입력받기, GUI)
현호s
2020. 11. 11. 09:42
반응형
# 앱 실행중 데이터 입력
## 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();
}
}
}
반응형