본문 바로가기
🍺JAVA

[JAVA] N을 이용하여 홀수합 더하기

by 김말자 2022. 12. 29.
728x90
728x90
BIG
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n;
		int i = 0;
		System.out.println("N값입력");
		while(true) {
			 try {
			      n=sc.nextInt();
			   }catch(InputMismatchException e) {
			      System.out.println("정수값을 입력하세요");
			   //버퍼 비우기
			      sc.nextLine(); //1줄 읽기
			      continue; //반복문 조건으로 분기
			   }
			if(n>1) {
			for(i = 1; i <= n; i++) {
				if(i%2==1) {
				i= i + 2;
				}	
			}}
		break;
		}
	System.out.println(i);
	}

728x90
반응형
BIG

댓글