유정잉

3월 3일 숙제 본문

비공개 메모장 !!!

3월 3일 숙제

유정♡ 2024. 2. 27. 09:40

sjwsrh@naver.com

z.zip
0.03MB

 

 

 

4) while 문을 이용해 숫자를 입력 받아 입력받은 모든 수의 합을 출력해라. 0이 입력되면 입력이 종료되고 합을 출력한다.
import java.util.Scanner;

public class homework {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int sum = 0;

while(true) {
int input = sc.nextInt();

sum += input;

if(input == 0) break;
} System.out.println(sum);
} }

 

6)   while break문을 사용해 0이 입력될 때 까지 입력한 숫자의 개수를 출력

import java.util.Scanner;

public class homework {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int count = 0;

while (true) {
int input = sc.nextInt();

count++;

if (input == 0)
break;
}
System.out.println(count);
} }


Q. 출력결과가 왜-29 가 왜 뜨는지

byte b=127;

int i=100;

System.out.println((byte)(b+i)); 

​답) byte 자료형은 -128~127까지의 값을 표현할 수 있다. 따라서 byte b = 127;은 가능하지만, (byte)(b + i)에서는 두 개의 정수가 더해지고 그 결과가 byte로 캐스트됩니다. 이 때 발생하는 현상은 오버플로우입니다. byte 자료형은 1바이트로 표현되는데, 이 범위를 벗어나는 값이 들어오면 가장 최소값부터 다시 시작하게 됩니다. 이 경우 b와 i의 합은 227이 되는데, 이는 byte 자료형의 범위를 벗어납니다. 따라서 오버플로우가 발생하고, 결과적으로는 -29가 출력됩니다.

 

확인문제 챕터4 1번~7번

1번 답) 2

2번 답)

public class Example {
public static void main(String[] args) {
String grade = "B";
int score = switch (grade) {
case "A" -> 100;
case "B" -> {
int result = 100 - 20;
yield result;
}
default -> 60;
};
System.out.println(score);
} }

3번답)

public class Example {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<10; i++) {
if(i%3 = = 0) {
sum += i;
} }
System.out.println("3의 배수의 합: " + sum);
} }

4번 답)

public class Example {
public static void main(String[] args) {
while(true) {
int num1 = (int)(Math.random()*6) + 1;
int num2 = (int)(Math.random()*6) + 1;
System.out.println("(" + num1 + ", " + num2 + ")");
if( (num1+num2) = = 5) {
break;
} } } }

5번 답)

public class Example {
public static void main(String[] args) {
for(int x=1; x<=10; x++) {
for(int y=1; y<=10; y++) {
if( (4*x + 5*y) = = 60) {
System.out.println("(" + x + ", " + y + ")");
} } } } }

6번 답)

public class Example {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
for(int j=1; j<=i; j++) {
System.out.println("*");
if(j= =i) {
System.out.println();
} } } } }

7번 답)

import java.util.Scanner;
public class Example {
public static void main(String[] args) {
boolean run = true;
int balance = 0;
Scanner scanner = new Scanner(System.in);
while(run) {
System.out.println("-------------------------------------");
System.out.println("1.예금 | 2.출금 | 3.잔고 | 4.종료");
System.out.println("-------------------------------------");
System.out.println ("선택> ");
int menuNum = Integer.parseInt(scanner.nextLine());
switch(menuNum) {
case 1:
System.out.println ("예금액> ");
balance += Integer.parseInt(scanner.nextLine());
break;
case 2:
System.out.println ("출금액> ");
balance -= Integer.parseInt(scanner.nextLine());

break;
case 3:
System.out.println ("잔고> ");
System.out.println(balance);
break;
case 4:
run = false;
break; }
System.out.println(); }
System.out.println("프로그램 종료");
} }

 

확인문제 챕터6

18번 답)

public class ShopService {
private static ShopService singleton = new ShopService();
private ShopService() {}
static ShopService getInstance() {
return singleton;
} }

 

728x90