(7) 문제

JAVA/간단한 코드 2018. 10. 24. 19:53
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package Studypackage;
 
import java.util.Scanner;
 
public class studyclass 
{
    // for , while : 1 ~ 100 까지 합계 구하기
    public static void main(String[] args)
    {
        int sum = 0;
        int i = 0;
        
        for(i=1;i<=100;i++)
        {
            sum += i;
        }
        System.out.println("for");
        System.out.println("i = " + i);
        System.out.println("1 ~ 100 까지 합계 = " + sum);
        System.out.println();

        
        sum = 0;
        i = 0;
        
        while(true)
        {
            i++;
            sum += i;
            if(i == 100)
            {
                break;
            }
        }
        System.out.println("while");
        System.out.println("i = " + i);
        System.out.println("1 ~ 100 까지 합계 = " + sum);
        System.out.println();

             
        System.out.println("a 부터 b 까지 합계");
        int a , b;
        Scanner sc = new Scanner(System.in);
        System.out.print("a 입력 >>> ");
        a = sc.nextInt();
        do
        {
            System.out.print("b 입력 (a보다 큰 값) >>> ");
            b = sc.nextInt();
        }while(b <= a);
        
        sc.close();
        
        i = a; sum = 0;
        
        while(i<=b)
        {
            sum += i;
            System.out.println("i = " + i++);
            System.out.println("sum = " + sum);
            
        }
        System.out.println(a + " 부터 " + b + " 까지  합계 : " + sum);


    }
}
 
 
 
cs



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Studypackage;
 
import java.util.Random;
import java.util.Scanner;
 
public class studyclass 
{
 
    public static void main(String[] args)
    {
        int i = 0;
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        int sum = 0;
        int o = 0;
        int x = 0;
        
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("[[초등학생 수학문제 풀기]]");
        System.out.println("덧셈 문제 10개 맞춰 보기");
        System.out.println();
        
        for(i=0;i<10;i++)
        {
            num1 = r.nextInt(89)+10;
            num2 = r.nextInt(89)+10;
            
            System.out.printf("%d + %d = ",num1,num2);
            num3 = num1 + num2;
            sum = sc.nextInt();
            if(num3 == sum)
            {
                System.out.println("정답입니다.");
                o++;
            
            }
            else
            {
                System.out.println("오답입니다.");
                x++;
            
            }
        }
        System.out.println();
        System.out.println("==========");
        System.out.printf("정답 개수 : %d\n",o);
        System.out.printf("오답 개수 : %d\n",x);
        System.out.println("==========");



    }
}
 
cs


'JAVA > 간단한 코드' 카테고리의 다른 글

(11) 문제  (0) 2018.10.30
(10) 문제  (0) 2018.10.29
(9) 문제  (0) 2018.10.28
(5) 문제  (0) 2018.10.22
(4) 문제  (0) 2018.10.21

설정

트랙백

댓글