728x90
사용자에게 행의 크기를 입력 받고 그 수만큼의 반복을 통해 열의 크기도 받아
문자형 가변 배열을 선언 및 할당
그리고 각 인덱스에 'a' 부터 총 인덱스의 개수만큼 하나씩 늘려 저장하고 출력
출력:
행의 크기 : 4
0열의 크기 : 2
1열의 크기 : 6
2열의 크기 : 3
3열의 크기 : 5
a b
c d e f g h
i j k
l m n o p
해답코드~
import java.util.Scanner;
public class referance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("행의 크기 : ");
int rowNum = sc.nextInt();
// 행의크기, rowNum을 입력 받습니다.
int colNum;
char ch = 'a';
int[][] ary1 = new int[rowNum][];
// 다차원배열인 ary1의 행 부분에 입력받은 rowNum을 대입해줍니다.
for(int i=0; i<ary1.length; i++) {
// rowNum에 입력된 숫자만큼 for 문을 반복합니다.
System.out.print( i + "열의 크기 : ");
colNum = sc.nextInt();
// rowNum 에서 설정된 갯수 만큼의 colNum을 입력받습니다.
ary1[i] = new int[colNum];
// ary1[i], 즉의 각 행마다 colNum의 만큼의 '열'을 갖는 새로운 행렬을 만듭니다.
// ex) ary1[0][2] , ary1[1][2]....
}
System.out.println();
for(int i=0; i<ary1.length; i++) {
for(int j=0; j<ary1[i].length; j++) {
// ary1의 길이만큼 반복하면서 ary1[i] = new int[colNum]에서 설정한 열의 크기만큼 다시 반복합니다.
// == ary1[i][colNum]
System.out.print(ch + " ");
// 한 행당 해당 열만큼 ch + " " 를 출력합니다.
ch++;
// char형 ch의 값인 a의 아스키코드값을 계속 더해줍니다.
}
System.out.println();
// 해당 행의 반복 끝에 줄바꿈을 해줍니다.
}
}
}
결과 :
행의 크기 : 4
0열의 크기 : 3
1열의 크기 : 2
2열의 크기 : 3
3열의 크기 : 5
a b c
d e
f g h
i j k l m
String 2차원 배열 6행 6열을 만들고 맨위와 제일 앞 열은 각 인덱스를 저장합니다.
그리고 사용자에게 행과 열을 입력 받아 해당 좌표의 값을 'X'로 변환해 2차원 배열을 출력합니다.
출력 :
행 인덱스 입력 : 4
열 인덱스 입력 : 2
0 1 2 3 4 5
0
1
2
3 X
4
5
해답
import java.util.Scanner;
public class referance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("행 인덱스 입력 : ");
int rowN = sc.nextInt();
System.out.print("열 인덱스 입력 : ");
int colN = sc.nextInt();
int a=0;
int b=0;
String[][] arr1 = new String[6][6];
for(int i=0; i<arr1.length; i++) {
for(int j=0; j<arr1[i].length; j++) {
arr1[i][j] = " ";
arr1[0][j] = String.valueOf(a);
arr1[i][0] = String.valueOf(b);
arr1[0][0] = " ";
a++;
}
a=0;
b++;
}
arr1[rowN][colN] = "X";
//출력부
for(int i=0; i<arr1.length; i++) {
for(int j=0; j<arr1[i].length; j++) {
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}
행 인덱스 입력 : 3
열 인덱스 입력 : 4
1 2 3 4 5
1
2
3 X
4
5
주어진 배열항목의 최대값을 출력하는 코드를 작성해보세요.
int[] array = {1,5,3,8,2};
public class java0110 {
public static void main(String[] args) {
int[] array = {1,5,3,8,2};
int max = array[0];
//최대값 max를 array[0]으로 선언 - 초기화
for(int i=0; i<array.length; i++) {
if(array[i]>max) {
max = array[i];
}
}
System.out.println("최대값은 : " + max);
}
}
결과 :
최대값은 : 8
주어진 배열 항목의 전체 합과 평균을 구해 출력하는 코드를 작성해보세요(중첩for문 이용!)
int[][] ar1 = { {95, 86}, {83, 92, 96}, {78, 83 , 93, 87, 88} };
public class java0110 {
public static void main(String[] args) {
int[][] ar1 = {
{95, 86},
{83, 92, 96},
{78, 83 , 93, 87, 88}
};
int sum=0;
int count=0;
double avg = 0;
for(int i=0; i<ar1.length; i++) {
for(int j=0; j<ar1[i].length; j++) {
sum += ar1[i][j];
count++;
}
}
System.out.println("학생수 : " + count);
avg = (double) sum/count;
System.out.println("총 점 : " + sum);
System.out.println("평균점수 : " + avg);
}
}
결과 :
학생수 : 10
총 점 : 881
평균점수 : 88.1
학생들의 점수를 분석하는 프로그램을 만드세요.
while문과 scanner의 nextline() 메소드를 이용하세요.
최대값 평균값을 구하는 코드를 작성하세요.
import java.util.Scanner;
public class java0110 {
public static void main(String[] args) {
System.out.println();
Scanner sc = new Scanner(System.in);
int[] scores = null;
int choice;
int studNum = 0;
int max=0;
double avg;
int sum = 0;
do {
System.out.println("------------------------------------");
System.out.println("1.학생수|2.점수입력|3.점수리스트|4.분석|5.종료");
System.out.println("------------------------------------");
System.out.print("선택> ");
choice = sc.nextInt();
switch(choice) { // choice 가 뭘까용?
case 1: // choice 가 1일때
System.out.print("학생수> ");
studNum = sc.nextInt(); //학생수를 studNum 으로 입력받아요
scores = new int[studNum]; //배열 scores를 studNum 만큼의 크기로 지정하고 선언합니당
break; // case1을 나와줘요 (여기서 배열은 studNum만큼의 크기로 설정이 됬어요)
case 2:
for(int i=0; i< studNum; i++ ) {
//i가 studNum의 길이가 될때까지 i를 증가시켜줘요
System.out.print("scores[" + i +"] : ");
scores[i] = sc.nextInt();
//i가 studNum이 될때까지 반복되면서 해당 print문과 scanner가 실행되면서 i만큼 값을 저장해요.
}
break;
case 3:
for(int i=0; i< studNum; i++) {
//i가 studNum의 길이가 될때까지 i를 증가시켜줘요. 그냥 외웁시당.
System.out.println("scores[" + i +"] : " + scores[i]);
//i가 studNum이 될때까지 scores의 값을 print로 출력해줘요.
}
break;
case 4:
for(int i=0; i< studNum; i++) {
//i가 studnNum이 될때까지 i를 증가시켜줘요.
//여기서는 평균을 구하기위한 합계와 최대값을 구할거에요.
sum += scores[i];
// sum에 i가 반복되는만큼의 scores를 가산해줘요.
if(scores[i] >= max) {
// 최대값을 구해봅시다.
// i번 돌때까지 max(최대값)을 비교할거에요.
// max값이 scores[i]의 값보다 작을때
max = scores[i];
// max에 해당 scores[i]를 대입시켜줘요.
}
}
avg = sum / (double)(scores.length);
// avg 평균을 구할거에요. 앞서 구한 sum을 scores의 길이만큼 (배열의 개수)나눠줘요.
System.out.println("최고 점수 : " + max);
System.out.println("평균 점수 : " + avg);
}
}while(choice != 5);
System.out.println("프로그램 종료");
}
}
728x90
'JAVA > Java 기초' 카테고리의 다른 글
| 객체, 클래스 변수.java (0) | 2023.01.10 |
|---|---|
| 참조-배열 등등...java (0) | 2023.01.10 |
| 참조 타입.java (0) | 2023.01.09 |
| 반복문(for, while, do-while) (0) | 2023.01.06 |
| 연산자-2.java (0) | 2023.01.05 |