728x90 전체 글245 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 출력하기 #한 수는 순차적 증가, 한 수는 순차적 감소되는 값을 출력하기 결과: 1 10 2 9 3 8 4 7 5 6 public class For2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a=0; int b=11; for(int i=1; i 2012. 4. 19. 1, 10, 18, 25...출력하기 #1, 10, 18, 25...순으로 출력하는 프로그램 이 프로그램은 늘어나는 수가 1, 1+9=10, 10+8=18, 18+7=25...와 같이 늘어나는 수가 순차적으로 1씩 작아지는 규칙을 가지고 있다. public class For1 { /** * @param args */ public static void main(String[] args) { // 1, 10, 18, 25...출력하기 int a=1; for(int i=9; i>0; i--){ System.out.print(a+"\t"); a=a+i; // 이 명령어를 출력문 앞에 두게 되면 초기 숫자 1이 출력되지 않는다. } } } 2012. 4. 19. 1,3,7,13...출력 프로그램 #1,2,4,7...이 순차적 증가를 통한 것이라면 1,3,7,13...은 2의 배수의 증가 public class AprilEighteen2 { /** * @param args */ public static void main(String[] args) { //1, 3, 7, 13, 21, 31 출력 int a = 1; for(int i=0; i 2012. 4. 19. 홀수(양수), 짝수(음수) 출력 프로그램 #홀수는 양수로, 짝수는 음수로 출력되게 하는 프로그램 결과물 ex) 1, -2, 3, -4, 5..... public class AprilEighteen1 { /** * @param args */ public static void main(String[] args) { //1, -2, 3, -4, 5, -6출력 for(int i=1; i 2012. 4. 19. 짝수 출력하는 프로그램(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. 이전 1 ··· 17 18 19 20 21 다음 SMALL