문제 #1
Scanner를 이용하여 소문자 알파벳을 하나 입력받고 다음과 같이 출력하는 프로그램을 작성하라.
소문자 알파벳 하나를 입력하시오 >> e
abcde
abcd
abc
ab
a
[코드]
package test;
import java.util.Scanner;
public class ScannerAlpabetEx {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("소문자 알파벳 하나를 입력하시오 >> ");
String s = scan.next(); // 문자열 읽기
char c = s.charAt(0); // 문자열의 첫 번째 문자를 c에 저장
for(char i = 'a'; i <= c; c--) {
for(char j = 'a'; j <= c; j++)
System.out.print(j);
System.out.println();
}
scan.close();
}
}
문제 #2
정수를 몇 개 저장할지 키보드로부터 개수를 입력받아 (100보다 작은 개수) 정수 배열을 생성하고, 이곳에 1에서 100까지 범위의 정수를 랜덤하게 삽입하라. 배열에는 같은 수가 없도록 하고 배열을 출력하라.
package test;
import java.util.Scanner;
public class RandomArrayEx {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("정수 몇 개? ");
int count = scan.nextInt();
int[] arr = new int[count];
for(int j = 0; j < count; j++)
arr[j] = (int) (Math.random()*100 + 1); // 0에서 100까지 정수를 랜덤하게 생성
for(int j = 0; j < count; j++)
System.out.print(arr[j]+" ");
scan.close();
}
}
'Backend > Java' 카테고리의 다른 글
[Java] BufferedReader, BufferedWriter (0) | 2024.02.02 |
---|---|
[Java] 문제풀이 4장 1~6번 (0) | 2024.01.05 |
[Java] ArrayList 사용 정리 (0) | 2023.11.08 |
[Java] Scanner와 BufferedReader의 차이점 (0) | 2023.07.28 |
[Java] 업캐스팅 (0) | 2023.06.09 |