package array;
public class Array1 {
public static void main(String[] args) {
int student1 = 90;
int student2 = 80;
int student3 = 70;
int student4 = 80;
int student5 = 60;
System.out.println("학생1 점수: " + student1);
System.out.println("학생2 점수: " + student2);
System.out.println("학생3 점수: " + student3);
System.out.println("학생4 점수: " + student4);
System.out.println("학생5 점수: " + student5);
}
}
package array;
public class ArrayRef1 {
public static void main(String[] args) {
int [] students; // 배열 선언
students = new int[5]; // 배열의 크기 설정
students[0] = 90; // 배열에 위치에 맞는 값을 넣을 수 있다.
students[1] = 80;
students[2] = 70;
students[3] = 60;
students[4] = 50;
System.out.println("학생1 점수: " + students[0]);
System.out.println("학생2 점수: " + students[1]);
System.out.println("학생3 점수: " + students[2]);
System.out.println("학생4 점수: " + students[3]);
System.out.println("학생5 점수: " + students[4]);
}
}
int size = 1000;
new int[size];
리펙토링
- 기존의 코드를 유지하면서 내부 구조를 개선하여 가독성을 높이고, 유지보수를 용이하게 하는 과정을 말한다.
- 리펙토링은 버그를 줄이고, 코드의 설계를 개선하는 데에도 도움이된다.
package array;
public class ArrayRef2 {
public static void main(String[] args) {
int [] students; // 배열 선언
students = new int[5]; // 배열의 크기 설정
students[0] = 90;
students[1] = 80;
students[2] = 70;
students[3] = 60;
students[4] = 50;
for (int i=0; i<students.length; i++){ // students.length -> length는 배열의 크기를 구할수 있다.
System.out.println("학생"+(i+1)+" 점수: " + students[i]);
}
}
}
package array;
public class ArrayRef3 {
public static void main(String[] args) {
int [] students; // 배열 선언
students = new int[]{90,80,70,60,50}; // 배열의 들어가는 값을 초기화 할 수 있다.
for (int i=0; i<students.length; i++){ // students.length -> length는 배열의 크기를 구할수 있다.
System.out.println("학생"+(i+1)+" 점수: " + students[i]);
}
}
}
package array;
public class ArrayRef4 {
public static void main(String[] args) {
int [] students = {90,80,70,60,50}; // 배열 선언 + 초기화
for (int i=0; i<students.length; i++){ // students.length -> length는 배열의 크기를 구할수 있다.
System.out.println("학생"+(i+1)+" 점수: " + students[i]);
}
}
}
package array;
public class ArrayDi0 {
public static void main(String[] args) {
int [][] arr = new int [2][3]; // 행,열
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
// 0번째 행 출력
System.out.print(arr[0][0] + " ");
System.out.print(arr[0][1] + " ");
System.out.print(arr[0][2] + " ");
System.out.println();
// 1번쨰 행 출력
System.out.print(arr[1][0] + " ");
System.out.print(arr[1][1] + " ");
System.out.print(arr[1][2] + " ");
}
}
package array;
public class ArrayDi2 {
public static void main(String[] args) {
int [][] arr = new int [2][3]; // 행,열
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
for(int row = 0; row < 2; row++){
for(int col =0; col<3;col++){
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
}
package array;
public class ArrayDi2 {
public static void main(String[] args) {
int [][] arr = new int [][]{ // 선언과 동시에 초기화가 가능하다.
{1,2,3},
{4,5,6}
}; // 행,열
for(int row = 0; row < 2; row++){
for(int col =0; col<3;col++){
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
}
package array;
public class ArrayDi2 {
public static void main(String[] args) {
int [][] arr = new int [][]{ // 선언과 동시에 초기화가 가능하다.
{1,2,3},
{4,5,6}
}; // 행,열
for(int row = 0; row < arr.length; row++){ // arr 자체는 2개의 데이터를 가지고 있다,
for(int col =0; col<arr[row].length;col++){ // arr 의 열의 길이는 arr[row] 의 길이를 측정하면 된다.
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
}
package array;
public class ArrayDi4 {
public static void main(String[] args) {
int [][] arr = new int [2][3];
int i = 0;
for (int row =0; row<arr.length; row++){
for (int col=0;col<arr[row].length; col++){
arr[row][col] = ++i;
}
}
for(int row = 0; row < arr.length; row++){ // arr 자체는 2개의 데이터를 가지고 있다,
for(int col =0; col<arr[row].length;col++){ // arr 의 열의 길이는 arr[row] 의 길이를 측정하면 된다.
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
}
package array;
public class EnhancedFor1 {
public static void main(String[] args) {
int [] numbers = {1,2,3,4,5};
for(int i=0;i<numbers.length;i++){
int number = numbers[i];
System.out.println(number);
}
// 향상된 for문
for (int number:numbers) { // numbers 배열에 있는 원소 하나하나르 가져와서 사용한다.
System.out.println(number);
}
}
}
package array.ex;
import java.util.Arrays;
public class ArrayEx1 {
public static void main(String[] args) {
int student1 = 90;
int student2 = 80;
int student3 = 70;
int student4 = 60;
int student5 = 50;
int total = student1 + student2 + student3 + student4 + student5;
double average = (double) total / 5;
System.out.println("total = " + total);
System.out.println("average = " + average);
}
}
package array.ex;
import java.util.Arrays;
public class ArrayEx1 {
public static void main(String[] args) {
int [] students = {90,80,70,60,50};
int total = 0;
for(int i = 0; i<students.length; i++){
total += students[i];
}
double average = (double) total / students.length;
System.out.println("total = " + total);
System.out.println("average = " + average);
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int [] nums = new int[5];
for(int i = 0; i < nums.length; i++){
nums[i] = scanner.nextInt();
}
System.out.println("출력");
for(int i = 0; i < nums.length; i++){
if (i != nums.length -1){
System.out.print(nums[i] + ", ");
}else {
System.out.print(nums[i]);
}
}
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arrays = new int[5];
System.out.println("5개의 정수를 입력하시오");
for(int i = 0; i<5;i++){
arrays[i] = scanner.nextInt();
}
for(int i = 0; i<5;i++){
if(i == 4){
System.out.print(arrays[4-i]);
}else{
System.out.print(arrays[4-i] + ", ");
}
}
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int sum = 0;
double average;
System.out.println("5개의 정수를 입력하시오");
for(int i = 0; i<5; i++){
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
average = sum / 5;
System.out.println("입력한 정수의 합계: " + sum);
System.out.println("입력한 정수의 평균: " + average );
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("입력받을 숫자를 입력하세요: ");
int count = scanner.nextInt();
int [] nums = new int[count];
int sum = 0;
double average;
System.out.println(count + "개의 정수를 입력하시오:");
for(int i=0; i<count;i++){
nums[i] = scanner.nextInt();
sum += nums[i];
}
average = sum / count;
System.out.println("입력된 정수의 합계: " + sum);
System.out.println("입력한 정수의 평균: " + average);
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("입력받을 숫자의 개수를 입력하세요: ");
int count = scanner.nextInt();
int []nums = new int[count];
System.out.println(count + "개의 정수를 입력하세요: ");
for(int i=0;i<count;i++){
nums[i] = scanner.nextInt();
}
int max = nums[0];
int min = nums[0];
for(int i = 1; i<nums.length;i++){
if (max < nums[i]) {
max = nums[i];
}
if (min > nums[i]) {
min = nums[i];
}
}
System.out.println("가장 큰 정수는: " + max);
System.out.println("가장 작은 수는: " + min);
}
}
package array.ex;
import java.util.Scanner;
public class ArrayEx7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int korean,math,english;
int [] scores = new int[4];
for(int i = 0;i<4;i++){
System.out.println((i+1) + "번 학생의 성적을 입력하세요:");
System.out.print("국어 점수: ");
korean = scanner.nextInt();
System.out.print("수학 점수: ");
math = scanner.nextInt();
System.out.print("영어 점수: ");
english = scanner.nextInt();
scores[i] = korean + math + english;
}
for(int i = 0; i<4; i++){
System.out.println((i+1) + "번 학생의 총점: " + scores[i]+", 평균: " + ((double)scores[i] / 3));
}
}
}
상품 등록 : 상품 이름과 가격을 입력받아 저장한다.
상품 목록 : 지금까지 등록한 모든 상품의 목록을 출력한다.
첫 화면에서 세 가지 선택지를 준다 : 1.상품 등록 , 2.상품 목록 , 3.종료
상품 등록시 사용자가 상품 이름과 가격을 입력하면 배열에 저장한다.
상품 목록을 출력하면 배열에 저장된 모든 상품을 출력한다.
종료를 누르면 프로그램을 종료한다.
상품은 최대 10개까지 등록 가능
조건
- String[] productNames : 상품 이름을 저장할 String 배열
- int[] productPrices : 상품 가격을 저장할 String 배열
- int productCount : 현재 등록된 상품의 개수를 저장할 int 변수
package array.ex;
import java.util.Scanner;
public class ProductAdminEx {
public static void main(String[] args) {
int maxProducts = 10;
String[] productNames = new String[maxProducts];
int[] productPrices = new int[maxProducts];
int productCount = 0;
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print("1.상품 등록 | 2.상품 목록 | 3.종료\n 메뉴를 선택하세요:");
int menu = scanner.nextInt();
if (menu == 1) {
if (productCount > maxProducts) {
System.out.println("더 이상 상품을 등록할 수 없습니다.");
continue;
}
System.out.print("상품 이름을 입력하세요: ");
productNames[productCount] = scanner.next();
System.out.print("상품 가격을 입력하세요: ");
productPrices[productCount] = scanner.nextInt();
productCount++;
}else if (menu == 2) {
if (productCount == 0) {
System.out.println("등록된 상품이 없습니다.");
continue;
}else{
for(int i=0; i<productCount; i++){
System.out.println("상품 이름: " + productNames[i] + " 상품 가격: " + productPrices[i]);
}
}
}else{
System.out.println("상품 등록 프로그램을 종료합니다.");
break;
}
}
}
}