import java.util.*;

public class Sorting {
	public static void main(String[] args) {

		Scanner scn = new Scanner(System.in);
		System.out.print("정렬 갯수 > ");
		int line = scn.nextInt();
		int [] arr = new int [line];

		for (int i=0; i<arr.length; i++) {
			System.out.print((i+1)+"번째 숫자를 입력 > ");
			int num = scn.nextInt();
			arr[i] = num;
		}
		System.out.println(Arrays.toString(arr));

		int temp;
		while(true) {
			System.out.println("정렬 순서를 정하세요 : 1.오름 2.내림");
			int order = scn.nextInt();
			if (order == 1) { // 오름차순
				for (int i = 0; i < arr.length - 1; i++) {
					for (int j = i + 1; j < arr.length; j++) {
						if (arr[i] > arr[j]) {
							temp = arr[i];
							arr[i] = arr[j];
							arr[j] = temp;
						}
					}
				}
				break;
			} else if (order == 2) { // 내림차순
				for (int i = 0; i < arr.length - 1; i++) {
					for (int j = i + 1; j < arr.length; j++) {
						if (arr[i] < arr[j]) {
							temp = arr[i];
							arr[i] = arr[j];
							arr[j] = temp;
						}
					}
				}
				break;
			} else {
				System.out.println("1 또는 2로 선택해주세요.");
				continue;
			}
		}
		System.out.println(Arrays.toString(arr));
	}
}

배열 순차 정렬 프로그램

 

 

 

'문제풀이 > Java' 카테고리의 다른 글

[자바] 계산기  (0) 2021.05.12
[자바] 가위바위보 게임  (0) 2021.05.11
[자바] 로또 번호 생성  (0) 2021.05.10
[자바] Baseball Game  (0) 2021.05.10
[자바] 숫자 맞추기 UP&DOWN  (0) 2021.05.10
public class Lotto {
	public static void main(String[] args) {
		boolean swit[] = new boolean[45];
		int lotto[] = new int [6];
		int w, r;

		w = 0;
		while(w < 6) {
			r = (int)(Math.random()*45); // 0 ~ 44
			if(swit[r] == false) {
				swit[r] = true;
				lotto[w] = r+1;	     // 1 ~ 45
				w++; 
			}
		}
		for(int i =0; i < lotto.length; i++) {
			System.out.print(lotto[i]+ " ");
		}
	}
}

로또 번호 생성기

 

 

'문제풀이 > Java' 카테고리의 다른 글

[자바] 가위바위보 게임  (0) 2021.05.11
[자바] 배열 순차 정렬  (0) 2021.05.11
[자바] Baseball Game  (0) 2021.05.10
[자바] 숫자 맞추기 UP&DOWN  (0) 2021.05.10
[자바] 문자열이 숫자인지 판별하기  (0) 2021.05.10
import java.util.Scanner;

public class Baseball {
	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		
		// 선언부
		int r_num[] = new int[3];
		int u_num[] = new int[3];
		boolean clear;
		
		boolean swit[] = new boolean[10]; // 0 ~ length-1
		//				 allocation(동적할당)
		
		// 초기화
		clear = false;
		for(int i=0; i<swit.length; i++) {
			swit[i] = false;
		}
		int w, r;
		
		w = 0;
		while(w < 3) {
			r = (int)(Math.random()*9);// 0~8
			if(swit[r] == false) {
				swit[r] = true;
				r_num[w] = r + 1;  // 1~9
				w++;
			}
		}
		for(int i=0; i<r_num.length; i++) {
			System.out.print(r_num[i]+ " ");
		}
		System.out.println();
		
		////////////////////////////////  loop
		w = 0;
		
		while(w<10) {
		// user 입력
		while(true) {
			for(int i=0; i<u_num.length; i++) {
				System.out.print((i + 1)+"번째 수 >> ");
				u_num[i] = scn.nextInt();
				}
				if(u_num[0] != u_num[1]
						&& u_num[1] != u_num[2]
								&& u_num[0] != u_num[2]) {
					break;
				}
				System.out.println("같은 숫자를 입력하셨습니다. 다시 입력해 주세요.");
			}
			
			// 비교			-> 탈출
			// 스트라이크, 볼
			int strike, ball;
			strike = 0;
			ball = 0;
			
			// ball
			for(int i=0; i< r_num.length; i++) {	// 0 1 2
				for (int j=0; j<u_num.length; j++) {// 0 1 2
					if(u_num[i] == r_num[j] && i != j) {
						ball++;
					}
				}
			}
			
			// strike
			for(int i=0; i< r_num.length; i++) {
				if(u_num[i] == r_num[i]) {
					strike++;
				}
			}
		// 맞췄을 경우
			if(strike > 2) {
				clear = true;
				break;
			}
		// 메세지 출력
			System.out.println(strike + "스트라이크"+ ball +"볼입니다.");
			w++;
		}
		////////////////////////////////
		// 결과출력
		if(clear) {
			System.out.println("Game Clear!!!");
		}else {
			System.out.println("Game Over~");
		}
	}
}

Baseball Game

 

조건 

> 기회는 10번
> 3개의 숫자는 같은 수이면 안 된다.
> 자리수와 숫자가 같은 경우 : strike
> 숫자만 같은 경우 : ball

 

 

 

 

'문제풀이 > Java' 카테고리의 다른 글

[자바] 가위바위보 게임  (0) 2021.05.11
[자바] 배열 순차 정렬  (0) 2021.05.11
[자바] 로또 번호 생성  (0) 2021.05.10
[자바] 숫자 맞추기 UP&DOWN  (0) 2021.05.10
[자바] 문자열이 숫자인지 판별하기  (0) 2021.05.10
import java.util.Scanner;

public class UpDown {
	public static void main(String[] args) {
		// 1 ~100까지 숫자 찾기 게임! 기회는 단 10회!
		Scanner scn = new Scanner(System.in);
		int temp = 0;
		int count = 0;
		boolean clear = false;
		
		int comNum = (int)(Math.random()*100)+1;	// comNum 1~100까지 랜덤 수 뽑기
		
		while(count<10) {
			System.out.print("user : ");
			int userNum = scn.nextInt();		// userNum 입력

			if(comNum > userNum ) {			// comNum이 userNum보다 클 경우
				temp = 1;			// temp에 1 저장
			}else if (comNum < userNum) {		// comNum이 userNum보다 작을 경우
				temp = 2;			// temp에 2 저장
			} else {				// comNum이 userNum보다 같을 경우
            			clear = true;			// clear에 true를 저장
				break;				// if블럭 빠져 나가기
			}
			
			switch(temp) {							
			case 1 :
				System.out.println("UP!");
				break;
			case 2 :
				System.out.println("DOWN!");
				break;
			}
			count++;
		}
		if(clear == true) {
			System.out.println("Congratulations! Game Clear!!!");
		} else {
			System.out.println("GameOver~~~");
		}
	}
}

Scanner로 숫자를 입력받아서 UP & DOWN 게임

'문제풀이 > Java' 카테고리의 다른 글

[자바] 가위바위보 게임  (0) 2021.05.11
[자바] 배열 순차 정렬  (0) 2021.05.11
[자바] 로또 번호 생성  (0) 2021.05.10
[자바] Baseball Game  (0) 2021.05.10
[자바] 문자열이 숫자인지 판별하기  (0) 2021.05.10
import java.util.Scanner;

public class NumStr {
	public static void main(String[] args) {
		// 입력된 문자열이 숫자인지 문자가 섞여있는지 판단하는 코드를 작성
		char temp = 0;
		boolean flag = true;
		// 문자열 입력
		Scanner scn = new Scanner(System.in);
		System.out.print("입력 > ");
		String str = scn.next();
		
		for(int i=0; i<str.length(); i++) {
			temp = str.charAt(i);			// 입력한 str을 문자로 쪼개서 temp로 받기
				if (temp < '0' || temp > '9' ) {// temp의 값이 '0'작거나 '9'보다 클 경우
					flag = false;
					break;
				}	
		}
		if(flag == true) {	
			System.out.println("숫자로 되어있습니다.");
		}else {
			System.out.println("문자열입니다.");
		}
	}
}

    

위 예제는 입력한 문자열이 숫자로만 되어 있는지 문자열인지 판별해 주는 코드 입니다.

 

 

 

 

'문제풀이 > Java' 카테고리의 다른 글

[자바] 가위바위보 게임  (0) 2021.05.11
[자바] 배열 순차 정렬  (0) 2021.05.11
[자바] 로또 번호 생성  (0) 2021.05.10
[자바] Baseball Game  (0) 2021.05.10
[자바] 숫자 맞추기 UP&DOWN  (0) 2021.05.10

+ Recent posts