JAVA/자바 실습 / / 2021. 4. 11. 15:34

Java | Lab1

01. Hello World 출력 프로그램

/* Hello World! 를 출력하는 프로그램 */
public class Hello {

	public static void main(String[] args) {
		
		System.out.println("Hello World!");

	}

}

02. 다양한 출력 프로그램

/* 다양한 출력을 지원하는 프로그램 */
public class Hello2 {
	public static void main(String[] args) {
		System.out.println("(1) Hello, World!");
		System.out.printf("(2) %s\n", "Hello, World!");
		System.out.print("(3) Hello, World!");
		System.out.println("(4) Hello, " + "World!");
		System.out.println("(5) " + 3 + 6);
		System.out.println("(6) " + (3 + 6));
	}
}

03. 두 스트링을 출력하는 프로그램

/* 두 스트링을 출력하는 프로그램 */
public class TwoStrings {
	public static void main(String[] args) {
		System.out.println("Hello," + " World!");
	}
}

04. 두 숫자를 출력하는 프로그램

/* 두 숫자를 출력하는 프로그램 */ 
public class TwoNumbers {
	public static void main(String[] args) {
		int a = 10, b = 20;
		System.out.println(a + " " + b);
	}
}

05. 두 숫자를 멋지게 출력하는 프로그램

/* 두 숫자를 출력하는 프로그램 */ 
public class TwoNumbers {
	public static void main(String[] args) {
		int a = 10, b = 20;
		System.out.println("a = " + a + " b = " + b);
	}
}

06. 1~10까지 합산하는 프로그램

/* 1~10까지 합산하는 프로그램 */
public class AddNumbers {

	public static void main(String[] args) {
		int sum = 0, i;
		
		for(i=1; i<=10; i++)
			sum += i;
		
		//for문을 사용하여 1~10까지 합산한 값을 sum에 저장하고 출력
		System.out.println("sum="+sum);
	}

}

07. 메소드를 이용한 덧셈 출력 프로그램

public class Calculator {
	public static int add (int m, int n) {
		return m + n;
		
	}
	
	public static void main(String[] args) {
		int a = 30, b = 5;
		System.out.println("덧셈 결과 = " + add(a,b));
	}

}

'JAVA > 자바 실습' 카테고리의 다른 글

Java | Lab5  (0) 2021.04.15
Java | Lab4_Ex  (0) 2021.04.12
Java | Lab4  (0) 2021.04.12
Java | Lab3  (0) 2021.04.12
Java | Lab2  (0) 2021.04.11
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유