🌼1063
📝 삼항 연산자
조건식 ? 피연산자1 : 피연산자2
조건식의 연산결과가 true 이면, 결과는 피연산자 1이고, 조건식의 연산결과가 false 이면 결과는 피연산자2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println( a > b? a : b);
}
}
🌼1064
📝 세 정수 중에 가장 작은 수 출력
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println(((a<b ? a:b)<c )? (a<b ? a:b):c );
}
}
🌼1065
📝 세 정수 중 짝수만 출력
짝수 : 2로 나눈 나머지가 0인 수. num % 2 == 0
홀수 : 2로 나눈 나머지가 1인 수. num % 2 == 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if ( a % 2 == 0 )
System.out.println(a);
if ( b % 2 == 0 )
System.out.println(b);
if ( c % 2 == 0)
System.out.println(c);
}
}
🌼1066
📝 홀수인지 짝수인지 출력
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if ( a % 2 == 0 )
System.out.println("even");
else
System.out.println("odd");
if ( b % 2 == 0 )
System.out.println("even");
else
System.out.println("odd");
if ( c % 2 == 0 )
System.out.println("even");
else
System.out.println("odd");
}
}
🌼1067
📝 이중 if문
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
if ( a < 0 )
{
System.out.println("minus");
if ( a % 2 == 0)
{
System.out.println("even");
}
else
{
System.out.println("odd");
}
}
else
{
System.out.println("plus");
if ( a % 2 == 0)
{
System.out.println("even");
}
else
{
System.out.println("odd");
}
}
}
}
🌼1068
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score >= 90 && score <= 100)
System.out.println("A");
else if (score >= 70 && score <= 89)
System.out.println("B");
else if (score >= 40 && score <= 69)
System.out.println("C");
else if (score >= 0 && score <= 39)
System.out.println("D");
}
}
🌼1069
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
switch (str.charAt(0))
{
case 'A' : System.out.println("best!!!");
break;
case 'B' : System.out.println("good!!");
break;
case 'C' : System.out.println("run!");
break;
case 'D' : System.out.println("slowly~");
break;
default : System.out.println("what?");
break;
}
}
}
🌼1070
📝 switch
break; 나오기 전까지 다음 case 문으로 계속 넘어간다.
continue; case 문에 해당되어도 다음 case문으로 넘어간다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int mon = sc.nextInt();
switch (mon)
{
case 12 :
case 1 :
case 2 : System.out.println("winter"); break;
case 3 :
case 4 :
case 5 : System.out.println("spring"); break;
case 6 :
case 7 :
case 8 : System.out.println("summer"); break;
case 9 :
case 10 :
case 11 : System.out.println("fall"); break;
}
}
}
🌼1071
📝 goto문
자바에는 goto 문이 없어 loop : 반복문 break loop; 사용해야함
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] splitStr = str.split(" ");
loop:
for(int i = 0; i < str.length(); i++)
{
if(Integer.parseInt((splitStr)[i]) == 0)
break loop;
System.out.println(splitStr[i]);
}
}
}
같은 의미의 반복문을 빠져나오는 return; 사용
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] splitStr = str.split(" ");
int i = 0;
while (i < str.length())
{
if(Integer.parseInt((splitStr)[i]) == 0){
return;
}
System.out.println(splitStr[i]);
i++;
}
}
}
🌼1072
📝 int[] num = new int[5];
num 이라는 숫자 배열은 5칸이 있는 배열이다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] num = new int[n];
for(int i = 0; i < n; i++)
num[i] = sc.nextInt();
reget:
for(int i = 0; i < n; i++) {
System.out.println(num[i]);
if (i == n) {
break reget;
}
}
}
}
📝 nextInt() 정수값을 받고 바로 nextLine() 을 하면 값이 멈춰있음
그러므로 nextLine() 명령어를 한번 더 언급해 다음 값을 받을 수 있도록 해줘야 함.
//import java.util.Arrays;
import java.util.Scanner;
public class 코드업_1072 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine(); // 엔터가 나올때까지 읽어라.
String num = sc.nextLine();
String[] splitNum = num.split(" ");
// System.out.println(Arrays.toString(splitNum));
loop:
for(int i = 0; i < n; i++)
{
System.out.println(splitNum[i]);
if ( i == n)
break loop;
}
}
}
📝 문자열로 받아 문제 해결
//import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strN = sc.nextLine();
int n = Integer.parseInt(strN);
String num = sc.nextLine();
String[] splitNum = num.split(" ");
// System.out.println(Arrays.toString(splitNum));
loop:
for(int i = 0; i < n; i++)
{
System.out.println(splitNum[i]);
if ( i == n)
break loop;
}
}
}
'java 개발일지' 카테고리의 다른 글
코드업 기초100제, #1083 ~ #1091 (1) | 2022.09.15 |
---|---|
코드업 기초100제, #1073 ~ #1082 (1) | 2022.09.13 |
코드업 기초100제, #1053 ~ #1062 (0) | 2022.09.11 |
코드업 기초100제, #1043 ~ #1052 (1) | 2022.09.11 |
코드업 기초100제, #1033 ~ #1042 (1) | 2022.09.10 |
댓글