입력 받은 값, 변수 타입 별 Scanner 사용 법
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); 정수 입력
String ch = sc.next(); 문자 입력
String str = sc.nextLine(); 문자열 입력
double dou = sc.nextDouble(); 실수 입력
import java.util.Scanner;
public class Array_01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any integer");
int n = sc.nextInt();
System.out.println("Enter any character");
String ch = sc.next();
System.out.println("Enter any string");
Scanner sc1 = new Scanner(System.in);
String str = sc1.nextLine();
System.out.println("Enter any double");
double dou = sc.nextDouble();
System.out.println("integer : " + n +"\n"
+ "character : " + ch +"\n"
+ "String : " + str + "\n"
+ "Double : " + dou);
}
}
Scanner 를 이용한, 연산 코드
import java.util.Scanner;
public class SCanner01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num3=0;
System.out.println("숫자 두개를 입력해주세요");
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
System.out.println("연산자 선택 ( +, -, *. /, % )");
String oper = sc.next();
switch (oper){
case "+":
num3 = num1 + num2;
break;
case "-":
num3 = num1 - num2;
break;
case "*":
num3 = num1 * num2;
break;
case "/":
num3 = num1 / num2;
break;
case "%":
num3 = num1 % num2;
break;
}
System.out.println(num1 + oper + num2+ "=" +num3);
}
}
난수(Random) 발생
임의의 수(난수)를 발생시키는 메써드, Random()을 사용.
import java.util.Random; 클래스 패키지 필요.
int rand = new Random().nextInt(9);
// int를 쓰면 정수만 나오고, double을 쓰면 실수만 나온다. Math.random()을 쓰면 두가지 같이 나오게 할 수 있음.
// 두개 변수를 같이 더해야 Math.random() 처럼 사용할 수 있다.
Math.random() 메써드를 사용하기도 한다.
Random Class
import java.util.Random;
public class random01 {
public static void main(String[] args) {
Random random = new Random();
random.setSeed(System.currentTimeMillis());
// random.setSeed(45); // Seed 를 45로 지정하면 계속 컴파일 해도 같은 값이 나온다.
// random한 숫자를 추출하기 위해서 늘 변하는 값이 있어야 하는데,
// 컴퓨터는 내장된 pseudo(system random number) number를 가지고 있다가
// 랜덤한 숫자가 연산 등에서 필요하면 추출한다.
// 현재 시간을 seed로 사용하면, 현재 시간은 늘 변하기 때문에,
// 랜덤한 숫자 추출의 Seed(씨앗)로 완벽하다.
System.out.println("n개 미만의 랜덤 정수 반환");
System.out.println(random.nextInt(46));
System.out.println(random.nextBoolean());
System.out.println(random.nextLong());
System.out.println(random.nextFloat());
System.out.println(random.nextDouble());
System.out.println(random.nextGaussian());
} // Gauss 분포, 표준편차.
}
n개 미만의 랜덤 정수 반환
35
true
-2397444879181054479
0.3008014
0.0323672555839829
0.019734510373269978
( 값이 계속 바뀐다. )
로또 번호 받기 (중복 있음)
import java.util.Random;
public class random02 {
public static void main(String[] args) {
Random rd = new Random();
rd.setSeed(System.currentTimeMillis());
for(int i=0; i<6; i++) {
System.out.print("[" + (rd.nextInt(45)+1) + "]");
}
// 로또 번호에서는 자주 나오는 숫자가 있으므로, 가중치를 고려하면 더 좋다.
}
}
난수를 이용한 구구단 설계
// 1~10 까지 난수를 발생시킨다면,
// new Random().nextInt((큰수-작은수)+1)+작은수 구문.
// 숫자가 작으면 다른 범위로 갈 수도 있기 때문이다.
import java.util.Random;
public class random03 {
public static void main(String[] args) {
Random random = new Random();
random.setSeed(System.currentTimeMillis());
int num = random.nextInt(8)+2; // 2~9
for(int i=1; i<=9; i++) {
System.out.println(num + " * " + i + " = " + (num*i));
}
}
}
난수를 이용한 알파벳 출력
import java.util.Random;
public class random03 {
public static void main(String[] args) {
// ASCII CODE ('A'=65, 'Z'=90)와 일반 A부터 시작해서 Z까지를
// (('Z'-'A') +1 ) + 'A';
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
int random1 = rand.nextInt((90-65)+1)+65;
System.out.println((char)random1);
int random2 = rand.nextInt('Z'-'A'+1)+'A';
System.out.println(random2);
}
}
01 02 03 04
05 06 07 08
09 10 11 12
출력하기
또는
A B C D
E F G H
I J K L
출력하기
public class Q4 {
public static void main(String[] args) {
int q=0;
for(int i=0; i<=2; i++) {
for(int y=1; y<=4; y++) {
System.out.printf("%02d ", (y+q) );
} // %02d : 2자리 정수를 표현하고, 한자리일 때, 앞에 0을 붙여라
q += 4;
System.out.println();
}
// 이게 정석
// int count=0;
// for(int i=0; i<=2; i++) {
// for(int y=0; y<=3; y++) {
// System.out.printf("%02d ", ++count);
// }
// System.out.println();
// }
// 알파벳 보이기
// char alpha=65;
//
// for(int i=0; i<=2; i++) {
// for(int y=0; y<=3; y++) {
// System.out.printf("%c ", alpha++);
// }
// System.out.println();
// }
}
}
for 문으로
*
**
***
****
*****
과
*
***
*****
*******
*********
만들기
public class Q4 {
public static void main(String[] args) {
// for(int i=0; i<=4; i++) {
// for(int y=1; y<=i; y++) {
// System.out.print("*");
// }
// System.out.println("*");
// }
int q=0;
for(int i=0; i<=4; i++) {
for(int y=3; y>=i; y--) {
System.out.print(" ");
}
for(int z=0; q>=i+z; z++) {
System.out.print("*");
}
q+=3;
System.out.println();
}
}
}
for(int i=0; i<5; i++) {
for(int j=1; j<5-i; j++) {
System.out.print(" ");
}
for(int k=0; k<i*2+1 ; k++) {
System.out.print("*");
}
System.out.println();
}
| JAVA 개념 ( class, instance, parameter, argument, 상속, 캡슐화, 다형성, 인터페이스, 추상 클래스 ) (0) | 2022.07.26 |
|---|---|
| JAVA for, while, break, continue, label 를 이용한 예문 (0) | 2022.07.26 |
| JAVA 배열(Array) 다차원배열, 배열 데이터 정렬 (0) | 2022.07.22 |
| JAVA 제어문 (if, switch, for, while, do~while) (0) | 2022.07.22 |
| JAVA 연산자 (증감, 산술, 시프트, 비교, 비트, 논리, 조건, 대입) (0) | 2022.07.20 |