프로그래밍

자바 배열 예제

JAVA
반응형

a~z 알파벳 출력

package ch2;

public class study1 {
	public static void main(String[] args) 
	{
		int al = (int)'a';
		char codes []= new char[26];
		
		for (int i = 0 ; i<26; i ++)
			codes[i]= (char) (al+i);
		
		for (int i = 0 ; i<26; i ++)
			System.out.print(codes[i]+" ");
	}	
}
a b c d e f g h i j k l m n o p q r s t u v w x y z

주사위를 만번 던저 각 면이 나오는 횟수

package ch2;
import java.util.Random;

public class study1 {
	public static void main(String[] args) 
	{
		Random rand = new Random();
		final int size  = 6;
		int [] fre = new int [size];
		
		for (int i =0 ; i< 10000 ; i ++)
			fre [(int)(Math.random()*6)] ++;
		
		for (int i =0 ; i<size; i++)
			System.out.println(i+1+" "+fre[i]);
	}	
}
1 1686
2 1593
3 1691
4 1661
5 1684
6 1685

 

원하는 개수만큼 1~100 사이의 수 생성하고 그 합과 평균 구하기

package ch2;
import java.util.Scanner;
import java.util.Random;
public class study1 {
	public static void main(String[] args) 
	{
		Scanner a = new Scanner(System.in);
		Random b = new Random();
		System.out.print("임의의 수 개수: ");
		int c = a.nextInt(), sum =0;
		int arr[]= new int [c];
		for (int i =0; i<c ; i++)
			arr[i]=b.nextInt(100)+1;
		for (int i : arr)
		{
			System.out.print(i+" ");
			sum += i;
		}
		double av = sum/c;
		System.out.println("\n합: "+sum+" 평균: "+av);		
	}	
}
임의의 수 개수: 5
6 21 1 69 67 
합: 164 평균: 32.0

5개의 성적 입력하고 평균 구하기

package ch2;
import java.util.Scanner;
public class study1 {
	public static void main(String[] args) 
	{
		Scanner a = new Scanner(System.in);
		int score[]= new int [5];
		int sum=0;
		for (int i=0; i<5; i++)
		{
			System.out.print("성적을 입혁하세요: ");
			score[i]=a.nextInt();
			sum+=score[i];
		}
		System.out.print("평균 성적은: "+sum/5);	
	}	
}
성적을 입혁하세요: 10
성적을 입혁하세요: 20
성적을 입혁하세요: 30
성적을 입혁하세요: 40
성적을 입혁하세요: 50
평균 성적은: 30

무명 배열

10개의 정수를 생성 후 오름차순 정렬하기 (버블정렬 사용)

package ch2;
import java.util.Scanner;
import java.util.Random;

public class study1 {
	public static void main(String[] args) 
	{
		Scanner a = new Scanner(System.in);
		Random b = new Random();
		
		int rand[]= new int [10];
		for (int i=0; i<rand.length ; i++)
			rand[i]=b.nextInt(100)+1;
		System.out.print("최초의 리스트: ");
		for (int i =0; i<rand.length ; i++)
			System.out.print(rand[i]+" ");

		for (int i =0; i<rand.length; i++)
		{
			for (int j=0;j<rand.length-1;j++)
			{
				if (rand[j]>rand[j+1])
				{
					int temp = rand[j];
					rand[j] = rand [j+1];
					rand[j+1] = temp;
					
				}
			}
		}
		System.out.print("\n정렬된 리스트: ");
		for (int i =0; i<rand.length ; i++)
			System.out.print(rand[i]+" ");
	}	
}
최초의 리스트: 26 57 66 71 55 23 93 27 95 9 
정렬된 리스트: 9 23 26 27 55 57 66 71 93 95 

내림차순까지

package ch2;
import java.util.Scanner;
import java.util.Random;

public class study1 {
	public static void main(String[] args) 
	{
		Scanner a = new Scanner(System.in);
		Random b = new Random();
		
		int rand[]= new int [10];
		for (int i=0; i<rand.length ; i++)
			rand[i]=b.nextInt(100)+1;
		System.out.print("최초의 리스트: ");
		for (int i =0; i<rand.length ; i++)
			System.out.print(rand[i]+" ");
		
		for (int i =0; i<rand.length; i++)
		{
			for (int j=0;j<rand.length-1;j++)
			{
				if (rand[j]>rand[j+1])
				{
					int temp = rand[j];
					rand[j] = rand [j+1];
					rand[j+1] = temp;					
				}
			}
		}
		
		System.out.print("\n정렬된 리스트: ");
		for (int i =0; i<rand.length ; i++)
			System.out.print(rand[i]+" ");
		
		System.out.print("\n내림차순 정렬: ");
		for (int i = rand.length-1; i>=0;i--)
			System.out.print(rand[i]+" ");
	}	
}
최초의 리스트: 35 95 88 30 4 63 64 37 29 13 
정렬된 리스트: 4 13 29 30 35 37 63 64 88 95 
내림차순 정렬: 95 88 64 63 37 35 30 29 13 4 

배열 정렬 활용하기

package ch2;
import java.util.Arrays;

public class study1 {
	public static void main(String[] args) 
	{
		int[] arr1 = { 1789, 2035, 1899, 1456, 2013,
				1458, 2458, 1254, 1472, 2365, 1456,
				2165, 1457, 2456 };
		String[] arr2 = {"Java", "Python", "PHP", "C#",
				"C Programming", "C++" };
		System.out.println("숫자배열 원본: "+Arrays.toString(arr1));
		Arrays.sort(arr1);
		System.out.println("숫자배열 정렬: "+Arrays.toString(arr1));
		System.out.println("문자배열 원본: "+Arrays.toString(arr2));
		Arrays.sort(arr2);
		System.out.println("문자배열 정렬: "+Arrays.toString(arr2));
	}	
}
숫자배열 원본: [1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456]
숫자배열 정렬: [1254, 1456, 1456, 1457, 1458, 1472, 1789, 1899, 2013, 2035, 2165, 2365, 2456, 2458]
문자배열 원본: [Java, Python, PHP, C#, C Programming, C++]
문자배열 정렬: [C Programming, C#, C++, Java, PHP, Python]
반응형

'JAVA' 카테고리의 다른 글

자바 클래스 (set 메소드 get 메소드)  (0) 2019.12.22
자바로 369 박수 유무 판별  (0) 2019.12.15
자바 버블정렬  (0) 2019.12.15
자바 배열  (0) 2019.12.15
자바를 이용한 가위바위보 게임  (0) 2019.12.10