본문 바로가기

JAVA/변수5

자료형변환(2) package variables; import java.util.InputMismatchException; public class Casting2 { public static void main(String[] args) { // 자료형변환 int data = 100; String strData = data + "ㅁㄴㅇ"; System.out.println("정수데이터 " + "strData"); int convertInt = 0; try { // 예외발생 가능성이 있는 코드 작성 // 숫자 + 문자를 Integer로 형변환 하려고 하는 중에 문제가 발생하고 있다. convertInt = Integer.parseInt(strData); } catch (NumberFormatException e) { // .. 2022. 7. 18.
변수 예제 package variables; public class Quiz1 { public static void main(String[] args) { // Quiz01 // 각 변수의 자료형에 맞게 데이터 저장 후 출력 String name = "김말이"; int age = 20; int iq = 120; double height = 173.3; char grade = 'B'; System.out.printf("이름 : %s\n나이 : %d\n키 : %.1f\n아이큐 : %d\n등급 : %c", name, age, height, iq, grade); } } 2022. 7. 18.
자료형변환(1) package variables; public class Casting { public static void main(String[] args) { // 형변환(Casting) // 특정 변수에 대한 자료형을 형변환 할 수 있다. short tmpS; // 임시 저장변수 char tmpC; // 임시 저장변수 byte b = 97; short s = 20; char c = 'A'; float f = 1.23f; tmpS = s; s = b; // b = s의 경우의 수도 비교하기 위해 s값을 임시변수에 저장. System.out.println(s); // 자료형 short변수에 byte형 데이터 대입연산 s = tmpS; b = s; // -> byte(1byte)는 short(2byte)보다 작은 크기.. 2022. 7. 18.
단일문자/문자열 간의 연산 및 출력 package variables; public class Variables2 { public static void main(String[] args) { // char int // 서로 자유롭게 변환하여 사용이 가능하다. int integerData = 123; integerData += 10; // data = data + 10; char ch = 'D'; // 모든 단일문자는(혹은 키보드에 존재하는 key) 숫자형 표현방식(unicode, ascii code)을 가지고 있다. // 이를 통한 사칙연산으로도 단일문자 표현이 가능하다. System.out.println((char)((int)ch + 10)); System.out.println(integerData); char ch2 = 'A'; int .. 2022. 7. 18.