728x90
Problem 01
A~z의 문자하나는 아스키코드 출력
그 이외는 모두 에러 메세지 출력
- 주요점
결국 내가 받은 문자가 어떤것인지 명확하게 정의내려야 함
또한 받은 문자열의 길이를 확인하여서 if문 조건에 넣어주어야 함
- 문자는 String 으로 받고
- charAt(0) 을 통해 문자를 잘라서 아스키 값을 int로 저장해 두어야 함
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("ASCII code teller. Enter a letter: ");
String val = in.nextLine();
int num = val.charAt(0);
if (((num >= 65 && num <= 90) || (num >= 97 && num <= 122)) && (val.length()) < 2) {
System.out.printf("The ASCII code of C is %d\n", num);
} else {
System.out.println("You must input a single uppercase or lowercase alphabet.");
}
}
}
추가로
코드 정리 귀찮을 때는 ctrl + shift + f
Problem 2
유저가 숫자만 입력할때 범위와 출력결과를 보여주는 문제이다.
별 어려움 없이 조건문을 잘 활용하여서 문제를 해결하면 된다.
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int ran = (int)(Math.random()*99+1);
int s = 1,e=100;
while(true) {
System.out.printf("Guess a number (%d-%d): ",s,e);
int ans = in.nextInt();
if(ans == ran) {
System.out.printf("Correct! Number of guesses: %d",ran);
break;
}else if(ans<s || ans>e){
System.out.println("Not in range!");
}else if(ans<ran) {
System.out.println("Too small!");
s = ans+1;
}else if(ans>ran) {
System.out.println("Too large!");
e = ans-1;
}
}
}
}
Problem 3
사용자에게 문자열을 입력받은 다음 새로 문자 한개를 입력 받아 처음 입력 받은 문자열에서 같은 문자의 갯수를 구하는 문제이다.
크게 어렵지 않게 짜면 되지만 여기에서 알아야 되는 것은 java의 String은 C++이나 Python과 다르게 .charAt(i) 을 사용해야만 문자 하나하나에 접근이 가능하다.
import java.util.Scanner;
public class Problem3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a text: ");
String s = in.nextLine();
while(true) {
System.out.print("Enter a letter: ");
String ans = in.nextLine();
int count = 0;
if(ans.length()!= 1)
System.out.println("You must enter a single letter. ");
else {
for(int i = 0 ; i<s.length();++i) {
if(s.charAt(i) == ans.charAt(0)) count++;
}
System.out.printf("There are %d %c's in the text.\n",count,ans.charAt(0));
break;
}
}
}
}
Problem 4
4번째 문자는 3번과 유사하지만 문자열을 찾는 문제이다.
이것을 해결하는 방법은 라이브러리를 쓰는 방법도 존재하지만 지금은 아직 기초이니 String 클래스의 내장 함수인 indexOf 를 사용하기로 하였다.
indexOf는 문자의 위치를 반환하고(0부터 시작) 만약 없다면 -1을 반환한다.
import java.util.Scanner;
public class Problem4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a text: ");
String s = in.nextLine();
while(true) {
System.out.print("Enter a letter: ");
String ans = in.nextLine();
int count = 0;
if(ans.length()== 0)
System.out.println("You must enter a single letter. ");
else {
int pos=-1;
while((pos=s.indexOf(ans,pos+1))!=-1) {
count++;
}
System.out.printf("There are %d instance of \"%s\" \n",count,ans);
break;
}
}
}
}
Problem 5
마지막 문제는 그냥 5번 입력 받고 가장 큰 값인 학생과 점수와 그 다음 학생의 값을 출력하는 문제이다.
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter exam scores of each student.");
int[] f= {0,-1},s= {0,-1};
for(int i = 1 ;i<=5;++i) {
System.out.printf("Score of student %d: ",i);
int tmp = in.nextInt();
if(f[1]==-1) {
f[0]=i;
f[1]=tmp;
}else if(f[1]<tmp){
s[0] = f[0];
f[0] = i;
s[1]=f[1];
f[1]=tmp;
}
}
System.out.printf("The 1st place is student %d with %d points. \n",f[0],f[1]);
System.out.printf("The 2nd place is student %d with %d points. \n",s[0],s[1]);
}
}
728x90
'CS(Computer Science) > 20) 자바' 카테고리의 다른 글
자바를 자바 08 (0) | 2020.11.16 |
---|---|
자바를 자바 08(클래스 세부, call-by-value) (0) | 2020.11.16 |
자바를 자바 07 (0) | 2020.11.16 |
자바를 자바 06 (0) | 2020.11.16 |
자바를 자바 05(String/input,output/Control Flow) (0) | 2020.11.16 |