組み込みクラス
33409 ワード
👩🏼💻 カテゴリ
👩🏼💻 Stringクラス
:String変数名=new String(「文字列」)
👩🏼💻 Dateクラス
:Date変数名=new Date()package pack_Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
String txt = date.toLocaleString();
System.out.println("txt => " + txt);
System.out.println("\n날짜만 표기");
System.out.println(txt.substring(0, 13));
System.out.println("\n시간만 표기");
System.out.println(txt.substring(13)); //13부터 마지막까지~
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
System.out.println();
String dateRes = year + "-" + month + "-" + day;
System.out.println(dateRes);
System.out.println("\n\n--------------------\n\n");
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting : " + myDateObj); //format => 형식
//형식 적용 시작
DateTimeFormatter myFormatObj
= DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss a");
//or "dd/MM/yyyy HH:mm:ss a"
//a => 오전/오후
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting : " + formattedDate);
}
}
出力結果)
👩🏼💻 Formatクラス
Number Format:Number Format変数名=Number Format.getInstance();
🔎 Quiz)給与単位の印刷
メインクラスMain)package pack_Form;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("세전급여를 입력해 주세요(단위. 원) : ");
int money = scanner.nextInt();
scanner.close();
Money obj = new Money(money);
String salary = obj.getSalary();
System.out.print("실지급액 : " + salary + "원");
}
}
カスタムクラス通貨)package pack_Form;
public class Money {
private int money;
public Money(int money) {
this.money = money;
}
public String getSalary() {
double moneyParam = money - (money * 0.033);
Format objFormat = new Format(moneyParam);
String salary = objFormat.mtd_setForm();
return salary;
}
}
カスタムクラスFormat)package pack_Form;
import java.text.NumberFormat;
public class Format {
private double money;
public Format(double money) {
this.money = money;
}
public String mtd_setForm() {
NumberFormat nf = NumberFormat.getInstance();
String salary = nf.format(this.money);
return salary;
}
}
👩🏼💻 Mathクラス
package pack_Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
String txt = date.toLocaleString();
System.out.println("txt => " + txt);
System.out.println("\n날짜만 표기");
System.out.println(txt.substring(0, 13));
System.out.println("\n시간만 표기");
System.out.println(txt.substring(13)); //13부터 마지막까지~
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
System.out.println();
String dateRes = year + "-" + month + "-" + day;
System.out.println(dateRes);
System.out.println("\n\n--------------------\n\n");
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting : " + myDateObj); //format => 형식
//형식 적용 시작
DateTimeFormatter myFormatObj
= DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss a");
//or "dd/MM/yyyy HH:mm:ss a"
//a => 오전/오후
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting : " + formattedDate);
}
}
Number Format:Number Format変数名=Number Format.getInstance();
🔎 Quiz)給与単位の印刷
メインクラスMain)
package pack_Form;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("세전급여를 입력해 주세요(단위. 원) : ");
int money = scanner.nextInt();
scanner.close();
Money obj = new Money(money);
String salary = obj.getSalary();
System.out.print("실지급액 : " + salary + "원");
}
}
カスタムクラス通貨)package pack_Form;
public class Money {
private int money;
public Money(int money) {
this.money = money;
}
public String getSalary() {
double moneyParam = money - (money * 0.033);
Format objFormat = new Format(moneyParam);
String salary = objFormat.mtd_setForm();
return salary;
}
}
カスタムクラスFormat)package pack_Form;
import java.text.NumberFormat;
public class Format {
private double money;
public Format(double money) {
this.money = money;
}
public String mtd_setForm() {
NumberFormat nf = NumberFormat.getInstance();
String salary = nf.format(this.money);
return salary;
}
}
👩🏼💻 Mathクラス
ex)
🔎🤯 Quiz)身分証明書番号で出力
package pack_String;
import java.util.Scanner;
public class StringBuiltIn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("주민등록번호 입력 : ");
String num = scanner.next();
num = num.trim(); //입력값 앞뒤 공백 제거
////////// 성별 구분 코드 //////////
String genderCode = num.substring(7, 8);
String gender = "";
if (Integer.parseInt(genderCode) % 2 == 0) {
//Integer.parseInt => String 타입의 숫자를 int 타입으로 변환!!
gender = "Female";
} else {
gender = "Male";
}
////////// 나이 산출 코드 //////////
String yearPrefix = "";
switch (genderCode) {
case "1", "2":
yearPrefix = "19";
break;
case "3", "4":
yearPrefix = "20";
break;
}
String birthYear = yearPrefix + num.substring(0, 2);
// LocalDate obj = LocalDate.now(); //많이 씀!
int age = 2021 - Integer.parseInt(birthYear) + 1;
System.out.println("나이 : " + age);
System.out.println("성별 : " + gender);
scanner.close();
}
}
🔎🤯 Quiz)同じ値を持たない数字を出力する
package pack_RandomQuiz;
public class Main {
public static void main(String[] args) {
System.out.println("1부터 10사이의 난수 3개 출력");
double[] rndNum = new double[3];
int i = 0;
while (true) { //무한 순환
rndNum[i] = Math.floor(10*Math.random()+1);
i++;
if(i == 3) { //i가 3일 때 아래 내용 진행!
if(! (rndNum[0] == rndNum[1]
|| rndNum[1] == rndNum[2]
|| rndNum[2] == rndNum[0]) ){
System.out.print((int)rndNum[0] + " ");
System.out.print((int)rndNum[1] + " ");
System.out.println((int)rndNum[2]);
break;
} else {
i = 0;
}
}
}
}
}
Reference
この問題について(組み込みクラス), 我々は、より多くの情報をここで見つけました https://velog.io/@losuif/내장클래스テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol