
package scanner_quiz;
import java.util.Scanner;
public class Q1_nameNage {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("이름을 입력하세요 ("종료"를 입력하면 종료): ");
String name = sc.nextLine();
if (name.equals("종료")){
System.out.println("프로그램을 종료합니다.");
break;
}
System.out.print("나이를 입력하세요: ");
int age = sc.nextInt();
sc.nextLine();
System.out.println("입력한 이름: "+name+", 나이: "+age);
}
}
}
import java.util.Scanner;
System.out.print("이름을 입력하세요 ("종료"입력시 종료) : ")
String name = sc.nextLine();
if문으로 조건 충족 안하면 while문의 break 걸어주기.
if(name.equals("종료")){
System.out.print("프로그램을 종료합니다.");
break;
}
System.out.print("나이를 입력해주세요");
Int age = sc.nextInt();
sc.nextInt();
!!! 두번째로 나오는'이름'과 나란히 '나이'창을 넣는 것을 방지하기 위해, 'sc.nextInt();'를 넣는다.
scanner_quiz;
import java.util.Scanner;
public class Q2_price {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("상품의 가격을 입력하세요(-1을 입력하면 종료): ");
int price = sc.nextInt();
if (price == -1){
System.out.println("프로그램을 종료합니다.");
break;}
System.out.print("상품의 수량을 입력하세요 : ");
int quantity = sc.nextInt();
sc.nextLine();
System.out.println("총 비용"+ price*quantity);
}
}
}
여기서 내가 틀린점 :
1) if문 안에 '수량'입력창과 '총비용'입력창을 함께 넣음.
2) int total = price * quantity 로 묶는것을 권장.