728x90
반응형
SMALL
- 사용방법
- java.util.Random을 import
- Random rd = new Random();
- 다양한 방식으로 사용한다. rd.nextInt() : -2147483648 ~ 2147483647 중 하나 선택 rd.nextInt(숫자) : 0부터 숫자-1 까지의 수 중 하나(정수)를 얻음 rd.nextFloat(), rd.nextDouble() : 0부터 1사이의 실수 를 얻음
package Day02;
import java.util.Random;
public class IfTest {
public static void main(String[] args) {
//TODO Auto-generated method stub
p("If test");
p("*************");
int a = 1;
if(a > 0) {
p("a is positive");
}
p("if-else");
int b = -1;
if(b > 0) {
p("b is positive");
}
else {
p("b is not positive");
}
p("if-else if ***");
int c = 60;
if(c > 50) {
p("c is big number");
}
p("if-else if else");
int d = -30;
if(d > 0) {
p("d is positive");
}
else if(d == 0) {
p("d is zero");
}
else {
p("d is negative");
}
// nested(중첩) if statement
p("if-if *** ");
int math = 90;
int eng = 35;
if(math >= 60) {
if(eng >= 60) {
p("Pass");
}
else
p("Fail");
}
else
p("Fail");
// 조건을 && 등의 연산자로 묶을 수 있음
if(math >= 60 && eng >= 60)
p("pass");
else
p("Fail");
// if 문은 {} 괄호 생략 가능
p("***********");
p("switch");
p("***********");
int i = 3;
switch(i) {
case 1 :
p("1인가?");
case 2 :
p("2였네");
case 3 :
p("3맞지");
default :
p("다 아닌데?");
}
Random rd = new Random();
int j = rd.nextInt(3);
switch(j) {
case 0:
p("꽝");
break;
case 1:
p("꽝");
break;
case 2:
p("축!당첨!");
break;
}
}
public static void p(String str) {
System.out.println(str);
}
728x90
반응형
LIST
'개발 > JAVA' 카테고리의 다른 글
배열 (Array) (0) | 2023.01.09 |
---|---|
제어문(Control Statement) (0) | 2023.01.09 |
User Input (사용자로부터 입력을 받는 기능) (0) | 2023.01.09 |
연산(Operation), 연산자(Operator) (0) | 2023.01.09 |
class first (0) | 2023.01.09 |