1. 영문 소문자를 하나 입력받고 그 문자보다 알파벳 순위가 낮은 모든 문자를 출력하는 프로그램을 작성하라.
import java.util.Scanner;
public class pro1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("알파벳 문자 하나를 입력하시오 : ");
String s = sc.next();
char c = s.charAt(0);
// Returns the char value at the specified index.
// An index ranges from 0 to length() - 1.
// The first char value of the sequenceis at index 0, the next at index 1,and so on,
// as for array indexing.
for (int i=0; i<c-'a'; i++) {
for (int j = i; j <= c-'a'; j++) {
System.out.print((char)('a'+j));
}
System.out.println();
}
}
}
2. 정수를 10개 입력받아 배열에 저장한 후, 배열을 검색하여 3의 배수만 출력하는 프로그램을 작성하라.
import java.util.Scanner;
public class pro2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("정수 10개 입력 >>");
int arr[] = new int[10];
for(int i=0; i< arr.length ; i++) {
arr[i] = sc.nextInt();
if(arr[i]%3 == 0)
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
3. 정수를 입력받아 짝수이면 “짝”, 홀수이면 “홀”을 출력하는 프로그램을 작성하라. 사용자가 정수를 입력하지 않는 경우에는 프로그램을 종료하라. 정답을 통해 try-catch-finally를 사용하는 정통적인 코드를 볼 수 있다.
package chap3;
import java.util.InputMismatchException;
import java.util.Scanner;
public class pro3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("정수를 입력하세요>> ");
try {
int n = sc.nextInt();
if(n%2 == 0) System.out.println("짝수");
else System.out.println("홀수");
}
catch (InputMismatchException e) {
System.out.println("수를 입력하지 않아 프로그램을 종료합니다.");
}
}
}
5. 정수를 10개 입력받아 배열에 저장하고 증가 순으로 정렬하여 출력하라.
public class pro4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("정수 10개 입력 : ");
int tmp;
int arr[] = new int[10];
for (int i=0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
// 버블 정렬
for (int i=0; i<arr.length ; i++) {
for(int j = i; j <arr.length; j++) {
if(arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
for(int i : arr) {
System.out.print(i+" ");
}
}
}
8. 컴퓨터와 사용자의 가위바위보 게임 프로그램을 작성하라. 사용자가 입력하고 <Enter> 키를 치면, 컴퓨터는 랜덤 하게 가위, 바위, 보 중 하나를 선택한다. 그리고 누가 이겼는지 출력한다. “그만”을 입력하면 게임을 종료한다.
package blog;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str[] = {"가위", "바위", "보"};
int i;
while(true)
{
int n = (int)(Math.random()*3); //0~2 중 랜덤 정수 리턴
System.out.println("컴퓨터와 가위 바위 보 게임을 합니다.");
System.out.print("가위 바위 보!>>");
String s = sc.next();
if(s.equals("그만")) break;
for (i = 0; i < str.length; i++) {
if(s.equals(str[i])) break;
}
if(i == n) // 비긴 경우
{
System.out.println("사용자는 = "+str[i]+", 컴퓨터 = "+str[n]+", 비겼습니다.");
}
else if(i == 0 && n == 2 || i == 1 && n == 0 || i == 2 && n == 1) // 사용자가 이긴경우
{
System.out.println("사용자는 = "+str[i]+", 컴퓨터 = "+str[n]+", 사용자가 이겼습니다.");
}
else// 컴퓨터가 이긴 경우
{
System.out.println("사용자는 = "+str[i]+", 컴퓨터 = "+str[n]+", 컴퓨터가 이겼습니다.");
}
}
System.out.println("게임을 종료합니다...");
}
}
'학교 수업 > 명품 JAVA ESSENTIAL' 카테고리의 다른 글
4장 클래스와 객체_연습문제 (0) | 2024.04.14 |
---|---|
4장 클래스와 객체 (0) | 2024.04.10 |
3장 반복문 / 배열 / 예외 처리 (1) | 2024.04.10 |
2장 자바 기본 프로그래밍_연습문제 (1) | 2024.04.10 |
2장 자바 기본 프로그래밍 (0) | 2024.04.10 |