1. NestedFor(이중포문)
2. 오늘 실습코드
1) NestedFor
package day14;
public class NestedFor {
public static void main(String[] args) {
// 이중 for문 이용하여 구구단 만들기
// 이중 for문을 이용하여 간단하게 프로그램을 만듦
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
System.out.printf("%d*%d=%d\n", i, j, i*j);
}
System.out.println();
}
// for문을 한 번만 사용해서 구구단 만들기
// 1단 ~ 9단
// 이중 for문을 이용하면 간단하게 만들 수 있는데
// for문을 한 번만 사용함으로써 코드가 복잡해짐
// for (int i = 1; i < 90; i++) {
// if(i % 10 == 0) {
// continue;
// }
// System.out.printf("%d*%d=%d\n",i / 10 + 1, i % 10, (i / 10 + 1) * (i % 10));
// }
}
}
2) ArrText
package day14;
public class ArrTest {
public static void main(String[] args) {
int[][] arrData = new int[3][4];
int[] arData = {4, 3, 2, 1};
//1~12
int cnt = 0;
//정방배열 길이 구하기
int length = arrData.length * arrData[0].length;
for (int i = 0; i < arrData.length; i++) {
//비정방배열 길이 구하기
length += arrData[i].length;
for (int j = 0; j < arrData[i].length; j++) {
arrData[i][j] = ++cnt;
}
}
System.out.println(length);
arrData[0] = arData;
for (int i = 0; i < arrData.length; i++) {
for (int j = 0; j < arrData[i].length; j++) {
System.out.printf("%02d ", arrData[i][j]);
}
System.out.println();
}
}
}
3) Nike
** 아직 완성된 코드가 아님, 다음 수업 시간에 완성시키기로 함**
** String.format 지정된 형식에 따라 값을 문자열로 변경 **
ex) avg = Double.parseDouble(String.format("%.2f", (double)total / length));
String.format("%.2f", (double)total / length)
>> (double)total / length의 값을 "%.2f" 형식의 문자열로 바꿈
avg는 double 타입,
현재 String.format으로 String 타입으로 변경된 값을 다시 double 타입으로 변경 필요
>> Double.parseDouble
package day14;
import javax.swing.JOptionPane;
public class Nike {
public static void main(String[] args) {
//강남점, 홍대점, 잠실점
//일반점, 키즈점
//강남점 일반점 매출액을 입력하세요.
//지점별 총, 평균 매출액(강남, 홍대, 잠실)
//연령병 총, 평균 매출액(일반, 키즈)
//나이키 총, 평균 매출액
//평균 매출액보다 높은 매장은 인센티브 매장으로 출력
//대화상자 (JOptionPane) 사용
//매출액은 만원 단위, 평균은 백원 단위
String[] branchName = {"강남점", "홍대점", "잠실점"};
String[] ageName = {"일반점", "키즈점"};
int[][] arrIncome = new int[2][3];
int[] arBranchTotal = new int[3];
double[] arBranchAvg = new double[3];
int[] arAgeTotal = new int[2];
double[] arAgeAvg = new double[2];
int total = 0;
double avg = 0.0;
int length = 0;
for (int i = 0; i < arrIncome.length; i++) {
//나이키 평균 매출액 계산을 위한 총 배열의 길이 계산 필요
length += arrIncome[i].length;
//사용자에게 지점별, 연령별 매출액 입력받기
for (int j = 0; j < arrIncome[i].length; j++) {
arrIncome[i][j] = Integer.parseInt(JOptionPane.showInputDialog(branchName[j] + " " +
ageName[i] + " 매출액을 입력하세요[단위 : 만원]"));
//나이키 총 매출액
total += arrIncome[i][j];
//지점별 총 매출액
arBranchTotal[j] += arrIncome[i][j];
//연령별 총 매출액
arAgeTotal[i] += arrIncome[i][j];
}
//연령별 평균 매출액(2번 반복)
arAgeAvg[i] = Double.parseDouble(
String.format("%.2f", (double)arAgeTotal[i] / arrIncome[i].length));
}
//지점별 평균 매출액(3번 반복)
for (int i = 0; i < arBranchAvg.length; i++) {
arBranchAvg[i] = Double.parseDouble(
String.format("%.2f", (double)arBranchTotal[i] / arrIncome.length));
}
//나이키 평균 매출액
avg = Double.parseDouble(String.format("%.2f", (double)total / length));
}
}
'웹_프론트_백엔드 > 단과' 카테고리의 다른 글
[단과_JAVA] 2020.02.04 (0) | 2020.02.05 |
---|---|
[단과_Python] 2020.02.03 (0) | 2020.02.04 |
[단과_Python] 2020.01.31 (0) | 2020.02.03 |
[단과_JAVA] 2020.01.31 (0) | 2020.02.03 |
[단과_Python] 2020.01.30 (0) | 2020.01.30 |