public class Final_test {
public static void main(String[] args) {
int b = 5;
b = -b;
// -1 * 5
// Type mismatch: cannot convert from int to byte
int result = 10 / b;
System.out.println(result);
}
}
package ch03;
public class Final_test {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = (++x) + (y--);
// 11 20
// z : 31
System.out.println(z);
// x : 11 y : 19
}
}
public class Final_test {
public static void main(String[] args) {
boolean stop = true;
while(!stop) {
}
}
}
package ch03;
public class Final_test {
public static void main(String[] args) {
// 534 자루의 연필
int pencils = 534;
// 30명의 학생
int students = 30;
//1인당 몇개
int pencilsPerStudent = pencils/students;
System.out.println("1인당 연필 : " + pencilsPerStudent);
//남은 연필 개수
int pencilsLeft = pencils % students;
System.out.println("남은 연필 : " + pencilsLeft);
}
}
public class Final_test {
public static void main(String[] args) {
int var1 = 5;
int var2 = 2;
// 5 / 2 = 2.0 -> 왜? 정수나누기 정수는 정수인 결과값이 출력됨
//double var3 = var1 / var2 ;
double var3 = (double)var1 / var2 ; // 해결하기 위해선 두값 중 아무값에 실수값을 부여
int var4 = (int)(var3 * var2);
System.out.println(var4);
}
}
public class Final_test {
public static void main(String[] args) {
// 십의 자리이하를 버려라.(356 -> 300)
int value = 356;
System.out.println((value/100) * 100);
//(value/100) 3이 남고 * 100을하면 300이 나옴
}
}
public class Final_test {
public static void main(String[] args) {
float var1 = 10f;
float var2 = var1 / 100;
System.out.printf("%.9f\n",var2);
System.out.printf("%.9f\n",0.1);
if(var2 == 0.1) {
System.out.println("10%입니다.");
} else {
System.out.println("10%가 아닙니다.");
}
}
}
public class Final_test {
public static void main(String[] args) {
int lengthTop = 5;
int lengthBottom = 10;
int height = 7;
//double area = ((lengthBottom + lengthTop)*height/2.0);
double area = ((lengthBottom + lengthTop)*(double)height/2);
//52.5가 나와야함
System.out.println(area);
}
}
import java.util.Scanner;
public class Final_test {
public static void main(String[] args) {
// 키보드로 두 실수를 다음과 같이 입력받습니다.(Scanner 이용)
Scanner scanner = new Scanner(System.in);
System.out.print("첫 번째 수 : ");
//double num1 = Double.parseDouble(scanner.nextLine()); // string 타입을 더블로 바꾸기위해서 parseDouble 사용
double num1 = scanner.nextDouble();
System.out.print("두 번째 수 : ");
//double num2 = Double.parseDouble(scanner.nextLine());
double num2 = scanner.nextDouble();
// 입력된 첫 번째 수(num1)에 두번째 수(num2)를 나눈 결과 : 값으로 출력
// 두 번째 수(num2)에 0 또는 0.0을 입력되었을 때
// 결과 : 무한대
if(num2 == 0.0) {
System.out.println("결과 : 무한대");
}else {
System.out.println("결과 : " + num1/num2);
}
}
}
public class Final_test {
public static void main(String[] args) {
int var1 = 10; // 반지름
int var2 = 3; // 원주율(3.14 중에서 소수점 앞)
int var3 = 14; // 원주율(3.14 중에서 소수점 뒤)
// 원의 넓이 = 반지름 * 반지름 * 3.14
// 10 * 10 * 3 + 문자열 + 14
// 10 * 10 * "3." + 14
// 10 * 10 * "3.14"
// 10 * 10 * 3.14
double var4 = var1 * var1 * Double.parseDouble(var2 + "." + var3);
System.out.println("원의 넓이 : "+var4);
}
}
import java.util.Scanner;
public class Final_test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("아이디 : ");
String id = scanner.nextLine();
System.out.println("패스워드 : ");
String strPassword = scanner.nextLine();
// 패스워드는 int 타입으로 변환
int password = Integer.parseInt(strPassword);
if(id.equals("java")){ //아이디가 "java"이고
//string의 타입 비교는 .equals 사용
if(password == 12345) { //패스워드가 12345 이면
System.out.println("로그인 성공");
}else{
System.out.println("로그인 실패 : 패스워드가 틀림");
}
}else {
System.out.println("로그인 실패 : 아이디 존재하지 않음");
}
}
}
12 다음 코드는 비교 연산자와 논리 연산자의 복합 연산식입니다. 연산식의 출력 결과를 괄호() 속에 넣으세요.
public class Final_test {
public static void main(String[] args) {
int x = 10;
int y = 5;
// 10 > 7 && y <= 5
// true && true
// true
System.out.println((x > 7 && (y <= 5)));
// 10 % 3 == 2 || 5 % 2 != 1
// 1 == 2 || 1 != 1
// false || false
// false
System.out.println((x%3 == 2) || (y%2 != 1));
}
}
public class Final_test {
public static void main(String[] args) {
int value = 0;
value = value + 10; // value += 10
value = value - 10; // value -= 10
value = value * 10; // value *= 10
value = value / 10; // value /= 10
}
}
public class Final_test {
public static void main(String[] args) {
int score = 85;
// !(85 > 90)
// !false
// true
String result = (!(score > 90)) ? "가" : "나";
System.out.println(result);
}
}
끝