프로그램 예시

 

1. 합 2. 홀수합 3. 짝수합 4. 종료 => 1
원하는 수 ? 10
1~10 합 : 55 

 

이런 프로그램을 작성하시오.

 

입력받은 수, 합계, 프로그램 실행번호

Scanner sc = new Scanner(System.in);

int input, s, program;

 

while(ture){

do {System.out.print("1. 합 2.홀수합 3.짝수합 4.종료 =>");

program=sc.nextInt();

} while(program<1 || program>4);

 

if(program==4){

break;}

}

===========================4를 누르면 종료하는 프로그램을 만듦.

이제 1을 입력하고 숫자를 입력하면 입력한 숫자까지의 합을 구하고

2를 입력하고 숫자를 입력하면 입력한 숫자까지의 홀수합을 구하고

3를 입력하고 숫자를 입력하면 입력한 숫자까지의 짝수합을 구하는 것을 짜야함.

 

if (program ==1) {

System.out.print("원하는 수 ?");

input=sc.nextInt();

for(int num=1; num<=input; num++){

s+=num;

}

} else if (program ==2) {

System.out.print("원하는 수 ?");

input=sc.nextInt();

for(int num=1; num<=input; num+=2){

s+=num;

}

} else  {

System.out.print("원하는 수 ?");

input=sc.nextInt();

for(int num=2; num<=input; num+=2){

s+=num;

}

}

System.out.printf("1 ~ %까지  합 : %d", input, s);

s=0;

}

 

sc.close();

 

선생님께서는 

 

이렇게 하셨다.

변수를 3개를 더 설정함.

String title;

int start, offset;

switch(menu) {

case 1 : title="합 : "; start=1; offset=1; break;

case 2 : title="홀수 합 : "; start=1; offset=2; break;

default : title="짝수 합 : "; start=2; offset=2; break;

}

s=0;

for(int n=start; n<=num; n+=offset) {

s+=n;

}

System.out.println("1~"+num+"까지 "+title+s);

 

내가 짠 코드 보다 훨씬 짧아졌다.

 

이런 생각은 어케하는건지...

for(int i=1; i<=3; i++) {  // i가 1부터 3까지 총 3번 반복

for(int j=1; j<=3; j++) {

// i가 1일때 j 1부터 3까지 3번 반복, i가 2일 때 j 1부터 3까지 반복, i가 3일때 j 1부터 3까지 반복

if(i+j)==4{ // 근데 i+j의 합이 4이면 for문을 나가고 j 더이상 실행x

break;}

}

System.out.println(i+", "+j); // (1, 1) (1, 2) (2, 1) 가 나옴. 

}

 

ga:

for(int i=1; i<=3; i++) {

for(int j=1; j<=3; j++) {

if(i+j == 4){ // i+j 의 합이 4이면 ga를 나가라.

break ga;} 

}

System.out.println(i+ ", "+j); // (1, 1) (1, 2) 

}

 

단, 입력받은 수가 0 이하면 입력을 종료하고 합을 출력 후 프로그램 종료

 

실수

double sum, input;

입력 받는 수 input. 

입력 받은 수들을 저장할 곳 sum

 

Scanner sc = new Scanner(System.in);

 

sum=0;

 

while (true) {

System.out.print("수 ? [종료:0]");

input=sc.nextDouble();

if (input<=0.0) {

break; 

}

sum+=input

}

 

System.out.print("결과 : " + sum);

 

sc.close();

 

if.
1) int형 변수 n이 10 이상이고 20 미만일 때 true인 조건식
if( n>=10 && n< 20 ){}
2) char형 변수 ch가 공백과 탭이 아닐 때 true인 조건식
if( ch!=' ' && ch!='\t' ) {}
3) char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식
if ( ch=='x' || ch=='X' ) {}
4) int형 변수 y가 4의 배수이고 100의 배수가 아니거나 400의 배수이면 ture인 조건식
if ( (y % 4 == 0 && y % 100 !=0) || y % 400 == 0 ) {} // AND, OR연산에서는 AND가 우선순위
5) char형 변수 ch가 영문자 'Y'나 'y'가 아닐 때 true인 조건식
if ( ch !='Y' && ch !='y' ) {}
6) char형 변수 ch가 영문자일 때 true인 조건식
if ( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) {}
if ( ch>='A' && ch<='z' ) 는 안됌. 65~90, 97~122이므로 중간에 7개는 영문자 아닌 값들이 존재

7) char형 변수 ch가 영문자가 아닐 때 true인 조건식
// if ( (ch<'A'&& ch>'Z' && ch<'a' && ch>'z') )
if ( !(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z') ) 
8) boolean형 변수 b가 false일 때 true인 조건식
if ( b == false ) {}
if ( ! b){}

위를 출력하기.

 

1~10까지 출력하고 줄을 바꾸고

2~11까지 출력하고 줄을 바꾸고.

.

.

.

10개의 행

 

for ( int n=1 ; n<=10 ; n++) {  

for ( int m=n ; m<=n+9; m++){

System.out.printf("%3d", m);

}

System.out.println();  //10개의 행 출력.

}

 

1개의 행에는 10개의 수가 있음. 첫번째수와 마지막수의 차이는 9.

 

ex;

** 2단 **

2 * 1 = 2

2 * 2 = 4

.

.

.

2 * 9 = 18

---------

** 3단 **

3 * 1 =3

.

.

.

 

분석 : 2~9단까지 8번 반복

2 가 출력될때 1~9까지 9번 반복.

총 72번이 반복되어야함.

 

int dan, n;

for (dan=2; dan<10; dan++) {

System.out.printf("** %d단 **\n", dan);

for (n=1; n<10; n++){

System.out.printf("%d * %d = %d\n", dan, n, dan*n);

}

System.out.println("-------");

}

for (int i=1; i<=3; i++) {

System.out.println("i:"+i);  // 이 print문은 i=1, i=2, i=3 일때가 나오므로 총 3번이 나옴.

for(int j=1; j<=i; j++) {

System.out.println("i:"+I+", j:"+j); // 이 print문은 i=1일 때 1번, i=2 일 때 2번, i=3일때 3번 나옴.

} // 즉 i=1일때 for 1번 실행, i=2일때 for 2번 실행 ...

System.out.println("--------"); // 이 print문은 i=1, i=2, i=3 일때가 나오므로 총 3번이 나옴.

}

1~100까지 홀수합

int s=0;

for (int n=1; n<=100; n+=2) {

s+=n;

}

for (초기식 ; 조건 ; 증가식) {}

초기식이 조건을 만족하면 {}를 실행하고 증가한다.

 

1~100까지 짝수합

int s=0;

for (int n=2; n<=100; n+=2) {

s+=n;

}

1~100까지 3의 배수의 합

int s=0;

for (int n=3; n<=100; n+=3) {

s+=n;

}

+ Recent posts