상냥한 세상 :: [대학교 과제]Columbia University COMS|Introduction of Java 1st Project

본문으로 바로가기

  • 문제


  • 코드 1
import java.util.Scanner;


//Exception in thread "main" java.lang.ArithmeticException: / by zero: 분모가 0일때 오류발생
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        double sum = 0;
        double count = 0;
        //count=1을 해버리면 while문에서 count<=lengthCount를 해야한다.
        //그래야 19번째 줄에서 endKey에 4를 입력하면 count=1~4까지 입력받기 때문이다.
        //count<lengthCount도 안하고 count=1만 선언하면 5개를 입력하라고 만든다. 그래서 count=0시에 lengthCount는 <=가 아니라<이여야함.
        double average = 0;
        double E;
        double lengthCount;
        String endKey;
        System.out.println("Do you play the game?");
        endKey = scan.nextLine();

        if (endKey.equals("stop")) {
            System.out.println("Program END");
            System.exit(0);
        }


        if (endKey.equals("yes")){
            System.out.println("How many count do you want?");
            lengthCount=scan.nextInt();

            System.out.println("Enter your number to calculate average");

            while (count<lengthCount){
                E = scan.nextInt();
                if (E < 0) {
                    lengthCount--;
                    continue;
                    //여기까진 들어감
                }
                sum+=E;
                count+=1;

                /*
                if(endKey.equals("stop")){
                이렇게 적으면 if안에서 숫자 숫자 숫자 숫자 stop 엔터 를 적으면 stop적은걸 읽는게아니라 엔터를 읽어버려서 if문 안 메소드를 무시해버린다.

                    System.out.println("Program End");

                    //count-1이유는 4개를 입력하면 count가 4여야하는데 실제론 1로 선언된 count때문에 5로 간주되므로
                    average = sum / (count-1);
                    System.out.println("result: "+ average);
                    break;

                }
                */

            }
                    //대체 else를 왜 안건드는거지?


            System.out.println("sum1 : " + sum);
            System.out.println("count1: "+ count);
            System.out.println("result1: "+sum/(count));



            }
        }
    }

결과문


  • 코드 2 
    - 다른버전 : -1을 어떤곳에 입력하든 -1이전까지만 숫자를 받아서 나누고 평균을 낸다
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);

        double sum=0;
        double count=0;
        double average=0;
        double E=0;

        System.out.println("How many number do you want to calculate");
        double countLength=in.nextInt();


        System.out.println("Okay.. then please put the number");
        while(count<countLength) {
            E = in.nextInt();

            if (E == -1) {
                average = (sum / count);
                break;
            }
            sum += E;
            count += 1;
        }
        System.out.println("sum1: "+sum);
        System.out.println("count1: "+count);
        System.out.println(average);
    }
}

'Computer Science > Java' 카테고리의 다른 글

Introduction of Java-2.5(1)  (0) 2020.05.16
Introduction of Java-2  (0) 2020.05.12
Introduction of Java-1  (1) 2020.05.11