* 자바의 정석 공부 및 조건문,반복문에 관한 연습문제 풀이
1.int형 변수 x가 10보다 크고 20보다 작을 대 true의 조건식
2.char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식
3.char형 변수 ch가 'x'또는 'X'일 때 true의 조건식
4.char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식
5.char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식
6.int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을 때 true인 조건식
7.boolean형 변수 powerOn이 false일 때 true인 조건식
8.문자열 참조변수 str이 "yes"일 때 true인 조건식
- int x 10 20 true 형 변수 가 보다 크고 보다 작을 때 인 조건식
10 < x && x < 20- char ch true 형 변수 가 공백이나 탭이 아닐 때 인 조건식
!(ch == ' ' || ch =='\t')- char ch ‘x' ’X' true 형 변수 가 또는 일 때 인 조건식
ch == 'x' || ch == 'X'- char ch 형 변수 가 숫자(‘0’~‘9’)일 때 인 조건식 true
'0' <= ch && ch <='9'- char ch ( ) true 형 변수 가 영문자 대문자 또는 소문자 일 때 인 조건식
('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')- int year 400 4 100 형 변수 가 으로 나눠떨어지거나 또는 로 나눠떨어지고 으로
나눠떨어지지 않을 때 인 조건식 true
year%400==0 || year%4==0 && year%100!=0- boolean powerOn false true 형 변수 이 일 때 인 조건식
!powerOn- str “yes” true 문자열 참조변수 이 일 때 인 조건식
str.equals("yes")
public class Exercise4_5 {
public static void main(String[] args) {
for(int i=0; i<=10; i++) {
for(int j=0; j<=i; j++)
System.out.print("*");
System.out.println();
}
} // end of main
} // end of class
public class Exercise4_5_2 {
public static void main(String[] args) {
int i=0;
while( i<=10) {
int j=0;
while(j<=i) {
System.out.print("*");
j++;
}
System.out.println();
i++;
}
}
}
class Exercise4_7 {
public static void main(String[] args) {
String str = "12345";
int sum = 0;
for(int i=0; i < str.length(); i++) {
/*
(1) 알맞은 코드를 넣으시오.
*/
}
System.out.println("sum="+sum);
}
}
charAt(int i) i .(i 0 메서드는 문자열에서 번째 문자를 반환한다 의 값은 부터 시작
한다 예를 들어 라는 문자열이 있을 때 는 문자 가 된다.
charAt(int i)을 이용해서 반복문으로 각 문자열의 문자를 하나씩 읽어서 숫자로 변환한
다음 에 계속 더하면 된다 문자 을 숫자 로 바꾸는 방법은 문자 에서 문자 을 sum . '3' 3 '3' '0'
빼주는 것이다 알파벳이나 숫자는 문자코드가 연속적으로 할당되었기 때문에 이런 방법 .
이 가능하다.
뺄셈과 같은 이항연산자는 타입보다 작은 타입은 피연산자 은 int (byte, short, char) int
로 변환한다 그래서 은 으로 변환되고 그 결과는 숫자 이 된다 . '3'-'0' 51 - 48 3 .
'3'-'0' → 51 - 48 → 3
ps. 4-7번문제는 풀지 못 하여 풀이라도 정리해 본다....책에서는 나오지 않는 매서드를 활용하여 아쉽...
class Exercise4_8 {
public static void main(String[] args) {
int value = ( /* (1) */ )
System.out.println("value:"+value);
}
}
1.각 변에 6을 곱한다.
0.0 6 <= Math.random() 6 < 1.0 6
0.0 <= Math.random() 6 < 6.0
2.각 변을 int형으로 변환한다.
(int)0.0 <= (int)(Math.random() 6) < (int)6.0
0 <= (int)(Math.random() 6) < 6
- 각 변에 1을 더한다.
0 + 1 <= (int)(Math.random() 6) + 1 < 6 + 1
1 <= (int)(Math.random() 6) + 1 < 7
이렇게 하면 이제 1과 7사이의 정수 중 하나를 얻을 수 있다. 1은 범위에 포함되고 7은 포함되지 않는다.
public static void main(String[] args) {
int num = 12345;
int sum = 0;
/*
(1)
*'
System.out.println("sum="+sum);
}
}
코드를 입력하세요
```class Exercise4_10
{
public static void main(String[] args)
{
// 1~100 answer . 사이의 임의의 값을 얻어서 에 저장한다
int answer = /* (1) */ ;
int input = 0; // 사용자입력을 저장할 공간
int count = 0; // 시도횟수를 세기위한 변수
// Scanner 화면으로 부터 사용자입력을 받기 위해서 클래스 사용
java.util.Scanner s = new java.util.Scanner(System.in);
do {
count++;
System.out.print("1 100 :"); 과 사이의 값을 입력하세요
input = s.nextInt(); // input . 입력받은 값을 변수 에 저장한다
/*
(2)알맞은 코드를 넣어 완성하시오.
*/
} while(true); // 무한반복문
} // end of main
} // end of class HighLow