Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- opener
- javaBeans
- session.removeAttribute
- 팝업창
- 버리자
- iframe
- 데이터사이언스
- \
- 향상된 for문
- target
- ;
- 파일호출
- session.getAttribute
- 미래직장
- 영감
- 동기부여
- Alert
- 페이지 이동
- 로그인화면
- BEANS
- static
- Import
- 자바빈즈
- 빈즈
- session.setAttribute
- "
- 파일 호출
- 페이지이동
- 동기 부여
- scanner
Archives
- Today
- Total
갈림길 이정표
[4. 조건문과 반복문] 반복문 problem 1~3 본문
package pack1;
public class Test7_problem1 {
public static void main(String[] args) {
//문제 1. 1~100사이의 숫자 중 3의 배수이나 2의 배수가 아닌 수를 출력하고, 그 합과 건수 출력
System.out.println("----problem1----");
int w = 1;
int sum = 0;
int num = 0;
System.out.print("3의 배수이나 2의 배수가 아닌 수: ");
System.out.println();
while(w <= 100) {
if(w%3 == 0 && w%2 != 0) {
System.out.print(w + " ");
sum += w;
num++;
}
w++;
}
System.out.println();
System.out.println("총 합: " + sum);
System.out.println("갯수: " + num);
}
}
package pack1;
public class Test7_problem2 {
public static void main(String[] args) {
//문제 2. -1, 3, -5, 7, -9, 11, ... , 99까지의 합?
// System.out.println("----problem2----");
// int q = 1;
// int sum1 = 0;
//// int sum2 = 0;
// while (q<=99) {
// if(((q-1)/2)%2 == 0) {
// int p = (-1) * q;
// sum1 += p;
// }else {
// sum1 += q;
// }
// q += 2;
// }
// System.out.println("합: " + sum1);
System.out.println("----problem2----");
int q = 0;
int sum1 = 0;
while (q<=99) {
if(((q+1)/2)%2 != 0) {
int p = (-1) * (q+1);
sum1 += p;
}else {
sum1 += (q+1);
}
q += 2;
}
System.out.println("합: " + sum1);
//
}
}
package pack1;
import java.util.Scanner;
public class Test7_problem3 {
public static void main(String[] args) {
//문제 3. 키보드로 숫자 입력: 5
// 5까지의 합 출력
// 계속할까요?(1/0) -> 1이면 계속 0이면 작업 끝
System.out.println("----problem3----");
int start = 1;
int sum = 0;
while(true) {
Scanner cs = new Scanner(System.in);
System.out.print("1~? 까지의 합: ");
int fini = cs.nextInt();
while(start<=fini) {
sum += start;
start++;
}
System.out.println("1~" + fini + " 까지의 합은: " + sum);
System.out.println("다시 할래?" + "(1: 응, 0: 아니)");
Scanner cs1 = new Scanner(System.in);
System.out.print("대답: ");
int deci = cs1.nextInt();
if(deci == 1 || deci != 0) { //== 중요
while(deci != 1 || deci !=0) {
System.out.println("무한루프나 먹어라");
Scanner cs2 = new Scanner(System.in);
}
continue;
}else { //== 중요 (=: 대입, ==: 일치)
System.out.println("감사합니다");
break;
}
}
System.out.println("프로그램 종료");
}
}
'Programming Language > Java 마스터하기(feat. 이것이 자바다)' 카테고리의 다른 글
[6. 클래스] (5) | 2020.07.19 |
---|---|
[5. 참조 타입] (0) | 2020.07.19 |
[4. 조건문과 반복문] 조건문 problem 1~5 + α (0) | 2020.07.19 |
[4. 조건문과 반복문] (0) | 2020.07.19 |
[3. 연산자] (0) | 2020.07.19 |
Comments