___________________________________
for (초기식 ; 조건식 ; 증감식) {
실행문;
}
___________________________________
do{
실행문;
}while(조건식);
___________________________________
while(조건식){
실행문;
}
___________________________________
로또프로그램
package java0702;
import java.util.Random; import java.util.Scanner;
public class LottoForMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Random r = new Random(); int i, j; int n; /*= r.nextInt(); */ //괄호안에 숫자를 적으면 1부터 숫자 이내의 값에서 랜덤이 추출된다 int b0=0 , b1=0 , b2=0, b3=0, b4=0 , b5=0;
for ( j=0 ; j< 3 ; j++) { for ( i=0 ; i <6 ; i++) {
n= r.nextInt(46)+1;
if ( i==0 ) b0= n;
else if ( i==1 ) { if (n==b0) { i--; continue; } else b1= n; } else if ( i==2 ) { if (n==b0 || n ==b1) { i--; continue; } else b2= n; } else if ( i==3 ) { if (n==b0 || n ==b1 || n ==b2) { i--; continue; } else b3= n; } else if ( i==4 ) { if (n==b0 || n ==b1 || n ==b2 || n ==b3) { i--; continue; } else b4= n; } else if ( i==5 ) { if (n==b0 || n ==b1 || n ==b2 || n ==b3 || n ==b4) { i--; continue; } else b5= n; } System.out.printf("- %d \t", n); } System.out.printf("=======================\n\n");
////////////////////////////////////////////////////////////////////
for ( j=0 ; j< 5 ; j++) { for ( i=0 ; i < 6 ; i++) {
n= r.nextInt(46)+1; if ( i==0 ) b0= n;
else if ( i ==1 ) { if (b0 !=n ) b1 = n; else { i--; continue; } } else if ( i ==2 ) { if ( b0 !=n && b1 !=n ) b2 = n; else { i--; continue; } } else if ( i ==3 ) { if (b0 !=n && b1 !=n && b2 !=n ) b3 = n; else { i--; continue; } } else if ( i ==4 ) { if (b0 !=n && b1 !=n && b2 !=n && b3 !=n ) b4 = n; else { i--; continue; } } else if ( i ==5 ) { if (b0 !=n && b1 !=n && b2 !=n && b3 !=n && b4 !=n ) b5 = n; else { i--; continue; } }
System.out.printf("- %d \t", n); } System.out.println(); }
}
}
//////////////////////////////////////////////////////////////////// // 다른 문제풀이
import java.util.Random;
public class LottoForMain {
public static void main(String[] args) { Random r = new Random(); int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0;
for (int i = 0; i < 6; i++) { int n = r.nextInt(45); if (i == 0) b1 = n; else if (i == 1) { if (b1 != b2 && n != 0) b2 = n; } else if (i == 2) { if (b1 != b2 && b1 != b3 && b2 != b3 && n != 0) b3 = n; } else if (i == 3) { if (b1 != b2 && b1 != b3 && b2 != b3 && b2 != b4 && b3 != b4 && n != 0) b4 = n; } else if (i == 4) { if (b1 != b2 && b1 != b3 && b2 != b3 && b2 != b4 && b3 != b4 && b3 != b5 && b4 != b5 && n != 0) b5 = n; } else if (i == 5) { if (b1 != b2 && b1 != b3 && b2 != b3 && b2 != b4 && b3 != b4 && b3 != b5 && b4 != b5 && b4 != b6 && b5 != b6 && n != 0) b6 = n; } } System.out.println(b1 + " " + b2 + " " + b3 + " " + b4 + " " + b5 + " " + b6); } }
|
배열을 배우기 전이라 볼넘버를 담을 변수를 6개 생성하여 넣어주고
중복될 경우 제외시켜줬다.
이것을 배열을 사용하여 바꾸면 아래 코드처럼 사용할 수 있다
Scanner sc = new Scanner(System.in); Random r = new Random();
int i, j, k; int n; int ballCount=6; int randomNumber = 45; int[] ball = new int[ballCount];
for (i=0; i < ball.length ; i++) { n = r.nextInt(randomNumber)+1; boolean isContain = false;
for (j=0; j<i; j++) { if (ball[j]==n) { isContain = true; break; //없어도무관 ,검사 더 하냐마냐차이 } }
if (isContain == false) ball[i]=n; // (isContain == false) 과 ( !isContain ) 은 같은 뜻 else { i--; continue; } }
for ( i=0; i< ball.length ; i++) { System.out.printf("- %d \t", ball[i]); } System.out.println(); |
=======================================
삼항 연산자 예제
박스는 농구공을 5개씩 담을 수 있다
농구공 갯수에 따라 박스 갯수를 구하라
import java.util.Scanner;
public class Ball {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.println("농구공 개수 입력 >> "); int ans= sc.nextInt();
int box=0;
box=((ans%5==0) ? (ans/5) : (ans/5+1)) ; // 삼항 연산자가 ((ans%5==0) ? (box= ans/5) : (box= ans/5+1)) ; 이런식으로 // 안에서 다른걸로 초기화 하는 건 안된다
System.out.println("박스는 " + box + "개 필요하다 ");
}
}
|
=======================================
데이터 치환하기,
삼항 연산자 예제
import java.util.Scanner;
public class Change {
public static void main(String[] args) { // 데이터 치환하기 int a =10; int b =15; int t=0; t=a; a=b; b=t; System.out.println(a + " , " + b);
Scanner sc = new Scanner(System.in); System.out.println("정수 입력 >> "); int ans= sc.nextInt(); String ai;
//방법1 (ans%2==0) ? ai="짝수 " : ai="홀수" ; System.out.println(ans + " 은" + a); //방법2 (ans%2==0) ? System.out.println(ans + "은 짝수다 "); : System.out.println(ans + "은 홀수다 "); //방법3
System.out.println(ans +"는 " + (ans%2==0 ? "짝수" : "홀수") + "이다");
}
}
|
=======================================
switch ~ case
몇월이냐는 질문에 알맞은 답 출력하기
연습하기 예제
sol1)
System.out.print("몇 월? >> "); int m= sc.nextInt(); String ma= "아";
switch (m) { case (12) : case (1) : case (2) : ma="겨울"; break;
case (3) : case (4) : case (5) : ma="봄"; break;
case (6) : case (7) : case (8) : ma="여름"; break;
case (9): case (10): case (11): ma="가을"; break;
default : ma = "엥 ? "; break; } |
sol2)
switch (m) {
case 12 :case 1 :case 2 : ma="겨울"; break; case 3 :case 4 : case 5 : ma="봄"; break; case 6 : case 7 : case 8 : ma="여름"; break; case 9 : case 10 : case 11: ma="가을"; break; }
System.out.println(ma + " 이다 !"); |
=======================================
switch ~ case
점수를 입력하면 그에 해당하는 학점을 출력하라
십의자리라는 공통점이있으니 그것을 중심으로 만든다 >
System.out.print("점수 입력 >> "); int to= sc.nextInt();
to/=10;
String s1;
switch (to) {
case 10 : s1="A+"; break; case 9 : s1="A"; break; case 8: s1="B"; break; case 7: s1="C"; break; default: s1="D"; break; } System.out.println(s1 + "학점" +"\n\n"); System.out.println("========================================="); |
=======================================
임의의 두 값의 덧셈을 맞춰라
맞출경우 새로운 문제가 출제되지만
틀릴경우 같은 문제가 출력된다
import java.util.Random; import java.util.Scanner;
public class PlusGame { public static void main(String[] args) {
//=============================1
int num11= r.nextInt(20)+1; int num12= r.nextInt(20)+1; int inputResult boolean check = true ;
while(true){
if (check==true){ num11= r.nextInt(20)+1; num12= r.nextInt(20)+1; }
System.out.printf("%d + %d = ? 답 >> " , num11, num12); inputResult= sc.nextInt();
if (num1+num2 == inputResult){ System.out.prlintf("Success"); check=true; } else { System.out.prlintf("Fail"); check=false; } System.out.printf("계속 하시겠습니까? >> "); ans3= sc.next();
if ( ans3.equals("N") || ans3.equals("n") ) break; else System.out.printf("잘못입력했으니 다시 하세요 >>> ");
}
//=============================2
Scanner sc = new Scanner(System.in); Random r = new Random(); int num1, num2 , num3=0 ; String ans1 ="Y", ans2 ="y", ans3;
num1=r.nextInt(20)+1; num2=r.nextInt(20)+1;
while (true) {
System.out.printf("%d + %d = ? 답>> " , num1 , num2 ); num3= sc.nextInt();
if (num3!=(num1+num2)) System.out.println("F A I L \n");
else if (num3==(num1+num2)){ System.out.println("S U C C E S S \n"); num1=r.nextInt(20)+1; num2=r.nextInt(20)+1; }
System.out.printf("계속 하시겠습니까? >> "); ans3= sc.next();
if ( ans3.equals(ans1) || ans3.equals(ans2) ) continue; else if ( ans3.equals("N") || ans3.equals("n") ) break; else System.out.printf("다시 입력 하세요 >>> ");
}
} }
|
=======================================
for문으로
구구단 출력
import java.util.Random; import java.util.Scanner;
public class Ex_for1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Random r = new Random();
int i, j, k, to = 0; int num1, num2; for ( i=1 ; i<=100 ; i++) { if (i%2==0) System.out.printf("%d " ,-i);
else System.out.printf("%d " ,i);
}
///////////////////구구단
System.out.printf("몇단부터 ? >> "); num1= sc.nextInt();
System.out.printf("몇단까지 ? >> "); num2= sc.nextInt();
for (i=num1; i<=num2 ; i++) { for ( j=1 ; j<10 ; j++) System.out.printf("%d X %d = %3d \n", i, j ,i*j); System.out.println(); }
//////////////////////////
System.out.printf("몇단부터 ? >> "); num1= sc.nextInt();
System.out.printf("몇 곱 까지 ? >> "); num2= sc.nextInt();
for (i=num1; i<=num2 ; i++) {
for ( j=1 ; j<=num2 ; j++) System.out.printf("%d X %d = %d \n", i, j ,i*j); System.out.println(); }
//////////////////////////
System.out.printf("몇단부터 ? >> "); num1 = sc.nextInt();
System.out.printf("몇단까지 ? >> "); num2 = sc.nextInt();
for (i = 1; i < 10; i++) { for (j = num1; j <= num2; j++) System.out.printf("%d X %d = %3d\t", j, i, i * j); System.out.println(); } System.out.println();
}
}
|
=======================================
랜덤함수 연습과
정렬예비 연습
import java.util.Random; import java.util.Scanner;
public class Ex_for {
public static void main(String[] args) { // 1 부터 10까지 출력
Scanner sc = new Scanner(System.in); Random r = new Random();
int i; int start=1; int end=100;
/////////////////1 for (i = start; i<=end ; i++) { System.out.printf("%d ", i); }
for (i = start; i<=end ; i++) { System.out.printf("%c ", i); } System.out.println("\n");
for (char i1 = 'A'; i1<='Z' ; i1++) { System.out.printf("%c ", i1); }
////////////////////////
int num1, num2 , num3=0;
System.out.print("100 이하의 정수를 두개 입력하라 >> "); num1 =sc.nextInt(); num2 =sc.nextInt(); //1순위 num1 > 2순위 num2 if (num2>=num1) { num3=num1; num1=num2; num2=num3; }
for ( i= num2 ; i <= num1 ; i++) System.out.printf("%d ", i);
///////////1 부터 15까지 합 int to1=0; for (i=1 ; i<16 ; i++) { to1+=i; } System.out.printf("총 합 >> %d ", to1); ////////////////////////
int num11, num12 , num13=0;
System.out.print("정수를 두개 입력하라 >> "); num11 =sc.nextInt(); num12 =sc.nextInt(); //1순위 num1 > 2순위 num2 if (num12>=num11) { num13=num11; num11=num12; num12=num13; } int to2=0;
for ( i= num12 ; i <= num11 ; i++) { to2+=i; } System.out.printf("총 합 >> %d ", to2);
}
}
|
=======================================
for문 예제
구구단 출력
입력받은 정수의 약수를 구하기
완전수 구하기
import java.util.Random; import java.util.Scanner;
public class For99 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); Random r = new Random();
int i , j , k ; int ans;
ans=2;
// 2단만 출력 for ( i=1 ; i< 10 ; i++) { System.out.printf("%d X %d = %d\n", ans , i ,ans*i); } // 2단부터 9단까지 출력 for (j=2 ; j<10 ; j++) { System.out.printf("====%d단====\n", j);
for ( i=1 ; i< 10 ; i++) System.out.printf("%d X %d = %d\n", j , i ,j*i);
System.out.println(); }
// 가로 구구단 for (j=2 ; j<10 ; j++) { System.out.printf("%2d단 >> ", j); for ( i=1 ; i< 10 ; i++) System.out.printf("%d X %d = %3d\t", j , i ,j*i);
System.out.println(); }
//세로 구구단 for (j=1 ; j<10 ; j++) { for ( i=2 ; i< 10 ; i++) System.out.printf("%d X %d = %3d\t", i , j ,j*i); System.out.println(); }
// 입력받은 정수의 약수를 구하세요
System.out.printf("정수 입력 >> "); ans = sc.nextInt();
System.out.printf("%d 의 약수 >> ", ans);
for (i=1 ; i<=ans ; i++) { if (ans % i==0) System.out.printf(" %d", i);
}
// 2부터 30까지의 약수를 구하라 for (j=2 ; j<=30 ; j++) { System.out.printf("%2d 의 약수 >> ", j);
for (i=1 ; i<=j ; i++) if (j % i==0) System.out.printf(" %3d", i);
System.out.printf("\n"); }
// 완전수 구하기 int to=0;
//sol1 for (k=1 ; k<=1000 ; k++) { //1 부터 100까지 검사
for (i=1 ; i<k ; i++) { // 약수를 구하기 위한 폴문 && 자기자신이 아니라면 if (k % i==0) to+=i; // 약수일 경우 total에 더해준다 } if (to==k) //total(약수의총합) == k(검수하는 수) 같다면 System.out.printf(" %d 는 완전 수\n" ,to); //total 을 출력 to=0; //같을 경우 + 같지않을 경우 = 모두 to를 초기화 시켜준다
}
//sol2 for (k=1 ; k<=1000 ; k++) {
for (i=1 ; i<=k/2 ; i++) //x 약수의 최대값은 x의 절반이다 ( 1 제외 ) if (k % i==0) to+=i;
if (to==k) System.out.printf(" %d 는 완전 수\n" ,to); to=0; }
} }
|
=======================================
[Java] rand 값 사용방법 (0) | 2019.07.05 |
---|---|
[Java] 수정글 확인 킬킬 (0) | 2019.07.04 |
[Java] 자바 형변환 , 비트연산자 (0) | 2019.07.03 |
[Java] 자바 기본 문제풀이 & 연습풀이 (0) | 2019.07.03 |
[Java] 출력 문제풀이 (0) | 2019.07.02 |
댓글 영역