Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions JavaStudyCarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Scanner;
public class JavaStudyCarTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Car의 정보(이름, 스피드)를 담을 수 있는 클래스를 따로 선언해주세요.

public static void main(String[] args){
Scanner a = new Scanner(System.in);
System.out.println("자동차의 갯수를 입력하세요.");
int b = a.nextInt();
int[] speed=new int[b]; /*배열 선언과 배열 생성의 차이를 모르겠네요.. 결국 선언 자체가 생성하는 것 아닌가요? 또한 배열은 원소크기를 무조건 설정하거나 원소를 무조건
Copy link
Member

@k0000k k0000k Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int[] speed=new int[b]는 사실 2개의 문장이 합쳐진 형태입니다.
먼저 첫 부분인 int[] speed를 살펴봅시다. 이 부분은 배열을 만든 것이 아닌, int를 담을 수 있는 일반 배열을 가리킬 이름을 선언한 것 입니다. 이를 '레퍼런스를 선언했다' 라고 합니다.
다음은 두 번째 부분인 new int[b]를 봅시다. 이 부분은 new 키워드를 이용해서 int를 b개 담을 수 있는 공간을 실제로 메모리에 할당합니다. 이때부터 배열을 사용할 수 있습니다.
두 개의 문장을 따로 써도 되지만, 편의를 위해 int[] speed=new int[b]라고 씁니다.

넣어야하나요? int[] speed=new int[]로 설정해서 for문으로 값을 입력받을 때 마다 배열의 원소크기가 하나 씩 증가하도록 구상하려 했는데 미리 원소크기를 지정안해서인지
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럴땐 일반 배열이 아닌 ArrayList를 사용하면 됩니다. List계열의 자료 구조는 크기가 정해져 있지 않은 반면, 일반 배열은 생성시에 크기를 지정해야 합니다.

컴파일 오류가 뜨더라구요 */
String[] name=new String[b];
for(int i=0; i<b; i++) { //i++이 아닌 ++i 로 지정하면 b-1번 반복해야하는 것 아닌가요.. i=0부터 시작이 아니라 i=1부터 시작할 거 라 생각해서 한 개 덜 반복 할 줄 알았는데..
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for문의 증감식은 for문 내부의 명령어들이 끝난 뒤에 실행됩니다. 따라서, i++ 또는 ++i 은 차이가 없습니다. 단, i = ++i * 2 또는 i = i++ * 2 와 같은 문장을 증감식에 썼을 때는 연산자의 우선순위에 차이가 생겨 결과가 달라지게 됩니다.

Scanner x = new Scanner(System.in);
System.out.println(i + 1 + "번 째 자동차의 스피드를 입력하세요.");
speed[i] = x.nextInt();
Scanner y = new Scanner(System.in);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanner 객체는 1번만 만들어두면 해당 클래스에서 Close 하기 전까지 계속 사용할 수 있습니다. 따라서 이 문장은 필요하지 않습니다.

System.out.println(i+1+"번 째 자동차의 이름을 입력하세요.");
name[i] = y.nextLine();
}

System.out.println("------경기 참가자 소개-----");
for(int i=0; i<b; i++){
System.out.println("스피드는 "+speed[i]+"이고, 이름은 "+name[i]+"입니다.");
}
/*여기 행에서 scanner.close();로 스캐너를 닫으려 하는데 왜 컴파일 오류가 뜰까요..cannot find symbol variable scanner라고 떠요ㅜ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드를 보면 Scanner를 x와 y라는 이름으로 2개 선언하셨습니다. 따라서 x.close()y.close()로 닫을 수 있습니다.

구글링 nextLine()은 scanner를 못 닫는다는 내용이 있던데 이해가 안되네요..
*/

/*질문있습니당.
scanner
println 순서로 입력하면 결과가 scanner를 받고 print문이 나올 거라 예상이 되는데 실제로는 print문이 먼저 나오더라고요
scanner문을 사용해도 scanner문으로 설정 한 변수를 사용하지 않다면 입력받는 것이 생략이 되더라구요. 즉
Scanner x = new Scanner(System.in);
System.out.println(i + 1 + "번 째 자동차의 스피드를 입력하세요.");
speed[i] = x.nextInt()
를 본다면 Scanner문이 먼저 오더라도 x라는 변수를 사용 할 일이 speed[i] = x.nextInt() 에서 나오니까 print문이 먼저나오고
speed[i] = x.nextInt()이전에 나오는건가요? 제가 필력이 떨어져서 제 질문 내욜을 이해하실 지 모르겠네요ㅜ
*/


}
}