1. 키보드로 입력받은 데이터 출력하기
2. 사칙연산
3. 계산
4. 문자열에서 문자(인덱스) 출력하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package com.sy.practice1.example;
import java.util.Scanner;
public class A_VariablePractice {
//변수 실습
//변수1. 사용자 정보 입력받아 변수에 값을 담고 출력하기
public void method1() {
Scanner sc = new Scanner(System.in); //입력 후 'ctrl + shift + o' --> import java.util.Scanner;
System.out.print("이름을 입력하세요 : "); // syso + ctrl + space --> System.out.println(); 자동완성
String name = sc.nextLine(); //String형은 nextLine(); 사용.
System.out.print("나이를 입력하세요 : ");
int age = sc.nextInt();
sc.nextLine(); // nextInt(); 다음 입력으로 nextLine()이 오면 nextLine();으로 버퍼 비워주기.
System.out.print("성별을 입력하세요(남/여) : ");
char gen = sc.nextLine().charAt(0); //char 타입 : nextLine()chatAt();
System.out.print("키를 입력하세요(cm) : ");
double height = sc.nextDouble();
System.out.println("키 " + height + "인 " + age + "살 " + gen + "자 " + name + "님 반갑습니다^^");
}
//변수2. 정수 입력받아 사칙연산
public void method2() {
Scanner sc = new Scanner(System.in);
System.out.print("첫 번째 정수를 입력하세요 : ");
int num1 = sc.nextInt();
System.out.print("두 번째 정수를 입력하세요 : ");
int num2 = sc.nextInt();
System.out.println("더하기 결과 : " + (num1 + num2)); //괄호를 생략하면 문자열로 인식.
System.out.println("빼기 결과(첫 번째 정수 - 두 번째 정수) : " + (num1 - num2));
System.out.println("빼기 결과(절대값) : " + Math.abs(num1 - num2)); // 절대값 : math.abs()
System.out.println("곱하기 결과 : " + (num1 * num2));
System.out.println("나누기 몫 결과 : " + (num1 / num2));
}
//변수3. 실수 입력받아 사각형 면적, 둘레 계산하기
public void method3() {
Scanner sc = new Scanner(System.in);
System.out.print("가로 : ");
double width = sc.nextDouble();
System.out.print("세로 : ");
double length = sc.nextDouble();
System.out.println("면적 : " + (width * length));
System.out.println("둘레 : " + ((width + length) * 2));
}
//변수4. 영어 문자열 입력받아 앞 문자 3개 출력
public void method4() {
Scanner sc = new Scanner(System.in);
System.out.print("문자열을 입력하세요 : ");
String str = sc.nextLine();
System.out.println("첫 번째 문자 : " + str.charAt(0)); //문자열에서 문자의 위치(인덱스)는 0부터 시작.
System.out.println("두 번째 문자 : " + str.charAt(1));
System.out.println("세 번째 문자 : " + str.charAt(2));
}
}
|
cs |
'Java' 카테고리의 다른 글
자바 요약 정리 - 변수 (0) | 2021.07.06 |
---|---|
[Java 자바] 반복문 (0) | 2021.04.25 |
[Java 자바] 조건문 (0) | 2021.04.25 |
[Java 자바] 연산자 (0) | 2021.04.25 |
[Java 자바] 형변환 - 자동 형변환, 강제 형변환 (0) | 2021.04.25 |
자바 변수 - Variable (0) | 2021.03.25 |
자바(Java) 개발환경 구축하기(2021) 2/2 - How to Install Eclipse for Java (0) | 2021.03.11 |
자바(Java) 개발환경 구축하기(2021) 1/2 - How to Install Java JDK on Windows (0) | 2021.03.03 |