ํ์ต ๋ด์ฉ
๋ณ์(Variable)
๋ณ์ ์๋ช ๊ท์น
๋ณ์ ์ข ๋ฅ๋ณ ๋ฐ์ดํฐ ํฌ๊ธฐ
์๋ฃํ ๋ณํ
1. ์๋ํ ๋ณํ
2. ๊ฐ์ ํ ๋ณํ
float f1;
f1 = (float)3 / 2; // (float)์ ๋ถ์ฌ์ฃผ์ง ์์ผ๋ฉด ๊ฒฐ๊ณผ๊ฐ์ด integer๋ก ๋ฐ์๋ค์ฌ 1์ด ๋์ด.
System.out.println(f1); // ๊ฒฐ๊ณผ : f1 = 1.5
float f2;
f2 = 0xA + (float)4.5 + (float)4/5;
System.out.println(f2); // ๊ฒฐ๊ณผ : f2 = 15.3
double value = 1234.56;
int num1;
num1 = (int)value; // ์ ์๊ฐ์ผ๋ก ๋ฐ์๋ค์ฌ ์์์ ๊ฐ์ด ์ฌ๋ผ์ง.
// ๊ฒฐ๊ณผ : 1234
Scan
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class practice4 {
public static void main(String[] args) throws IOException {
//ํ์ค ์
์ถ๋ ฅ
//์ด๋ฆ, ๋์ด, ๋จ์(๋ฉด true), ํด๋ํฐ, ํค ,์ฃผ์ ๊ฐ์ ์
๋ ฅํ๋ฉด ํ์ ํ๋ฒ์ ์ถ๋ ฅ๋๋ ๋ฐฉ์
//Scanner ํด๋์ค๋ฅผ ํ์ฉํ์ฌ ์
๋ ฅ
//๋ง์ง๋ง ์ฃผ์๋ BufferedReader ํ์ฉ
Scanner sc = new Scanner(System.in);
String n;
int age;
boolean man;
String phone;
double height;
String add;
System.out.print("์ด๋ฆ : ");
n = sc.next();
System.out.print("๋์ด : ");
age = sc.nextInt();
System.out.print("๋จ์ : ");
man = sc.nextBoolean();
System.out.print("ํด๋ํฐ : ");
phone = sc.next();
System.out.print("ํค : ");
height = sc.nextDouble();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("์ฃผ์ : ");
add = br.readLine();
//BufferedReader ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ด์ฐ๊ธฐ ์ดํ ๋ฌธ์์ด๋ ํจ๊ป ์ถ๋ ฅ
//next(); ๋ ๋์ด์ฐ๊ธฐํ๋ฉด ๋ค์ ๋ฌธ์์ด ์์ด์ง
System.out.println("์ด๋ฆ : " + n);
System.out.println("๋์ด : " + age);
System.out.println("๋จ์ : " + man);
System.out.println("ํด๋ํฐ : " + phone);
System.out.println("ํค : " + height);
System.out.println("์ฃผ์ : " + add);
}
}
๋ณ์ x, y๊ฐ ์๋ก ๋ฐ๊พธ๊ธฐ
public static void main(String[] args) {
// x, y ๋ณ์์ ๊ฐ์ ๋ฐ๊พธ๊ธฐ
// ์ฒซ๋ฒ์งธ, ์์ ๋ณ์ temp๋ฅผ ๋ง๋ค์ด์ ์ ์ฅ ํ์ ๋ถ๋ฌ์ค๊ธฐ
int x,y;
x = 1;
y = 2;
int temp;
temp = x;
x = y;
y = temp;
System.out.println("x = " +x+ " y = " +y);
//๊ฒฐ๊ณผ : x =2 y =1
// ๋๋ฒ์งธ, ์๋ ์ฐ์ฐ์์ ํตํด ์์ ๋ณ์ ์์ด ๊ฐ ๋ฐ๊พธ๊ธฐ
x = x + y;
y = x - y;
x = x - y;
System.out.println("x = " +x+ " y = " +y);
//๊ฒฐ๊ณผ : x=1 y=2
}
}
์๊ธฐ ์์ (๋ณ์)์ ๊ฐฑ์
num1 = num1 + 1;
num1 += 1;
// ๋ ์์ ๊ฐ๋ค.
์ฆ๊ฐ,๊ฐ์ ์ฐ์ฐ
// increment (์ฆ๊ฐ์ฐ์ฐ) ++ == +1
// decrement (๊ฐ์์ฐ์ฐ) -- == -1
b = a++; // ์์ ์ฐ์ฐ์ ๋จผ์ ํ๊ณ a๊ฐ +1
b = ++a; // a๊ฐ์ ๋จผ์ ์ฆ๊ฐ์ํค๊ณ ํ์ ์ฐ์ฐ
๋ ๊ณต๋ถํด์ผ ํ ๋ด์ฉ
๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ
์์ง ์กฐ๊ฑด๋ฌธ์ ๋ฐฐ์ฐ์ง ์์ ์ํ์ง๋ง ๊ตฌ๊ธ์ ์ฐธ๊ณ ํ์ฌ Scanner ํด๋์ค๋ฅผ ์ด์ฉํด ๋ง๋ค์ด๋ณด์๋ค.
์๋์ ๋๋ ๋ง์ง๋ง ๊ตฌ๋ฌธ์ 0์ด ๋๋ ์กฐ๊ฑด์ "0์ผ๋ก ๋๋ ์ ์์ต๋๋ค." ๋ฌธ๊ตฌ๊ฐ ๋จ์ง์๊ณ ์๋ฌ ๋๋ 0์ ๊ฐ์ด ๋์จ๋ค.
์กฐ๊ฑด๋ฌธ์ ๋ฐฐ์ฐ๊ณ ๋ค์ ๋ง๋ค์ด๋ด์ผ๊ฒ ๋ค.
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
// ๊ฐ๋จํ ์ฌ์น์ฐ์ฐ ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ
// ์ฐ์ฐ์ ๋จผ์ ์
๋ ฅ ํ ์ซ์ 2๊ฐ๋ฅผ ์
๋ ฅํ๋ฉด ๊ณ์ฐ
Scanner scan = new Scanner(System.in);
int a,b;
String c;
int result=0;
System.out.print("์ฐ์ฐ์๋ฅผ ์
๋ ฅํ์ธ์. (+ - * /)");
c = scan.next();
System.out.print("์ฒซ๋ฒ์งธ ์ : ");
a = scan.nextInt();
System.out.print("๋๋ฒ์งธ ์ : ");
b = scan.nextInt();
if(c.equals("+")) {
result = a + b;
System.out.println(a + c + b + " = " + result);
}
else if(c.equals("-")) {
result = a - b;
System.out.println(a + c + b + " = " + result);
}
else if(c.equals("*")) {
result = a * b;
System.out.println(a + c + b + " = " + result);
}
else if(c.equals("/")) {
result = a / b;
System.out.println(a + c + b + " = " + result);
}
else if(c.equals("/") && ((a==0) || (b==0))) {
System.out.println("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.");
}
}
}
'๐ป ๊ฐ๋ฐ > ๐ TIL (Today I Learned)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
21.01.14 TIL (JAVA ๋ฐฐ์ด Array) (0) | 2021.01.14 |
---|---|
21.01.13 TIL(๊ตญ๋น 3์ผ์ฐจ - JAVA ์ฐ์ฐ) (0) | 2021.01.13 |
21.01.11 TIL (๊ตญ๋น 1์ผ์ฐจ) (0) | 2021.01.11 |
21.01.07 TIL (์ ์ฒ๊ธฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค) (0) | 2021.01.08 |
21.01.04 TIL (JAVA ๋ณ์, ์ฐ์ฐ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ) (0) | 2021.01.05 |