package practice;
import java.util.Scanner;
public class practice {
public static int sum(int n, int m) {
return n+m;
}
public static void main(String[]args) {
int i = 20;
int s;
char a;
s = sum(i,10);
a = '?';
System.out.println(a);
System.out.println("Hello");
System.out.println(s);
}
}
output
?
Hello
30
package practice;
public class practice {
public static void main(String[] args) {
System.out.println(5/2);
System.out.println(5.0/2);
System.out.println(5%2);
System.out.println(1.0-0.1-0.1-0.1-0.1-0.1);
System.out.println(1.0-0.9);
}
}
output
2
2.5
1
0.5000000000000001
0.09999999999999998
package practice;
public class practice {
public static void main(String[]args) {
final double PI = 3.14;
double radius = 10.0;
double circleArea = radius*radius*PI;
System.out.println("원의 면적 = " + circleArea);
}
}
output
원의 면적 = 314.0
package practice;
public class practice {
public static void main(String[] args) {
int i = 'a';
System.out.println(i);
char c = 97;
System.out.println(c);
int a = '2'+'3';
System.out.println(a);
int b = 2 + 'a';
System.out.println(b);
System.out.println((char)b);
}
}
output
97
a
101
99
c
package practice;
public class practice {
public static void main(String[] args) {
byte b = 127;
int i = 100;
System.out.println(b+i);
System.out.println(10/4);
System.out.println(10.0/4);
System.out.println((char)0x12340041);
System.out.println((byte)(b+i));
System.out.println((int)2.9+1.8);
System.out.println((int)(2.9+1.8));
System.out.println((int)2.9 + (int)1.8);
}
}
output
97
a
101
99
c
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.println("이름, 도시, 나이, 체중, 독신여부를 빈칸으로 분리하여 입력하세요");
String name = input.next();
String city = input.next();
int age = input.nextInt();
double weight = input.nextDouble();
boolean single = input.nextBoolean();
System.out.print("이름은 " + name + ", ");
System.out.print("도시는 " + city + ", ");
System.out.print("나이는 " + age + ", ");
System.out.print("체중은 " + weight + ", ");
System.out.print("독신 여부는 " + single + "입니다. ");
input.close();
}
}
output
이름, 도시, 나이, 체중, 독신여부를 빈칸으로 분리하여 입력하세요
이서연 수원시 21 20.0 True
이름은 이서연, 도시는 수원시, 나이는 21, 체중은 20.0, 독신 여부는 true입니다.
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("정수를 입력하세요 : ");
int time = input.nextInt();
int second = time % 60;
int minute = (time/60)%60;
int hour = (time/60)/60;
System.out.print(time + "초는 ");
System.out.print(hour + "시간 ");
System.out.print(minute + "분 ");
System.out.print(second + "초 입니다.");
input.close();
}
}
output
정수를 입력하세요 : 700000
700000초는 194시간 26분 40초 입니다.
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
int a = 3, b =3, c = 3;
a+=3;
b*=3;
c%=2;
System.out.println("a = " + a + ",b = " + b + ",c = " + c);
int d =3;
a = d++;
System.out.println("a = " + a + ", d = " + d);
a = ++d;
System.out.println("a = " + a + ", d = " + d);
a = d--;
System.out.println("a = " + a + ", d = " + d);
a = --d;
System.out.println("a = " + a + ", d = " + d);
}
}
output
a = 6,b = 9,c = 1
a = 3, d = 4
a = 5, d = 5
a = 5, d = 4
a = 3, d = 3
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
System.out.println('a'>'b');
System.out.println(3>=2);
System.out.println(-1<0);
System.out.println(3.45 <= 2);
System.out.println(3 == 2);
System.out.println(3!=2);
System.out.println(!(3!=2));
System.out.println((3>2) && (3>4));
System.out.println((3!=2)||(-1>0));
System.out.println((3!=2)^(-1>0));
}
}
output
false
true
true
false
false
true
false
false
true
true
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a year : " );
int year = input.nextInt();
boolean isLeapYear = (year%4==0 && year%100 !=0) || (year%400 == 0);
System.out.println(year + " is a leapyear ? " + isLeapYear);
}
}
output
Enter a year : 2023
2023 is a leapyear ? false
Enter a year : 2000
2000 is a leapyear ? true
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
int a = 3, b = 5;
System.out.println("두 수의 차는 " + ((a>b)?(a-b):(b-a)));
}
}
output
두 수의 차는 2
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
System.out.print("점수를 입력하시오 : ");
int score = scanner.nextInt();
if(score>=80)
System.out.println("축하합니다! 합격입니다");
scanner.close();
}
}
output
점수를 입력하시오 : 95
축하합니다! 합격입니다
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
System.out.print("수를 입력하시오 : ");
int number = in.nextInt();
if(number%3 ==0)
System.out.println("3의 배수입니다");
else
System.out.println("3의 배수가 아닙니다");
in.close();
}
}
output
수를 입력하시오 : 33
3의 배수입니다
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
char grade;
Scanner scanner = new Scanner(System.in);
System.out.print("점수를 입력하세요(0~100) : ");
int score = scanner.nextInt();
if (score>=90)
grade = 'A';
else if (score>=80)
grade = 'B';
else if (score>=70)
grade = 'C';
else if (score>=60)
grade = 'D';
else
grade = 'F';
System.out.println("학점은 " + grade + "입니다.");
scanner.close();
}
}
output
점수를 입력하세요(0~100) : 77
학점은 C입니다.
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
System.out.print("점수를 입력하세요 (0~100) : ");
int score = scanner.nextInt();
System.out.print("학년을 입력하세요 (1~4) : ");
int year = scanner.nextInt();
if(score>=60) {
if(year!=4)
System.out.println("합격!");
else if(score>=70)
System.out.println("합격!");
else
System.out.println("불합격!");
}
else
System.out.println("불합격!");
scanner.close();
}
}
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
char grade;
System.out.print("점수를 입력하세요(0~100) : ");
int score = scanner.nextInt();
switch (score/10) {
case 10:
case 9:
grade = 'A'; break;
case 8:
grade = 'B'; break;
case 7:
grade = 'C'; break;
case 6:
grade = 'D'; break;
default:
grade = 'F'; break;
}
System.out.println("학점은 " + grade + "입니다");
scanner.close();
}
}
output
점수를 입력하세요(0~100) : 100
학점은 A입니다
점수를 입력하세요(0~100) : 88
학점은 B입니다
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
char grade;
System.out.print("영문자 소문자를 입력 : ");
String var = scanner.next();
switch (var) {
case "a":
case "e":
case "i":
case "o":
case "u":
System.out.println(var + " : 모음"); break;
default:
System.out.println(var + " : 자음"); break;
}
scanner.close();
}
}
output
영문자 소문자를 입력 : a
a : 모음
영문자 소문자를 입력 : h
h : 자음
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner scanner = new Scanner(System.in);
System.out.print("무슨 커피 드릴까요 ? ");
String order = scanner.next();
int price = 0;
switch(order) {
case "에스프레소":
case "카푸치노":
case "카페라떼":
price = 3500;
break;
case "아메리카노":
price = 2000;
break;
default:
System.out.println("메뉴에 없습니다!");
}
if (price!=0)
System.out.print(order + "는 " + price + "원입니다.");
scanner.close();
}
}
output
무슨 커피 드릴까요 ? 카푸치노
카푸치노는 3500원입니다.
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
String s3 = "Welcome to Java";
System.out.println("s1 == s2 : " + (s1==s2));
System.out.println("s1 == s3 : " + (s1==s3));
System.out.println("s1.equals(s2) : " + (s1.equals(s2)));
System.out.println("s1.equals(s3) : " + (s1.equals(s3)));
}
}
output
s1 == s2 : false
s1 == s3 : true
s1.equals(s2) : true
s1.equals(s3) : true
package practice;
public class practice {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = new String("Hello");
if(s1 == s2)
System.out.println("s1과 s2는 같은 객체");
else
System.out.println("s1과 s2는 다른 객체");
if(s3 == s4)
System.out.println("s3과 s4는 같은 객체");
else
System.out.println("s3과 s4는 다른 객체");
}
}
output
s1과 s2는 같은 객체
s3과 s4는 다른 객체
package practice;
public class practice {
public static int sum(int n, int m) {
return n+m;
}
public static void main(String[] args) {
int i = 20;
int s;
char a;
s = sum(i,10);
a = '?';
System.out.println(a);
System.out.println("Hello");
System.out.println(s);
}
}
output
?
Hello
30
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("금액 입력 : ");
int price = input.nextInt();
int fifty = price / 50000;
int ten = (price - (fifty * 50000))/10000;
int five = (price - (fifty * 50000) - (ten * 10000))/5000;
int thousand = (price - (fifty * 50000) - (ten * 10000) - (five * 5000))/1000;
int fivehundred = (price - (fifty * 50000) - (ten * 10000) - (five * 5000) - (thousand * 1000))/500;
int hundred = (price - (fifty * 50000) - (ten * 10000) - (five * 5000) - (thousand * 1000) - (fivehundred * 500))/100;
int fivezero = (price - (fifty * 50000) - (ten * 10000) - (five * 5000) - (thousand * 1000) - (fivehundred * 500) - (hundred * 100))/50;
int onezero = (price - (fifty * 50000) - (ten * 10000) - (five * 5000) - (thousand * 1000) - (fivehundred * 500) - (hundred * 100) - (fivezero * 50))/10;
int one = (price - (fifty * 50000) - (ten * 10000) - (five * 5000) - (thousand * 1000) - (fivehundred * 500) - (hundred * 100) - (fivezero * 50) - (onezero * 10));
System.out.println("오만원 : " + fifty);
System.out.println("만원 : " + ten);
System.out.println("오천원 : " + five);
System.out.println("천원 : " + thousand);
System.out.println("오백원 : " + fivehundred);
System.out.println("백원 : " + hundred);
System.out.println("오십원 : " + fivezero);
System.out.println("십원 : " + onezero);
System.out.println("일원 : " + one);
}
}
output
금액 입력 :
63474
오만원 : 1
만원 : 1
오천원 : 0
천원 : 3
오백원 : 0
백원 : 4
오십원 : 1
십원 : 2
일원 : 4
사칙연산을 입력받아 계산하는 프로그램임. 연산자는 기본연산자이고 피연산자는 실수형이다. 피연산자와 연산자 입력은 빈칸으로 구분한다. 0으로 나눌 경우 "0으로 나눌 수 없습니다."를 출력함
연산식 입력 : 2 + 4
2 + 4 = 6
package practice;
import java.util.Scanner;
public class practice {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
double x1, x2, res;
String cal;
System.out.print("연산자 입력 : ");
x1 = input.nextDouble();
cal = input.next();
x2 = input.nextDouble();
if(cal.equals("+")) {
res = x1 + x2;
System.out.print(x1 + cal + x2 + " = " + res);
}
else if(cal.equals("-")) {
res = x1 - x2;
System.out.print(x1 + cal + x2 + " = " + res);
}
else if(cal.equals("*")) {
res = x1 * x2;
System.out.print(x1 + cal + x2 + " = " + res);
}
else if (cal.equals("/")) {
if(x2 == 0) {
System.out.print("0으로 나눌 수 없습니다.");
}
else {
res = x1 / x2;
System.out.print(x1 + cal + x2 + " = " + res);
}
}
}
}
output
연산자 입력 : 2 + 4
2.0+4.0 = 6.0