Error Case

[Java] int 와 long 차이

HoSeongYu 2021. 12. 18. 13:18

[Code]

import java.util.*;
class Main {  
  public static void main(String args[]) { 
    Scanner scan = new Scanner(System.in);
    int S = scan.nextInt();
    System.out.println(maxcnt(S));
  } 

  public static int maxcnt(int S){
    int max = 0;
    int cnt = 0;
    int i=1;
    while(max <= S){
      max += i;
      i++;
      cnt++;
    }
    cnt--;
    return cnt;
  }
}

[Error Message]

java.util.InputMismatchException


[Solution]

타입변경 : int -> long

 

[결론]

int는

-2147483648 ~ 2147483647

long은

-9223372036854775808 ~ 9223372036854775807

까지 표현할 수 있습니다.