//////
Search
🗒️

태건님_220922

날짜
2022/09/22
작성자
서태건
카테고리
회고

가위(0), 바위(1), 보(2)

public class RockPaperScissor { final int SCISSOR = 0; final int ROCK = 1; final int PAPER = 2; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("가위(0), 바위(1), 보(2): "); int user = sc.nextInt(); int computer = (int) (Math.random() * 3); if( user == computer ) System.out.println("인간과 컴퓨터가 비겼음"); else if (user == (computer + 1) % 3) // 0은 1한테 지고 1은 2한테, 2는 0한테 진다. System.out.println("인간: " + user + " 컴퓨터: " + computer + " 인간 승리"); else System.out.println("인간: " + user + " 컴퓨터: " + computer + " 컴퓨터 승리"); } }
Java
복사

(user == (computer + 1) % 3) == 인간이 이기는 경우

인간이 가위(0)일때 컴퓨터 보(2) (2+1)%3=0
인간이 바위(1)일때 컴퓨터 보(0) (0+1)%3=1

swich case문

switch(n) { case 1: System.out.println("case 1"); case 2: System.out.println("case 2"); case 3: System.out.println("case 3"); default: System.out.println("default"); }
Plain Text
복사

n=2일때출력

case 2case 3defaultbreak를 쓰지 않으면 해당 줄부터 끝까지 출력

switch(n) { case 12: case 1: case 2: System.out.println("겨울"); break; case 3: case 4: case 5: System.out.println("봄"); break; } } }
Plain Text
복사

case : 중복하여 사용 가능 (ex:계절출력)

객체 생성

class Rectangle{ int width; int height; public void setWidth(int w){ width=w; } public void setHeight(int h){ height=h; } public int getArea() { return width*height; } }
Plain Text
복사
set Width(int w) —> setWidth(int width)로
int width; 와 변수명 같다면 this를 사용하여 아래와 같이 표현
class Rectangle{ int width; int height; public void setWidth(int width){ this.width=width; }
Plain Text
복사