My footsteps

code 실습 / 4️⃣ 본문

국비수업/CODE

code 실습 / 4️⃣

밀김 2023. 2. 21. 14:42
728x90

 

 

 

 

✏️Scanner를 통해 다음을 입력받으세요.

20 30 29 39 49 38 10 19 87 29 38 27 8 90 87 -1

 

1번 : -1을 제외한 값들의 개수를 구하는 코드를 작성해보세요

15가 출력되면 정답입니다!

   Scanner sc = new Scanner(System.in);

   int count = 0;

   int input = 0;

   while(input != -1) {

 

   }

package ex1;
import java.util.Scanner;

public class prictice1 {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int count = 0;//값의 총개수 저장하는 변수 어떤 경우에 올라가는지 -1빼고...
		int input = 0;//system.in while문안에서 입력받는 역할
		
		while(input != -1) { //조건식이 거짓일경우 while문 종료니까,-1이 input에 저장되면 종료
			input = sc.nextInt();
			count++;
		}
		
		System.out.println(count-1);
	
	}
}

 

 

 

 

 

 

 

 

2번: 이 값들 중 가장 큰값이 무엇인지 출력하는 코드를 작성해보세요

90이 출력되면 정답입니다

   int max = -1;

package ex1;
import java.util.Scanner;

public class prictice1 {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int count = 0;//값의 총개수 저장하는 변수 어떤 경우에 올라가는지 -1빼고...
		int input = 0;//system.in while문안에서 입력받는 역할
		int max = -1;
		
		while(max<90) { //조건식이 거짓일경우 while문 종료니까,-1이 input에 저장되면 종료
			input = sc.nextInt();
			max = sc.nextInt();
			
			if(input<max)
			count++;
		}
		System.out.println(max);
	
	}
}

 

 

 

 

 

 

 

 

3번 : 10이 몇번째인지 index를 출력하는 코드를 작성해보세요

10은 7번째에 있으나, 이 문제는 배열의 예습으로, 6이 출력되야 합니다.

index는 0부터 시작하거든요. 즉, 20의 index는 0이에요

   int index = -1;

package ex1;
import java.util.Scanner;

public class prictice1 {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int count = 0;//횟수를 저장하는 공간.
		int input = 0;//system.in while문안에서 입력받는 역할
		int index = -1;
		
		//입력할때마다 카운트를 올리고,10을만나면 그때 그 카운트를 인덱스에 저장.for문 못씀 
		
		while(input != -1) { 
			input = sc.nextInt();
			count++;
			if(input==10)
				index = count-1;
		}
		System.out.println(index);
	}
}

 

 

 

 

 

 

 

728x90

'국비수업 > CODE' 카테고리의 다른 글

code 실습 / 6️⃣  (0) 2023.02.23
code 실습 / 5️⃣  (0) 2023.02.22
code 실습 / 3️⃣  (0) 2023.02.20
code 복습🤔  (0) 2023.02.18
code 실습 / 2️⃣  (0) 2023.02.17