<div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">import java.util.Scanner;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">import java.util.Arrays;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">public class CodeVer2 {</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>public static void main(String[] args) </div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>{</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>Double num1, num2, num3, num4, num5, sum, avg;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>Scanner keyboard = new Scanner(System.in);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Enter the first number:");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num1 = keyboard.nextDouble();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Enter the seond number:");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num2 = keyboard.nextDouble();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Enter the third number:");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num3 = keyboard.nextDouble();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Enter the fourth number:");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num4 = keyboard.nextDouble();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Enter the fifth number:");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num5 = keyboard.nextDouble();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>keyboard.close();</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>double[] num = new double[5];<font color="#ff0000"> // num 이란 이름의 배열이 5개의 변수로 만들어집니다.</font></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num[0] = num1;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num[1] = num2;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num[2] = num3;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num[3] = num4;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>num[4] = num5;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>Arrays.sort(num); <font color="#ff0000">// 배열이 최소값->최대값으로 정렬됩니다.</font></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>double[] data = {num1, num2, num3, num4, num5}; <font color="#ff0000">//최빈값(제일 많이 input이 된 수) 을 찾기위해 데이터를 만듭 니다.</font></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>int mode = 0;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>int[] index = <span style="background-color: #e4ff75">new int[99990];</span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>int maximum = Integer.MIN_VALUE;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>for (int i = 0; i < data.length; i++){</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>index[(int) data[i]]++;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>}</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>for (int i = 0; i < index.length; i++){</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>if(maximum < index[i]){</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>maximum = (int) index[i];</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>mode = i;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>}</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>}</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>sum = num[0] + num[1] + num[2] + num[3] + num[4];</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>avg = sum/5;</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println(" ");</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Sum: " + sum);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Average:" + avg);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Maximum:" + num[4]);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Minimum:" + num[0]);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Median: " + num[2]);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>System.out.println("Mode: " + mode);</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><span class="Apple-tab-span" style="white-space: pre"> </span>}</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">}</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">위 코드가 제가 짠 코드인데요...</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">최빈값 부분에서 로직에러가 생기네요.</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">1) <span style="background-color: #e4ff75"> </span> 이렇게 색칠된 부분인데요.</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"> 그 부분에 int 에서 가능한 최대값인 2의 32승 -1의 값을 넣으면</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"> 메모리 부족 에러가 뜨면서 실행이 안됩니다.</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">2) 만약에 사용자가 넣은 값이 11,22,11,22,33 이렇게 5개의 수라면</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"> 11과 22둘다 최빈값이 되어야하는데 11밖에 안 뜨네요.</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"> (1,2,3,4,5 를 넣었을 때 1,2,3,4,5 전부 한번씩 있기 때문에 최빈값은 1,2,3,4,5 인데 1이라고만 뜨구요)</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px"><br /></div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">초보라서 그런지 많이 서투네요...</div> <div style="font-family: 돋움, dotum, Helvetica, sans-serif; list-style: none; margin: 0px; padding: 0px; line-height: 16.799999237060547px">혹시 괜찮은 해결방법이 없을까요? </div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.