[Java] While, do-While, Math.random() and Random Class
While Statements and Applications
Initialization of variables; for(;;), you can make it a loop by putting true in place of the conditional expression. And just like the for statement, the multiplication table can be output in the while statement. Variable initialization; do{ do is executed first, and then if the condition is met, it is repeated again. Factorial n! Find the value In other words, when performing factorial, natural numbers from 1 to n must be sequentially multiplied. 소수 구하기 소수는 1과 자기자신 외의 값만으로만 나누어지는 수이다. 즉, 1과 음수는 될 수 없기에 먼저 제외한다. 그리고 InputMismatchException 는 정수 값이 아닌 문자 입력시에 발생할 에러를 catch 처리한다. // 시작값과 끝값 사이의 소수를 구하고 각각의 개수, 합, 전체 나열하고자 할 때의 코드를 확인하고 작성해보기 0 and 1 using the Math.random() method. When you want to find a random integer, (int)Math.random()*interval range+start value; It can be obtained with If you calculate by substituting real random numbers, you can see why an expression like this is created. //Write a code to obtain an authentication key, create an authentication key consisting of a 3-digit integer and 4 lowercase letters // You can create random alphabet and lowercase characters by writing them in int and coercively converting them to (char). //컴퓨터의 랜덤 값이 홀수인지 짝수인지 맞추기 //Bingo game where the computer's random values are guessed math.You can achieve the same effect by using the Random class instead of the random() method. In the same way as the Scanner class, a Random object is created and a random variable value is input and substituted into an integer type. At this time, rnd.nextInt(100-1@@T 2074@@+1)+1; Like (range number)+first number; You can specify the range of the random value by expressing it as:
while (conditional expression) {
Statement to be executed repeatedly; → When the condition is true, it is executed.
cs
do-While Statements and Applications
Statement to be executed repeatedly;
Incremental;
} while(conditional expression);
cs
cs
cs
Math.random( ) and its applications
You can get a random real number between
cs
cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
System.out.println("== Bingo game ==\n");
Random rnd =@ @T2164@@ new Random();
int rndNum @@T2175 @@= rnd.nextInt(100-@@T2180 @@1+1)@@T 2187@@+1;
Scanner sc = new Scanner(System.in);
int cnt = 0;
do {
System.out.print("숫자입력(1~100) => ");
int inputNum = Integer.parseInt(sc.nextLine());
cnt++;
if(inputNum<rndNum) {
System.out.println(">> "+inputNum+"보다 큰 수 입니다.");
} else if(inputNum>rndNum) {
System.out.println(">> "+inputNum+"보다 작은 수 입니다.");
} else {
System.out.println("\n>> 빙고!! "+cnt+"번만에 맞췄습니다.");
sc.close();
break;
}
} while (true);
|
cs |
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going