생각하는 감쟈
[JAVA] Method , 형 변환 연습 본문
package kr.or.ddit.Homework;
import java.util.Scanner;
public class Homework02 {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Homework02 obj = new Homework02();
// obj.method1();
// obj.mehtod2();
// obj.mehtod3();
// obj.mehtod4();
obj.mehtod5();
}
}
method1) 스캐너를 통해 문자를 입력 받고, 소수점 2째 자리에서 버림 ex) 12.12345 → 12.12
public void method1() {
System.out.println("실수 를 입력해주세요.");
String str = sc.nextLine();
double num = Double.valueOf(str); // 받은 문자열이 더블형으로 바뀜
int result1 = (int)(num*100);
num = result1 /100.00;
System.out.println(num);
// 정답
// double num = Double.valueOf(str);
// num = num * 100;
// double result =(int)num/100.0;
}
mehtod2) 스캐너를 통해 대문자를 입력 받아 소문자로 변환
public void mehtod2() {
System.out.println("대문자를 입력 해주세요.");
String s = sc.nextLine();
char c1 = s.charAt(0);
int lower1 = c1 + 32;
char result2 = (char)lower1;
System.out.println(result2);
// 정답
// char ch = (char)(s.charAt(0)+32);
}
mehtod3) 소문자를 입력 받아 대문자로 변환
public void mehtod3() {
System.out.println("소문자를 입력 해주세요.");
String s = sc.nextLine();
char c1 = s.charAt(0);
int upper1 = c1 - 32;
char result3 = (char)upper1;
System.out.println(result3);
// 정답
// char ch = (char)(s.charAt(0)-32);
}
mehtod4) 스캐너를 통해 숫자 3자리를 입력 받고 각 자리수의 합 ex) 123 → 1+2+3
public void mehtod4() {
System.out.println("세자리 숫자를 입력해주세요.");
String s = sc.nextLine();
int i1 = Integer.parseInt(s);
int r1 = i1/100;
int r2 = (i1 % 100) / 10;
int r3 = i1 % 10;
int sum = r1 + r2 + r3;
System.out.println("각 자리수의 합 : " +sum);
// 정답 (charAt()를 이용한 코드)
// int ch1 = s.charAt(0)-'0';
// int ch2 = s.charAt(1)-'0';
// int ch3 = s.charAt(2)-'0';
//
// int sum = ch1 + ch2 + ch3;
//
// System.out.println(sum);
}
mehtod5) 스캐너를 통해 문자를 입력 받고, 소수점 2째 자리에서 반올림 ex) 12.2623 → 12.3
public void mehtod5() {
/*
* 스캐너를 통해서 문자(실수)를 입력 받고. 소수점 2째 짜리에서 반올림 ex ) 12.2623 - > 12.3
*/
System.out.println("뮨자 (실수)를 입력해주세요");
String s = sc.nextLine();
double num = Double.parseDouble(s);
num=num+0.05;
double result5 = ((int)(num*10))/10.0;
System.out.println(result5);
}
'Language > Java' 카테고리의 다른 글
[JAVA] Stack_heap, 배열 복사, 2차원 배열_01 (2) | 2024.03.15 |
---|---|
[JAVA] 참조 타입, 배열, 버블 정렬 (0) | 2024.03.13 |
[JAVA] 자바 별, 피라미드 찍기 (0) | 2024.03.13 |
[JAVA] 조건문 : while, break, continue 문 (0) | 2024.03.13 |
[JAVA] 조건문 : If문, Switch문 (1) | 2024.03.08 |
Comments