1. 세개의 수를 입력받아서 작은수에서 큰수로 출력
package com.day05;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, temp;
System.out.print("세개의 수를 입력해주세요:");
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
if(num1>num2){
temp = num1;
num1 = num2;
num2 = temp;
}
if(num1>num3){
temp = num1;
num1 = num3;
num3 = temp;
}
if(num2>num3){
temp = num2;
num2 = num3;
num3 = temp;
}
System.out.printf("결과 : %d %d %d\n", num1, num2, num3);
sc.close();
}
}
결과
세개의 수를 입력해주세요:5 7 1
결과 : 1 5 7
2. 선택정렬
package com.day05;
import java.util.Scanner;
class Sort{
int num[] = new int[5];
int temp;
public void input(){
Scanner sc = new Scanner(System.in);
System.out.printf("숫자 5개를 입력해주세요:");
for(int i=0;i<num.length;i++)
num[i] = sc.nextInt();
sc.close();
}
public int[] sortData(){
for(int i=0;i<num.length-1;i++){
for(int j=i+1;j<num.length;j++){
if(num[i]>num[j]){
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
return num;
}
public void print(){
System.out.print("Sorted Data:");
for(int su:num)
System.out.printf("%4d", su);
}
}
public class Test2 {
//Selection Sort
public static void main(String[] args) {
Sort s = new Sort();
s.input();
s.sortData();
s.print();
}
}
결과
숫자 5개를 입력해주세요:24 51 16 95 45
Sorted Data: 16 24 45 51 95
3. 10명 이내의 이름과 점수를 입력받아 점수가 높은 사람에서 낮은 사람 순으로 출력
package com.day05;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j, inwon, temp1;
String temp2;
int score[];
String name[];
do{
System.out.print("학생수를 입력해주세요[1~10]:");
inwon = sc.nextInt();
}while(inwon<1 || inwon>10);
score = new int[inwon];
name = new String[inwon];
System.out.println();
for(i=0;i<inwon;i++){
System.out.print((i+1)+ "번째 이름:");
name[i] = sc.next();
System.out.print("점수:");
score[i] = sc.nextInt();
System.out.println();
}
for(i=0;i<inwon-1;i++){
for(j=i+1;j<inwon;j++){
if(score[i]<score[j]){
temp1 = score[i];
score[i] = score[j];
score[j] = temp1;
temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
System.out.println("등수");
for(i=0;i<inwon;i++)
System.out.printf("%d등 %3s %4d점 \n", i+1, name[i],score[i]);
sc.close();
}
}
결과
학생수를 입력해주세요[1~10]:4
1번째 이름:길동
점수:75
2번째 이름:영희
점수:74
3번째 이름:철수
점수:98
4번째 이름:자바
점수:84
등수
1등 철수 98점
2등 자바 84점
3등 길동 75점
4등 영희 74점
4. 1~45까지의 수 중 6개의 난수를 발생시켜 크기순으로 출력
package com.day05;
import java.util.Random;
public class Test4 {
public static void main(String[] args) {
Random rd = new Random();
int num[] = new int[6];
int i, j, su,temp;
su = 0;
while(su<6){
num[su] = rd.nextInt(45)+1;
for(i=0;i<su;i++){
if(num[su]==num[i]){
su--;
break;
}
}
su++;
}
for(i=0;i<num.length-1;i++){
for(j=i+1;j<num.length;j++){
if(num[i]>num[j]){
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int n:num)
System.out.printf("%2d ", n);
System.out.println();
}
}
결과
12 14 32 33 34 39
5. 10명 이내의 이름과 점수를 입력받아 석차를 구하시오
석차가 높은사람에서 낮은사람 순으로 출력
package com.day05;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j, inwon;
int score[];
int rank[];
String name[];
do{
System.out.print("학생수를 입력해주세요[1~10]:");
inwon = sc.nextInt();
}while(inwon<1 || inwon>10);
score = new int[inwon];
name = new String[inwon];
rank = new int[inwon];
System.out.println();
for(i=0;i<inwon;i++)
rank[i] = 1;
for(i=0;i<inwon;i++){
System.out.print((i+1)+ "번째 이름:");
name[i] = sc.next();
System.out.print("점수:");
score[i] = sc.nextInt();
System.out.println();
}
for(i=0;i<inwon-1;i++){
for(j=i+1;j<inwon;j++){
if(score[i]<score[j])
rank[i] = rank[i] + 1;
else if(score[i]>score[j])
rank[j] = rank[j] + 1;
}
System.out.println("");
}
System.out.println("등수");
for(i=0;i<inwon;i++)
System.out.printf("%d등 %4s %4d점 \n", rank[i], name[i],score[i]);
sc.close();
}
}
결과
학생수를 입력해주세요[1~10]:4
1번째 이름:길동
점수:87
2번째 이름:영희
점수:74
3번째 이름:철수
점수:97
4번째 이름:자바
점수:59
등수
2등 길동 87점
3등 영희 74점
1등 철수 97점
4등 자바 59점
6. 숫자 5개를 입력받아 가장 큰 수와 가장 작은 수를 출력
package com.day05;
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num[] = new int[5];
int i, max, min;
System.out.print("정수 5개를 입력해주세요:");
for(i=0;i<num.length;i++)
num[i]= sc.nextInt();
max = num[0];
min = num[0];
for(i=0;i<num.length;i++){
if(max<num[i])
max = num[i];
if(min>num[i])
min = num[i];
}
System.out.println("가장 큰수:" +max+ "\n가장 작은수:" +min);
sc.close();
}
}
결과
정수 5개를 입력해주세요:14 24 84 65 74
가장 큰수:84
가장 작은수:14
7. 1~18사이의 숫자중 컴퓨터가 고른 숫자 맞추기 [기회는 3번]
package com.day05;
import java.util.Random;
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) throws Exception {
Random rd = new Random();
Scanner sc = new Scanner(System.in);
int su, num, i;
char ch;
while(true){
su = rd.nextInt(18)+1;
System.out.println("1~18사이의 정수를 입력해주세요");
i=0;
while(i<3){
do{
System.out.print("정수입력[" +(i+1)+ "번째 기회]:");
num = sc.nextInt();
}while(num<0 || num>18);
if(su == num){
System.out.println("맞았어요!");
break;
}
else{
System.out.println("틀렸어요!");
i++;
}
}
System.out.println();
System.out.println("정답은 " +su+ "입니다.");
System.out.println();
System.out.print("계속 하시겠습니까? [Y/N]");
ch = (char)System.in.read();
if(ch!='Y' && ch!='y')
break;
System.out.println();
System.in.skip(2);
}
sc.close();
}
}
결과
1~18사이의 정수를 입력해주세요
정수입력[1번째 기회]:11
틀렸어요!
정수입력[2번째 기회]:5
틀렸어요!
정수입력[3번째 기회]:9
틀렸어요!
정답은 17입니다.
계속 하시겠습니까? [Y/N]y
1~18사이의 정수를 입력해주세요
정수입력[1번째 기회]:15
틀렸어요!
정수입력[2번째 기회]:7
틀렸어요!
정수입력[3번째 기회]:5
틀렸어요!
정답은 16입니다.
계속 하시겠습니까? [Y/N]n
8. 컴퓨터와 가위바위보
package com.day05;
import java.util.Random;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) throws Exception {
Random rd = new Random();
Scanner sc = new Scanner(System.in);
int com, user;
String rock[] = {"가위", "바위", "보"};
String str;
char ch;
System.out.println("컴퓨터와 가위바위보를 합니다!");
System.out.println("1~3사이의 정수를 입력해주세요");
System.out.println();
while(true){
com = rd.nextInt(3);
System.out.println("1:가위, 2:바위, 3:보");
do{
System.out.print("정수입력:");
user = sc.nextInt();
}while(user<0 || user>3);
System.out.println("컴퓨터:" +rock[com]+ " 사용자:" +rock[user]);
//공식사용
if(com==user)
str = "비겼습니다!";
else if((user+2)%3==com)
str = "당신이 이겼습니다.";
else
str = "컴퓨터가 이겼습니다,";
System.out.println(str);
/*
if(com == user)
System.out.println("비겼습니다.");
else if((com==0 && user==1) || (com==1 && user==2) || (com==2 && user==0))
System.out.println("당신이 이겼습니다.");
else
System.out.println("컴퓨터가 이겼습니다.");
*/
System.out.println();
System.out.print("계속 하시겠습니까? [Y/N]");
ch = (char)System.in.read();
if(ch!='Y' && ch!='y')
break;
System.out.println();
System.in.skip(2);
}
sc.close();
}
}
결과
컴퓨터와 가위바위보를 합니다!
1~3사이의 정수를 입력해주세요
1:가위, 2:바위, 3:보
정수입력:2
컴퓨터:가위 사용자:보
컴퓨터가 이겼습니다,
계속 하시겠습니까? [Y/N]y
1:가위, 2:바위, 3:보
정수입력:1
컴퓨터:보 사용자:바위
컴퓨터가 이겼습니다,
계속 하시겠습니까? [Y/N]n
9. 10000이하의 수 중 제일 큰 153의 배수
package com.day05;
public class Q4 {
public static void main(String[] args) {
int num = 10000;
int key = 153;
int max=0, max_1;
int i=0;
while(true){
max_1 = max;
max = i * key;
if(max>num){
break;
}
i++;
}
System.out.println(max_1);
}
}
결과
9945
댓글