'자바 입문'에 해당되는 글 2건

  1. 2019.06.18 :: [JAVA] 자바 별 모양 만들기
  2. 2019.06.11 :: [JAVA] 자바 switch문 실습 : 주사위 번호 하나 뽑기
JAVA/자바 예제 2019. 6. 18. 21:38

별로 원하는 사이즈로 사각형,계단,피라미드,다이아몬드 모양을 만드는 코드를 만들어 보았다.

[모양종류]

 

[코드]

import java.util.Scanner;

public class A {

 static void star(char ch) {
Scanner sc = new Scanner (System.in);
int num = sc.nextInt();
int x,y;
switch(num){
case 1 : //사각형 모
Sytem.ut.println("가로 길이");
 x = sc.nextInt();
System.out.println("세로 길이");
 y = sc.nextInt();
for (int i=1;i<=(x*y);i++) {
System.out.print(ch);
if(i%x==0) {System.out.println();}
}
break;
case 2 : //계단모양
System.out.println("계단수");
 x = sc.nextInt();
 for (int i=1; i<=x; i++) {
 for (int j=1; j<=i;j++) {
System.out.print(ch);
}System.out.println();
 }
 break;
case 3 : //역계단모양
System.out.println("계단수");
x = sc.nextInt();
for (int i=1; i<=x; i++) {
for (int j=i; j<=x;j++) {
System.out.print(ch);
}System.out.println();
for (int j=1; j<=i;j++) {
System.out.print(" ");
}
}
break;
case 4 : //피라미드모양
System.out.println("높이");
x = sc.nextInt();
for (int i=1; i<=x; i++) {
for (int j=x; j>i;j--) {
System.out.print(" ");
}
for (int j=1;j<=(2*i-1);j++) {
System.out.print(ch);
}
System.out.println();
}
break;
case 5 : //다이아몬드 모양
System.out.println("다이아몬드 크기");
x = sc.nextInt();
for (int i=1; i<=x; i++) {
for (int j=x; j>i;j--) {
System.out.print(" ");
}
for (int j=1;j<=(2*i-1);j++) {
System.out.print(ch);
}
System.out.println();
}
for (int i=1; i<=x; i++) {
for (int j=1; j<=i;j++) {
System.out.print(" ");
}
for (int j=x; j>=(2*i-2);j--) {
System.out.print(ch);
}System.out.println();

} case 5 : //5번 다이아몬드 모양
System.out.println("다이아몬드 크기");
x = sc.nextInt();
for (int i=1; i<=x; i++) {
for (int j=x; j>i;j--) {
System.out.print(" ");
}
for (int j=1;j<=(2*i-1);j++) {
System.out.print(ch);
}
System.out.println();
}
for (int i=1; i<=x; i++) {
for (int j=1; j<=i;j++) {
System.out.print(" ");
}
for (int j=x; j>=(2*i)-(x-2);j--) {
System.out.print(ch);
}System.out.println();

}break;
default:
System.out.println("에러");
break;
}//end switch
}

public static void main(String[] args) {
System.out.println("1.사각형\t 2.계단\t 3.뒤집힌 계단");
System.out.println("4.피라미드\t 5.다이아몬드");
star('*');
} //end main
}//end class A

posted by 스노(Snow)
:
JAVA/자바 예제 2019. 6. 11. 21:06

[switch문]

자바 프로그램에서 제어문의 종류는 조건문과 반복문이 있습니다.

조건문에는 if문과 switch문이 있고, 반복문에는 for문, while문, do-while문이 있습니다.

if문은 조건식의 결과가 true, false 이렇게 두 가지밖에 없어서 경우의 수가 많아질수록 else-if를 반복적으로 추가해야 해서 코드가 복잡해집니다, 그러나 swich문은 변수의 값에 따라서 실행문이 결정되어 if문보다 코드가 간결해집니다.

 

Math.random() 메소드는 0.0과 1.0 사이에 속하는 난수 하나를 리턴합니다.

0.0    <= Math.random()         <1.0

0.0*6 <= Math.random()         < 1.0*6

0.0    <= Math.random()         < 6.0

0      <= (int)Math.random()    <6

1      <= (int)Math.random()+1 <7

 

case 끝에 break를 적은 이유는 다음 case를 실행하지 말고 switch를 빠져나오라는 뜻입니다. break가 없을 경우 다음 case가 실행됩니다.

[결과]

[코드]

public class SwitchDice {
public static void main(String [] args) {
System.out.println("주사위 번호 뽑기");
int num = (int)(Math.random()*6)+1;  // 주사위 번호 하나 뽑기

switch(num) {
case 1 : //num이 1일 경우
System.out.println("주사위 번호 : 1");
break;
case 2 :
System.out.println("주사위 번호 : 2");
break;
case 3 :
System.out.println("주사위 번호 : 3");
break;
case 4 :
System.out.println("주사위 번호 : 4");
break;
case 5 :
System.out.println("주사위 번호 : 5");
break;
default :
System.out.println("주사위 번호 : 6");
break;
}
}
}

posted by 스노(Snow)
: