* 연산 퀴즈
by mini_min◾ 1 - 2 + 3 - 4 + 5 ... 짝수는 빼고 홀수는 더하기 (10까지)
public class Quiz002_Hap {
public static void main(String[] args) {
int n = 0, s = 0;
while (n < 10) {
n++;
s += n;
n++;
s -= n;
}
System.out.println("결과 : " + s);
}
}
◾ A ~ Z 까지 영문자 한 줄에 5개씩 출력
public class Quiz003_Alphabet5 {
public static void main(String[] args) {
char ch = 'A';
int cnt;
cnt = 0;
while (ch <= 'Z') {
System.out.print(ch + "\t");
cnt++;
if (cnt == 5) {
System.out.println();
cnt = 0;
}
ch++;
}
}
}
◾ 1~100 수 중 3또는 5 배수 합과 평균
public class Quiz004_Multiple35 {
public static void main(String[] args) {
int n, s, cnt;
n = s = cnt = 0;
while (n < 100) {
n++;
if (n % 3 == 0 || n % 5 == 0) {
// System.out.print(n+"\t");
s += n;
cnt++; //갯수 카운트! 평균 구하려면 갯수 알아야하기 때문이다.
}
}
System.out.println("합 : " + s);
System.out.println("평균 : " + (s / cnt));
}
}
◾ 두 정수를 입력 받아 입력 받은 수중 적은 수에서 큰 수까지의 합
public class Quiz006_Hap {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2, temp;
int n, s;
System.out.print("두수 ? ");
num1 = sc.nextInt();
num2 = sc.nextInt();
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
s = 0;
n = num1; ///// n이란 변수 사용하기 . 그럼 temp = b; 할 필요없음
while (n <= num2) {
s += n;
n++;
}
System.out.printf("%d ~ %d 까지의 합 = %d\n", num1, num2, s);
sc.close();
}
}
◾ x는 100부터 시작해서 1초마다 2씩 증가하고 y는 0부터 시작해서 1초마다 5씩 증가 한다.
몇 초 후에 y가 x를 따라 잡는지와 따라 잡았을 때 x와 y의 값
public class Quiz007_Time {
public static void main(String[] args) {
int x = 100, y = 0;
int s = 0;
while (x >= y) {
s++;
x += 2;
y += 5;
}
System.out.println("x : " + x + ", y : " + y);
System.out.println("걸린시간 : " + s + "초");
}
}
◾ 1~100 사이 난수를 100개 발생시켜 10개씩 출력하기
n = (int)(Math.random()*100) + 1
Math.random() 는 0 <= 난수 < 1 사이의 실수가 나온다!
public static void main(String[] args) {
int n;
int cnt; //갯수 100개 발생시킬 것
cnt = 0;
while(cnt < 100) {
cnt++;
n = (int)(Math.random()*100) + 1; // 0~99까지 숫자 나옴 + 1 = 1~100 사이 난수가 나오게 됨.
System.out.printf("%5d", n); // 5칸씩 찍어내라. (간격)
if(cnt%10 == 0) {
System.out.println();
}
}
}
'개발 공부중 > 📑 코드 복습' 카테고리의 다른 글
11. Array 배열 + 퀴즈 (0) | 2023.02.24 |
---|---|
10. while 문 (0) | 2023.02.24 |
9. for 문 (0) | 2023.02.22 |
8. switch case 문 (1) | 2023.02.22 |
7. IF 제어문 + 퀴즈 (0) | 2023.02.22 |
블로그의 정보
개발자 미니민의 개발로그
mini_min