Software Development/Java

[Java] 연산

Mei99 2024. 5. 31. 12:25

 

package 연산;

public class VarTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int a = 10;
		int b = 13;
		float c = 0f;
		
		// 정수 나누기 정수 = 결과도 정수!!!!
		c = (a+b) / 2;
		System.out.println("a와 b의 평균은 = " + c);
        
	}

}

 

 

 

 

package 연산;

public class VarTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int a = 10;
		int b = 13;
		float c = 0f;		
	
		// 둘 중에 하나는 실수로 만들어주면 된다.
		//파이썬에서 데이터 타입 바꾸기는 a=3 float(a)
		// 자바에서는?
		c = (float)(a+b) / 2;
		System.out.println("a와 b의 평균은 = " + c);
		
		c = (float)(a+b) / 2.0f;
		System.out.println("a와 b의 평균은 = " + c);
        
        }

}