[백준 알고리즘] 1단계. 입출력
[1단계. 입출력받아보기_JAVA]
▶ 2557. HELLO WORLD! 출력
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!");
}
}
▶ 1000. A+B 출력
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
System.out.println(a+b);
}
}
▶ 1001. A-B 출력
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
System.out.println(a-b);
}
}
▶ 7287. 맞은문제, 아이디 출력
- 하드 코딩 문제로 따옴표안에 각자 맞은 문제와 ID 입력하면 된다.
- 맞은 문제와 ID는 본인 닉네임 클릭 후 확인 가능!
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("맞은문제");
System.out.println("ID");
}
}
▶ 10172. 개 그림 출력
- 그림1과 같이 출력 결과를 그대로 복사해서 코드 안에 붙였더니 출력 값과 동일한 결과로 출력이 되나 "출력형식이 잘못됐습니다"라는 오류가 나온다. 검색하니 공백이나 사소한 오타를 내면 나올 수 있는 에러라고 한다. 따라서 위의 코드는 자바에서 개행문자로 쓰는 \r과 \n이 중복됐기 때문에 에러가 나왔다고 판단했고 하나만 사용하니 올바른 결과를 출력한다.
- 개행 문자는 문서를 다루는 프로그램에 따라 알맞게 변형시켜서 사용해야함. 메모장 같은 경우엔 \r\n을 동시에 사용해야 바른 개행이 됨.
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("|\\_/|\n" +
"|q p| /}\n" +
"( 0 )\"\"\"\\\n" +
"|\"^\"` |\n" +
"||_/=\\\\__|");
}
}
▶ 10718. 그대로 출력하기
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("강한친구 대한육군");
System.out.println("강한친구 대한육군");
}
}
▶ 11718. 그대로 출력하기
- 입력에 공백도 포함이기 때문에 next()가 아닌 nextLine()을 쓴다.
- next(): 공백 전까지 출력
- nextLine(): 문자열 전체 출력
- hasNext(): 다음 입력 값이 있으면 true, 없으면 false
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String input = sc.nextLine();
if(input.length() <= 100 && !(input.isEmpty()))
{
System.out.println(input);
}
else
break;
}
}
}
▶ 11719. 그대로 출력하기2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String input = sc.nextLine();
if(input.length() <= 100)
{
System.out.println(input);
}
else
break;
}
}
}