본문 바로가기
728x90

전체 글241

짝수 출력하는 프로그램(2) #앞에 for문에 if문을 중첩하였지만, 이번엔 if문을 빼고 for문만으로 짝수를 출력하는 방법 public class For3 { /** * @param args */ public static void main(String[] args) { // 20내의 짝수를 출력 for(int i=0; i 2012. 4. 19.
짝수인지 홀수인지 알아내는 방법 #입력된 숫자가 짝수인지 홀수인지를 구분하는 프로그램 public static void main(String[] args) { // 입력받은 수의 홀짝 구분 Scanner sc = new Scanner(System.in); System.out.println("숫자를 입력하시오:"); int num = sc.nextInt(); if(num%2==0){ // %: 나머지 값을 구하는 연산자 System.out.println("짝"); }else{ System.out.println("홀"); } } } 2012. 4. 18.
짝수 출력 방법 #for문을 통해 일정한 범위 내에서 짝수를 찾아 출력하는 프로그램 public class For3 { /** * @param args */ public static void main(String[] args) { // 20내의 짝수를 출력 for(int i=0; i 2012. 4. 18.
1, 2, 4, 7...출력 #1, 2, 4, 7...출력하기 출력 결과물: 1, (1+1)=2, (2+2)=4, (4+3)=7, (7+4)=11.... i가 순차적으로 증가하면서 a라는 변수에 자신의 값을 더한다. 그리고 a는 i가 자신의 조건을 만족하여 끝나는 때까지 그 안에서 결과물이 다시금 자신의 값이 되어 순차적으로 증가한 i의 값에 더해져간다. public class For5 { /** * @param args */ public static void main(String[] args) { // 1,2,4,7,11 식으로 총 10개 출력 int i; int a=1; for(i=0; i 2012. 4. 18.
일정한 간격의 숫자 출력 #일정한 간격의 숫자(10, 8, 6, 4, 2, 0) 출력 public class For4 { /** * @param args */ public static void main(String[] args) { // 첫 번째 방법 for(int i=10; i>=0; i--){ if(i%2==0){ System.out.print(i+"\t"); } } System.out.println(); //한 줄 내리기 // 두 번째 방법 for(int i=10; i>=0; i-=2){ System.out.print(i+"\t"); } } } 2012. 4. 18.
홀수 출력 방법 #1~20의 숫자 중 홀수를 출력하는 방법 public class For2 { /** * @param args */ public static void main(String[] args) { // 첫 번째 방법 for(int i = 1; i 2012. 4. 18.
입출력문자 #scanner scanner를 이용하여 변수를 입력 받을 수 있다. Scanner sc = new Scanner(System.in); //콘솔에서 입력 받을 준비, sc=변수명 System.out.println("내용"); Int a = sc.nextInt(); //숫자를 입력 받아 a에 할당 Scanner sc = new Scanner(System.in); System.out.println("내용"); String a = sc.next(); //문자를 입력 받아 a에 할당 ex) 성명과 나이를 입력받아 출력 import java.until.Scanner; public static void main(String[] args){ Scanner sc = new Scanner(System.in); Syste.. 2012. 4. 18.
자바 출력 #결과값을 화면에 출력 System.out.println("내용"); print: 한 줄에 출력 println: 출력 후 한 줄 내리기 ex)'사과'출력 public class Apple { /** * @param args */ public static void main(String[] args) { System.out.println("사과"); } } 2012. 4. 18.
입력받은 세 숫자 중 가장 큰수를 구하는 것 #입력받은 세 숫자 중 가장 큰수를 구하는 것 package step3; import java.util.Scanner; public class Hw1 { /** * @param args */ public static void main(String[] args) { // 세 숫자 중 큰 수를 출력 Scanner sc = new Scanner(System.in); System.out.println("첫 번째 숫자를 입력"); int a = sc.nextInt(); System.out.println("두 번째 숫자를 입력"); int b = sc.nextInt(); System.out.println("세 번째 숫자를 입력"); int c = sc.nextInt(); int max=a; if(maxc){ max.. 2012. 4. 18.
두 개의 숫자와 연산자를 입력 받아 계산하는 계산기 #두 개의 숫자와 한 개의 연산자를 입력 받아 계산하는 코드 import java.util.Scanner; public class Cond5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("첫 번째 숫자를 입력하시오:"); int a = sc.nextInt(); System.out.println("두 번째 숫자를 입력하시오:"); int b = sc.nextInt(); System.out.println("연산자를 선택하시오: (0:+, 1:-, 2:*, 3:/)"); int o.. 2012. 4. 18.
간단 성적표 #이름과 점수를 입력하여 성적 등급을 나타낸다. package step2; import java.util.Scanner; public class Cond4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); // 콘솔에서 입력받을 준비 //println:출력 후 한 줄 내리기, print: 한 줄에 출력 System.out.println("성명을 입력하세요"); String name = sc.next();//문자열을 입력받아 name에 할당 System.out.println("점수를 입력하세요:"); int a = .. 2012. 4. 18.
데이터타입 데이터 타입 기본형 - 정수 : byte(1byte), short(2byte), int(4byte), long(8byte) -실수 : float(4byte), double(8byte) -문자 : char(2byte), string(문자열) 컴파일 -> bin: .class 패키지: 소스들의 방 2012. 4. 18.
SMALL