Java/Java 인강

Java 인강 필기 4

code2772 2022. 10. 2. 10:58
728x90
반응형
(22.7.28)

# 12강 ~ 16강 배열

​

배열 - 같은 자요형의 변수들의 나열된 묶음

​

사용 목적 - 같은 타입의 변수들의 관리를 편하게 하기 위해 사용

​

package day12;

​

public class Array01 {

​

public static void main(String[] args) {

​

int [] scores = new int[3];//3명의 학생점수 저장하고 싶다!

scores[0] = 100;

scores[1] = 80;

scores[2] = 70;

for(int i=0;i<3;i++) {

System.out.println(i+1+" 번째 점수 :"+scores[i]+"점");

}



}

​

}

​

package day12;

​

public class Araray02 {

​

public static void main(String[] args) {

int arInt[]=new int[3];

double arDouble[]=new double[3];

char arChar[]=new char[3];

String arString[] = new String[3];





for(int i=0;i<3;i++) {

System.out.print(arInt[i]);

}

System.out.println();

for(int i=0;i<3;i++) {

System.out.print(arDouble[i]);

}

System.out.println();

for(int i=0;i<3;i++) {

System.out.print(arChar[i]);

}

System.out.println();

for(int i=0;i<3;i++) {

System.out.print(arString[i]);

}

}

​

}

​

package day12;

​

import java.util.Arrays;

​

public class Array03 {

​

public static void main(String[] args) {

//a라는 정수형 배열을 생성할 때

int ar[] = new int[] {10,20,4,25,18};

//b라는 정수형 배열을 생성, 초기값으로 넣는 방법

int []b = {1,2,3,4,5,6};

//c라는 정수형 배열을 선언하고, 10개의 정수를 묶음

int c[] = new int[10];

// 배열.length - 배열의 길이, 배열의 요소갯수

for(int i=0;i<c.length;i++) {

c[i]=i;

}

for(int i=0;i<c.length;i++) {

System.out.print(c[i]);

}

System.out.println();

System.out.println(Arrays.toString(c));

// 문자열로 출력 : Arrays.toString(배열명)

​

}

}

​

package day12;

​

public class Array4 {

​

public static void main(String[] args) {

int []students = {100,95,91,88,93};

int sum=0;

for(int i =0;i<students.length;i++) {

sum+=students[i];

}

​

System.out.println("시험성적의 평균 :"+sum/(float)students.length);

//정수/정수 = 정수 , 실수값이 나오려면. 정수/(float) 강제 형변환

}

}

​

​

# 로또 프로그램

​

package day13.copy;

//import java.util.*; 모든 패키지에 있는 import

import java.util.Random;

import java.util.Scanner;

​

public class Day13_1 {

//로또 프로그램

public static void main(String[] args) {

​

Scanner sc= new Scanner(System.in);

Random random = new Random();

int lottoAr[] = new int[6];

int userAr[]= new int[6];

int bonus,bonusUser;

int count = 0;

//로또배열 6개의 랜덤값 지정

for(int i=0;i<6;i++) {

lottoAr[0]=random.nextInt(45)+1; //0~44 -> 0~45

}

//bonus = random

bonus=random.nextInt(45)+1;

​

//사용자에게 6개의 값을 입력

for(int i=0;i<6;i++) {

userAr[i]=sc.nextInt();

}

System.out.print("보너스 값 입력 : ");

bonusUser=sc.nextInt();

​

//로또 배열의 값과 사용자 배열의 값중 같은 값을 카운트하기

for(int j = 0;j<6;j++) {

for(int i=0;i<6;i++) {

if(lottoAr[0]==userAr[i]) {

count++;

}

}

if(count==6) {

System.out.println("축하 1등");

}

}

else if(count==5) {

if(bonus==bonusUser) {

System.out.println("축하 2등");

}

else {

System.out.println("축하 3등");

}

}

else if(count==5) {

System.out.println("축하 4등");

}

else if(count==5) {

System.out.println("축하 5등");

}

else if(count==5) {

System.out.println("꼴등");

}

​

}

​

​

배열의 선언과 생성

- 타입[]배열명; //선언

배열명 = new 타입[길이]; // -> 타입[]배열명 = new 타입[길이];int[ ]ar = new int[3]

(ar 묶음에서 첫 번째 요소에 접근할래!)

​

배열의 길이와 인덱스 - 배열의 값을 다루기 위해선 인덱스를 사용

EX) ar[0], ar[1], ar[2] - 배열의 시작번호는 0부터 시작한다

​

배열의 초기화와 출력 - 배열 생성 시 배열의 타입에 해당하는 기본값을 초기화!

​

2차원 배열의 선언과 생성

​

다차원 배열 - 배열의 각 요소가 배열

​

2차원 배열의 선언 - 자료형[][] 배열명;

​

2차원 배열의 생성 - 배열명 = new자료형[크기][크기];

//[묶음의개스크기][각요소의갯수]

​

목적 - 배열을 묶어서 관리하기 위해서 사용

배열은 변수를 관리하기 위한 목적으로 만든 구조!

2차원 배열도 각 변수에 접근하는 방법이 있어야 하며 그 방법이 바로 인덱스다!

​

package dya14;

​

public class Dat14_1 {

​

public static void main(String[] args) {

//5개의 변수를 묶는 1차원배열 *2 = 2차원 배열

int ar1[] = new int[5];

int ar2[] = new int[5];

​

int ar3[][]=new int[2][5];//[묶음의개스 크기][각요소의갯수]

​

​

}

​

}

​

​

package dya14;

​

import java.util.Scanner;

​

public class Day14_2 {

​

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int scores[][] = new int[4][3];

String subject[] = {"국어", "영아", "수학"};

​



for(int j=0;j<4;j++) {

System.out.println((j+1) + "번 학생의 점수");

for(int i =0; i<3;i++) {

System.out.print(subject[i]+":");

scores[j][i] = sc.nextInt();

}

}

//2차원 배얄에 지정된 값을 출력

for(int i =0; i<3;i++) {

System.out.print("\t"+subject[i]);

}

System.out.println();

for(int j=0;j<4;j++) {

System.out.print(j+1+"번:\t");

for(int i=0;i<3;i++) {

System.out.print(scores[j][i] + "\t" );

}

System.out.println();

}



}

}

​

​

​

​

​

배열의 System.array(src,srcPos, dest, destPos, length);

​

src : 복사할 배열

srcPos ;

​

복사를 하기 시작할 인덱스 위치

dest : 덮어쓸 배열

destPost : 덮어쓰기 시작할 인덱스 위치

length : 복사할 길이

​

package day15;

​

import java.util.Arrays;

​

public class Day15_1 {

​

public static void main(String[] args) {

​

int a [] = {1,2,3,4,5, 6};

int b [] = {0,0,0,0,0,0,0};

​

System.out.println(Arrays.toString(a));

System.out.println(Arrays.toString(b));

​

System.arraycopy(a, 2, b, 3, 4);

System.out.println(Arrays.toString(b));

​

}

​

}

​

package day15;

​

public class Dat15_2 {

​

public static void main(String[] args) {

​

String a[] = {"Java","Hello","Programming"};

for(String i:a) {// a만큼 i에 들어간다

System.out.println(i);

}

​

int b[] = {1,2,3,4,5,};

for(int i:b) {

System.out.println(i);

​

}

​

}

​

}

​

​

package day15;

​

import java.util.Scanner;

​

public class Day15_3 {

​

public static void main(String[] args) {

​

Scanner sc = new Scanner(System.in);

String fruits[] = new String[3];

for(int i=0; i<3;i++) {

System.out.print("주문할 과일 :");

fruits[i] =sc.next();

}

System.out.println("===주문받을 과일==");

for(String eachFruits:fruits) {

System.out.print(eachFruits+" ");

}

​

}

​

}

​

​

​

​

​

for each 문

- 배열을 순환 할 시, 반복문 보다 편리하게 순환할 수 있도록 새로운 문법 제공

​

구조

- for(자료형 변수명 : 배열명) { 변수가 배열을 순환하면서 반복할 명령; }

​

​

카페프로그램 알고리즘 짜기

​

알고리즘이란 - 문제를 해결하기 위한 절차적인 과정

​

​

# cafe 프로그램

​

package day16;

​

import java.util.Scanner;

​

public class Day16_1 {

​

public static void main(String[] args) {

// 카페주문 프로그램을 만들어보자!!

​

Scanner sc = new Scanner(System.in);

int count =0;

//주문은 총 5개까지 가능

String orderList[] = new String[5];

int total = 0;

while (true) {

System.out.println("*******cafe********");

System.out.println("1. 주문하기");

System.out.println("2. 취소하기");

System.out.println("3. 결제하기");

System.out.println("4. 끝내기");

System.out.println("입력 :");

int num=sc.nextInt();

if(num==1) {

String menuName = "";

int menuPrice=0;

System.out.println("1. 아메리카노\t3800");

System.out.println("2. 아메리카노1\t2800");

System.out.println("3. 아메리카노2\t4800");

System.out.println("4. 아메리카노3\t5800");

System.out.println("주문할 메뉴번호 :");

int menuNum=sc.nextInt();

if(menuNum==1) {

menuName="아메리카노";

menuPrice=3800;

}

else if(menuNum==2) {

menuName="아메리카노1";

menuPrice=2800;

}

else if(menuNum==3) {

menuName="아메리카노2";

menuPrice=4800;

}

else if(menuNum==4) {

menuName="아메리카노3";

menuPrice=5800;

}

else {

System.out.println("잘못입력");

continue;

}

total+=menuPrice;

orderList[count]=menuName;

count++;

System.out.println("주문한 메뉴는"+menuName);

System.out.println("가격은"+menuPrice);

​

}

else if(num==2) {

System.out.println("주문한 매뉴리스트");

for(int i=0;i<count;i++) {

System.out.println(orderList[i]);

}

System.out.println("취소할 매뉴는?");

int cancelNum=sc.nextInt();

if(1<=cancelNum && cancelNum<=count) {

String delMenu = orderList[cancelNum-1];

System.out.println("취소할 메뉴를 선택!");

for(int i=cancelNum-1;i<count;i++) {

orderList[i]=orderList[i+1];

}

if(delMenu.equals("아메리카노")) {

total-=3800;

}

else if (delMenu.equals("아메리카노1")) {

total-=2800;

}

else if (delMenu.equals("아메리카노2")) {

total-=4800;

}

else if (delMenu.equals("아메리카노1")) {

total-=2800;

}

else if (delMenu.equals("아메리카노1")) {

total-=2800;

}

count--;

}

​

}

else if(num==3) {

System.out.println("결제할 금액: "+total+"원");

System.out.println("지불할 금액:");

int money=sc.nextInt();

if(money<total) {

System.out.println("잔돈이 부족합니다.");

continue;

}

else {

System.out.println("잔돈은" +(money-total)+"원");

total = 0;

for(int i = 0; i<5 ;i++) {

orderList[i] = "";

}

count = 0;

}

}

else if(num==4) {

System.out.println("반복종료");

break;

}

else {

System.out.println("잘못입력했습니다.");

}

}

​

​

}

​

}
반응형

'Java > Java 인강' 카테고리의 다른 글

Java 인강 필기 6  (0) 2022.10.02
Java 인강 필기 5  (0) 2022.10.02
Java 인강 필기 3  (0) 2022.10.02
Java 인강 필기 2  (0) 2022.10.02
Java 인강 필기 1  (0) 2022.10.01