티스토리 뷰
BY. 후발대 4일차 수업
for문
* 구구단 : 후발대 서림튜터님 코드
public class Prac04 {
public static void main(String[] args) {
for(int i = 2; i < 10; i++) {
for(int j = 1; j < 10; j++) {
System.out.println(i + " * " + j + " = " + (i * j));
}
System.out.println();// 단마다 개행을 위한 한 줄 비움
}
}
}
* 구구단 : 가로로 출력
첫번째 for문 : 행에 해당
두번째 for문 : 열에 해당
\tab은 tab 한 칸 해준다.
public class GuguDan {
public static void main(String[] args) {
for(int i = 2; i<10; i++) { // 2단부터 시작하니까
for(int j = 1; j<10 ; j++) { // 곱하기 1부터 시작하니까
System.out.print(i + " * " + j + " = " + (i*j) + "\t");
}
System.out.println();
}
}
}
* 구구단 세로로 출력 ver.
public class Gugudan {
public static void main(String[] args) {
for(int i=2; i<=9; i++) {
System.out.print("["+i+"단]\t"); // \t 칸 간격 벌림
}
System.out.println();
for(int i=1; i<=9; i++){
for(int j=2; j<=9; j++){
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
}
* 1부터 10까지의 합을 출력하기
public class Prac {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 10; i++) { // 0부터 시작할것이므로 <10
sum +=(i+1); // 그래서 1부터 누적되므로 i+1로 적어준다.
// System.out.println(sum); => 여기다 쓰면 더하는 과정이 전부 보인다!
}
System.out.println(sum);
}
}
* for-each문 사용하여 계절을 하나씩 출력하기
public class Prac {
public static void main(String[] args) {
String[] seasons = {"봄", "여름", "가을", "겨울"};
for (String season : seasons) {
// season은 for을 통해서 담을 변수 season은 꺼내올 곳
// 즉 seasons에서 꺼내서 하나씩 season에 담아줘.
System.out.println(season);
}
}
}
* 정수를 입력받아서 홀수의 개수를 구하는 프로그램 만들기.
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("정수를 입력해주세요: ");
int num = sc.nextInt();
int i = 1; //i는 양의정수이므로.
int total = 0; // 전체 개수 구하는 변수
while (i<=num) {
//홀수이면 값 증가하게
if(i%2 == 1) {
total++;
}
i++;
}
System.out.println(num+ "까지의 홀수의 개수는 " + total + " 개 입니다.");
}
}
* 자연수 n을 입력받아 n까지의 짝수 합을 구하는 프로그램.
public class Prac {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("자연수 n을 입력하세요: ");
int num = sc.nextInt();
int sum = 0;
for (int i = 1; i <=num; i++) {
if(i%2==0) { // 짝수인지를 먼저 판별
sum = sum + i;
}
}
System.out.println(sum);
}
}
* 숫자 10개를 입력을 받아 짝수와 홀수의 개수를 구하는 프로그램
public class Prac {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("숫자를 10개 입력해주세요: ");
int even = 0; //짝수 판별 위한 변수
int odd = 0; // 홀수 판별 위한 변수
for (int i = 0; i < 10; i++) { //0번째 인덱스부터 10개를 돌 거니까
System.out.println((i+1) + "번째 숫자 : ");
int num = sc.nextInt();
if(num%2==0) {
even++;
} else {
odd++;
}
}
System.out.println("짝수의 개수: " + even + " 홀수의 개수: " + odd);
// for문 다 끝난 후에 써줘야하지.
}
}
switch문 활용
* 월별 날짜수를 출력하는 조건식을 만들어 보세요
public class Quiz {
public static void main(String[] args) {
int days;
int month = 5;
switch (month) {
case 2:
days = 28;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
System.out.println(month + "월은 " + days + "일까지 있습니다.");
}
}
728x90
반응형
'[개발] - Java > 간단한 Quiz' 카테고리의 다른 글
카드 3장 발급하여 결제와 비밀번호 변경 로직 만들기 (0) | 2023.01.14 |
---|---|
계산기 만들기 (0) | 2023.01.14 |
스캐너 사용 Quiz 3번째, 조건문 사용 (0) | 2023.01.14 |
스캐너 사용 간단한 퀴즈 두번째 (0) | 2023.01.14 |
스캐너 사용 간단한 Quiz (1) | 2023.01.14 |
Comments