chapter4 연습문제
4-1
1 x>10 && x<20;
2 !(ch=="" || ch!="\t")
3 ch == 'x' || ch =='X'
4 ch >= '0' && ch <= '9'
5 ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'
6 year%400==0 || year%4==0 && year%100!=0
7 powerOn == False
8(주의) str == "yes" (x) str.equals("yes")
4-2
int sum=0;
for (int i=1; i<=20; i++) {
if (i%2 != 0 && i%3 != 0)
sum += i;
답 : 73
4-3
int sum = 0;
for (int i=1; i<=10; i++) {
for (int j=1; j<=i; j++) {
sum += j;
}
}
답 : 220
4-4 (어려서워 보면서했다)
int sum = 0;
int s = 1;
int num = 0;
for (int i=1; true; i++, s=-s ) {
num = is;
sum += num;
if (sum>= 100)
break;
}
System.out.println(sum);
System.out.println(num);
}
}
4-5
4-6
4-7
charAt()
public static void main(String[] args) {
String str = "12345";
int sum = 0;
for (int i=0; i<str.length(); i++) {
int a =str.charAt(i)-48;
sum += a;
}
System.out.println("sum="+ sum);
4-8
public static void main(String[] args) {
int value = (int)(Math.random()*6)+1;
System.out.println("value:"+value);
4-9
4-7과 같음
4-10
public static void main(String[] args) {
int answer = (int)(Math.random()*100)+1;
int input = 0;
int count = 0;
System.out.println(answer);
java.util.Scanner s = new java.util.Scanner(System.in);
do {
count++;
System.out.println("1과100사이의 값을 입력하세요");
input = s.nextInt();
if (1 > input || input > 100) {
System.out.println("숫자를 잘못 입력했습니다");
} else if (input == answer) {
System.out.println("정답입니다");
} else if (input > answer) {
System.out.println("더 작은수를 입력하세요");
} else{
System.out.println("더 큰수를 입력하세요");
}
} while (true);
}