JAVA 기초
3일차 : while문, 다중 while문, do~while문
tɑ:lərəns
2012. 8. 5. 01:45
for문으로 별 찍기
/* for문을 사용해서 아래처럼 출력해 보세요. ***** **** *** ** * */ class MyFor01 { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=i; j++){ System.out.print("*"); } System.out.println(); //별 출력 } System.out.println("=========================="); //역으로 별 출력 for(int i=5;i>=1;i--){ for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } } }
while문을 이용하여 1부터 100까지의 합 구하기
class MyWhile01{ public static void main(String[] args) { //while문 사용해서 1부터 100까지 합 구하기 int i=1, sum=0; while(i<=100){ //조건식 sum+=i; i++; //증감식 } System.out.println("1부터100까지합:"+sum); } }
다중 while문을 이용하여 구구단 만들기
import java.util.Scanner; class MyWhile02 { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(true){//무한루프 //겉의 화일. true이면 무한루프 System.out.println("단입력[종료:0]"); int dan=scan.nextInt(); if(dan==0){ //0이 아니면 그 밑의 코드를 출력함 break; //루프를 강제로 빠져나감(while문을 종료) //switch, while, for에서만 break사용가능 } if(dan>9 || dan<2){ //9보다 크거나 2보다 작으면---------------▼ ex)10, -1 등 System.out.println("2에서 9사이의 수 입력"); continue;//초기의 조건절로 분기함(여기서는 15번라인) "단입력[종료:0]뜸 } int i=1; //해당구구단 출력 while(i<=9){ System.out.println(dan+"*"+i+"="+dan*i); i++; //다시 위로 올라가 단을 입력한다 } } } }
do~while문을 이용하여 1부터 100까지의 합 구하기
class MyDoWhile01{ public static void main(String[] args) { int i=1, sum=0; //3 do{ sum+=i; i++; //1. i++이 일단 2로 증가한 후 }while(i<=100); //2. 조건식을 본다. 그리고 3으로 올라감 System.out.println("1부터 100까지합:"+sum); } }
do~while문을 이용하여 구구단 만들기
import java.util.Scanner; class MyDoWhile02 { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int dan=0; do{ System.out.println("구구단입력(2이에서 9사이의 수 입력)"); dan=scan.nextInt(); }while(!(dan>=2 && dan<=9)); //참이지만 2~9가 아니면 loop를 계속 돌게(재입력) !(not)을 앞에 붙여줌. for(int i=1;i<=9;i++){ System.out.println(dan+"*"+ i+"="+dan*i); } } }
퀴즈 1
/* [2단] 2*1=2 2*2=4 ............... 2*9=18 [3단] 3*1=3 3*2=6 ............... 3*9=27 ... [9단] 9*1=9 9*2=18 .............. 9*9=81 ========================================= 2*1=2 3*1=3 4*1=4 ............... 9*1=9 2*2=4 3*2=6 4*2=8 ............... 9*2=18 ... 2*9=18 .......................... 9*9=81 */ class Quiz01{ public static void main(String[] args) { System.out.println("구구단을 출력하기"); for(int i=2; i<=9; i++){ System.out.print("["+i+"]단 "); for(int j=1; j<=9; j++){ int gob=i*j; System.out.print(i+"*"+j+"="+gob+" "); } System.out.println(); } System.out.println("=================================================="); System.out.println("구구단 세로로 출력하기"); for(int i=1; i<=9; i++){ for(int j=2; j<=9; j++){ System.out.print(j+"*"+i+"="+(i*j)+" "); } System.out.println(); } } }
퀴즈 2
/* while문을 사용해서 1부터 50까지 수중 3의 배수를 출력하고 3의 배수합도 구해서 출력해 보세요. */ class Quiz02 { public static void main(String[] args) { System.out.println("1~50까지의 수중 3의 배수를 출력하고 합도 구하여라."); int i=3, sum=0; while(i<=50){ System.out.println(i+" "); sum+=i; //3의 배수합 구하기 i=i+3; //3씩 증가하기 } System.out.println(); System.out.println("3의 배수합"+sum); } }
퀴즈 3
/* 1. while문을 사용해서 알파벳 대문자를 출력해 보세요. [A:65] [출력결과] ABCDE...Z 2. 단 입력받아 구구단을 출력해 보세요.(shile사용해서) */ class Quiz03 { public static void main(String[] args) { System.out.println("알파벳 대문자 출력하기"); char ch='A'; while(ch<='Z'){ System.out.print(ch++); } System.out.println(); int a=65; while(a<=90){ System.out.print((char)a); a++; } } }
퀴즈 4
/* 단 입력받아 구구단을 출력해 보세요. */ import java.util.Scanner; class Quiz04 { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("단입력"); int num=scan.nextInt(); int i=1; while(i<=9){ int gob=num*i; System.out.println(num + "*" + i + "=" + gob); i++; } } }
퀴즈 5
/* 다중 while 사용해서 아래처럼 구구단 출력해 보세요~ [2단] 2*1=2 2*2=4 .............. 2*9=18 [3단] 3*1=3 3*2=6 .............. 3*9=27 ... [9단] 9*1=9 9*2=18 ............. 9*9=81 */ class Quiz05 { public static void main(String[] args) { System.out.println("구구단 출력하기"); int i=2, j=1; //i=단 출력 while(i<=9){ System.out.print("["+i+"단] "); while(j<=9){ System.out.print(i+"*"+j+"="+i*j+" "); j++; } System.out.println(); //한 단 출력하고 개행하기 j=1; //j를 1로 다시 초기화하기(1부터 곱해져야 하므로) i++; //단 증가하기 } } }
퀴즈 6
//문제:1부터 100까지수중 3의 배수합 구하고 3의 배수 출력하기 ==>do~while사용 class Quiz06 { public static void main(String[] args) { int i=3, sum=0; do{ sum+=i;//3의 배수합구하기 System.out.println(i+" "); i=i+3; //3씩 증가하기 }while(i<=100); System.out.println("\n3의 배수합"+sum); //\n는 줄을 바꿔줄 때 } } /*
i=i+3; sum+=i; 이렇게 위치를 바꾸면 결과값이 다르게 나타난다. System.out.println(i+" "); 의 i값이 3에서 6씩 증가하게 되므로. */
퀴즈 7
/* 아이디와 비밀번호를 입력받아 맞으면 환영합니다!, 틀리면 아이디 또는 비밀번호가 틀려요! 라고 출력해 보세요. 단 입력기회는 3번이고 모두 틀리면 더이상 접속할 수 없습니다. 라고 출력하세요 */ import java.util.Scanner; class Quiz07{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); for(int i=1;i<=3;i++){ //3번의 loop를 돌아라 i가 1일 때 한 번, i가 2일 때 2번 돌지~ System.out.println("아이디를 입력하세요."); String id=scan.next(); System.out.println("패스워드를 입력하세요."); String pwd=scan.next(); //패스워드는 스트링타입으로 선언해준다 if(id.equals("choi") && pwd.equals("1234")){ System.out.println(id + "님 환영합니다."); break; //아이디와 비밀번호가 맞았을 때 브레이크로 잡아준다. 없으면 맞아도 다시 위(12~)로 올라가 반복한다. }else{ //틀린 경우 System.out.println("아이디 또는 비밀번호가 틀립니다."); } if(i==3){ //3번 모두 틀린 경우 System.out.println("더이상 접속할 수 없습니다"); System.exit(0) ;//프로그램을 강제로 종료하기 } } System.out.println("회원님 ...~~~~어쩌구 저쩌구..~"); } }
[ 과 제 ]