2. Java If-Else
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (N%2 != 0)
{System.out.println("Weird");}
else if (N>=2 && N<=5)
{System.out.println("Not Weird");}
else if (N>5 && N<=20)
{System.out.println("Weird");}
else if (N>20)
{System.out.println("Not Weird");}
scanner.close();
}
}
3. Java Stdin and Stdout I
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
4. Java Stdin and Stdout II
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();//๋ฒํผ์ฃผ๊ธฐ
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
5. Java Output Formatting
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s%03d \n", s1, x);
}
System.out.println("================================");
}
}
6. Java Loops I
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int i = 1;
while(i<11){
int result = N * i;
System.out.printf("%d x %d = %d\n",N,i,result );
i++;
}
scanner.close();
}
}
7. Java Loops II
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for(int j=0; j<n; j++){
a= a+b;
if(j>0){
System.out.print(" ");
}
System.out.print(a);
b= 2*b;
}
System.out.printf("\n");
}
in.close();
}
}
8. Java Datatypes
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
System.out.println("* byte");
if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE)
System.out.println("* short");
if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE)
System.out.println("* int");
if(x >= Long.MIN_VALUE && x <= Long.MAX_VALUE)
System.out.println("* long");
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
9. Java End-of-file
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 1;
while(scan.hasNext()){
System.out.println(i+ " "+ scan.nextLine());
i++;
}
//scan.hasNext()๋ ๋ค์์ ํธ๊ฐ ์์๋๊น์ง ๋ฌดํ๋ฐ๋ณต์. ์๋๋ฉด ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด True๋ฅผ ๋ฐํํ๊ธฐ๋ํจ.
scan.close();
//scan.close()๋ ์ค์บ์ ๊ทธ๋ง๋๋๊ฒ.
}
}
10. Java Static Initializer Block
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static int B;
private static int H;
private static boolean flag;
static{
Scanner scan = new Scanner(System.in);
B = scan.nextInt();
H = scan.nextInt();
scan.close();
if(B<=0 || H<=0)
{System.out.println("java.lang.Exception: Breadth and height must be positive");
flag = false;}
else {flag = true;
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}//end of main
}//end of class
--
Java Date and Time
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class Result {
public static String findDay(int month, int day, int year) {
Calendar cal = Calendar.getInstance();
//์บ๋ฆฐ๋ํด๋์ค์ฌ์ฉ๋ฐฉ๋ฒ ๋ก์ปฌ์์คํ
์๊ฐ์ ๋ณด๋ฅผ ํํํ๋๋กํจ.
cal.set(Calendar.MONTH, month-1);
//:1์= 0 ์ด๋ผ -1ํด์ค.
cal.set(Calendar.DATE, day);
cal.set(Calendar.YEAR, year);
//ํ์ ๋ก์ปฌ๋ ์ง๋ฅผ ์ค์ ํด์ ๋ฐ๊ฟ.
String[] day_of_week = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY","SATURDAY"};
//๋ฌธ์ ์์ string์ผ๋ก ํํํ๋ผํ์ผ๋ ์ค์
return day_of_week[cal.get(Calendar.DAY_OF_WEEK)-1];
//์ผ์์ผ์ = 1, ํ ์์ผ=7
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int month = Integer.parseInt(firstMultipleInput[0]);
int day = Integer.parseInt(firstMultipleInput[1]);
int year = Integer.parseInt(firstMultipleInput[2]);
String res = Result.findDay(month, day, year);
bufferedWriter.write(res);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
728x90
๋ฐ์ํ