1. 원하는 구구단의 단 출력
(1) for문 이용
for(초기화식; 조건식; 증감식) {
실행문
}
package com.day03;
import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dan;
System.out.print("단을 입력해주세요 : ");
dan = sc.nextInt();
for(int i=1;i<=9;i++) {
System.out.println(dan+ "x" +i+ "=" +(dan*i));
}
sc.close();
}
}
(2) while문 이용
while(조건식) {
실행문
}
package com.day03;
import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dan;
System.out.print("단을 입력해주세요 : ");
dan = sc.nextInt();
int j=0;
while(j<9) {
j++;
System.out.println(dan+ "x" +j+ "=" +(dan*j));
}
}
}
package com.day03;
import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dan;
System.out.print("단을 입력해주세요 : ");
dan = sc.nextInt();
int k=0;
do{
k++;
System.out.println(dan+ "x" +k+ "=" +(dan*k));
}
while(k<9);
sc.close();
}
}
결과
단을 입력해주세요 : 5
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
2. 1부터 사용자가 입력하는 수까지의 합
조건) 사용자가 1에서 5000사이의 수만 입력 가능
package com.day03;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Test2 {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int su, sum;
char ch;
while(true){
do{
System.out.print("1에서 5000사이의 수를 입력해주세요 : ");
su = Integer.parseInt(br.readLine());
} while(su<1 || su>5000);
sum=0;
for(int i=1;i<=su;i++)
sum = sum+i;
System.out.println("1부터 " +su+ "까지의 합 : " +sum+ "\n");
System.out.print("계속 하시겠습니까? [Y/N]");
ch = (char)System.in.read();
if(ch!='Y' && ch!='y')
break;
System.out.println();
System.in.skip(2);
}
}
}
1에서 5000사이의 수를 입력해주세요 : 20
1부터 20까지의 합 : 210
계속 하시겠습니까? [Y/N]y
1에서 5000사이의 수를 입력해주세요 : 10
1부터 10까지의 합 : 55
계속 하시겠습니까? [Y/N]n
3. 구구단 출력
package com.day03;
public class Test3 {
public static void main(String[] args) {
int i, j;
for(i=2;i<=9;i++){
System.out.println(i+ "단");
for(j=2;j<=9;j++){
System.out.println(i+ "x" +j+ "=" +(i*j));
}
System.out.println();
}
}
}
4. 입력받은 정수 짝수 / 홀수 구분
package com.day03;
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
System.out.print("정수를 입력해주세요:");
num = sc.nextInt();
if(num%2==0)
System.out.println(num+"은 짝수입니다.");
else
System.out.println(num+"은 홀수입니다.");
sc.close();
}
}
결과
정수를 입력해주세요.: 17
17은 홀수입니다.
5. 입력받은 두 정수의 대소비교
package com.day03;
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2;
System.out.print("정수 2개를 입력해주세요:");
num1 = sc.nextInt();
num2 = sc.nextInt();
if(num1>num2)
System.out.println(num1+ "이/가 " +num2+ "보다 큰 숫자입니다.");
else if (num1 == num2)
System.out.println(num1+ "와/과 " +num2+ "는 같은 숫자입니다.");
else
System.out.println(num2+ "이/가 " +num1+ "보다 큰 숫자입니다.");
sc.close();
}
}
결과
정수 2개를 입력해주세요.: 47 84
84이/가 47보다 큰 숫자입니다.
6. 입력받은 4개의 정수중 제일 큰 값과 제일 작은 값 출력
package com.day03;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, num4, max, min;
System.out.print("정수를 4개를 입력해주세요:");
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
max = num1;
min = num1;
if(max<num2)
max = num2;
if(max<num3)
max = num3;
if(max<num4)
max = num4;
if(min>num2)
min = num2;
if(min>num3)
min = num3;
if(min>num4)
min = num4;
System.out.printf("\n제일 큰 수 : %d", max);
System.out.printf("\n제일 작은 수 : %d", min);
sc.close();
}
}
결과
정수를 4개 입력해주세요.: 47 87 24 65
제일 큰 수 : 87
제일 작은 수 : 24
7. 입력받은 연도 윤년 / 평년 분류
윤년 구분 계산식 : year%4==0 && year%100!=0 || year%400==0
package com.day03;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year;
System.out.print("연도를 입력해주세요:");
year = sc.nextInt();
if(year%4==0 && year%100!=0 || year%400==0)
System.out.println(year+"년은 윤년입니다.");
else
System.out.println(year+"년은 평년입니다.");
sc.close();
}
}
결과
연도를 입력해주세요.: 2015
2015년은 평년입니다.
8. 구매 수량에 따른 할인률 계산
package com.day03;
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int price=15800;
int pro;
double sale;
System.out.print("상품을 몇 개 구매하시겠습니까:");
pro = sc.nextInt();
if(pro>=30)
sale = (price*pro)*0.2;
else if(pro>=20)
sale = (price*pro)*0.15;
else if(pro>=10)
sale = (price*pro)*0.1;
else
sale = 0;
System.out.println();
System.out.println("구입수량 : " +pro+ "개");
System.out.println(sale+"원이 할인되어 가격은 " +((price*pro)-sale)+ "원 입니다.");
sc.close();
}
}
결과
상품을 몇 개 구매하시겠습니까:15
구입수량 : 15개
23700.0원이 할인되어 가격은 213300.0원 입니다.
9. 입력받은 점수로 합격 / 불합격 / 과락 결정
package com.day03;
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name, ox;
int kor, mat, eng, tot;
double avg;
System.out.println("이름과 국어, 수학, 영어 점수를 입력해주세요.");
System.out.print("(예시 : 홍길동 80 80 80) : ");
name = sc.next();
kor = sc.nextInt();
mat = sc.nextInt();
eng = sc.nextInt();
tot = kor + mat + eng;
avg = tot/3.0;
if(kor<40 || mat<40 || eng<40 && avg <60) {
ox = "과락";
}
else if(avg <60){
ox = "불합격";
}
else{
ox = "합격";
}
System.out.printf("\n이름 : %s, 총점 : %d점, 평균 : %g점, 판정 : %s", name, tot, avg, ox);
sc.close();
}
}
결과
이름과 국어, 수학, 영어 점수를 입력해주세요.
(예시 : 홍길동 80 80 80) : 수지 87 84 97
이름 : 수지, 총점 : 268점, 평균 : 89.3333점, 판정 : 합격
댓글