메뉴 건너뛰기

HEUKMYO

program1

140620_[java]랜덤함수

2014.06.20 15:00 read.90

※랜덤

1. Math함수를 이용한 Random 함수 구하기


1) 일반 랜덤 숫자 출력하기


import java.util.Scanner;


public class Test_140620_2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //public static void main
  
  double result = Math.random();
  System.out.println(result);
    
 }

}


2) 1부터 100까지의 랜덤함수 구하기

import java.util.Scanner;


public class Test_140620_2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //public static void main 
  
  int result=(int)(Math.random()*100+1);
  System.out.println(result);
 }

}


2. Random함수를 이용한 랜덤 숫자 구하기

import java.util.Random;
import java.util.Scanner;


public class Test_140620_2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //public static void main 
   
  Random r= new Random();
  int result = r.nextInt(100)+1;

  
  System.out.println(result);
 }

}


예제1

1~99 책 랜덤으로 책을 펼쳤을때 나온 페이지 각 자리수의 합

54페이지 -> 9

import java.util.Random;

public class xx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//public static void main
		
		Random a= new Random();
		int result = a.nextInt(99)+1;
		
		System.out.println("책을 펼쳤을 때 나온 페이지 : "+result);
		
		int c=(result/10)+(result%10);
		
		System.out.println("책을 펼였을때 나온 페이지 각 자리수의 합 : " + c);
	}
}




21 mysql 설치 2014.07.23
20 jsp강좌 2014.07.23
19 140719_01 2014.07.17
18 140711_02_자바 2014.07.11
17 140711-자바스크립트 2014.07.11
16 140710-자바스크립트 2014.07.10
15 140709_01 2014.07.09
14 140707 2014.07.07
13 140707 2014.07.07
12 140704_01 2014.07.04
11 140701_01 2014.07.01
10 140626_01 2014.06.26
9 140625_01 2014.06.26
8 140624_01 2014.06.24
7 140623_[java] switch문 2014.06.23
> 140620_[java]랜덤함수 2014.06.20
5 140619_[java]조건문 2014.06.19
4 140618_[java]논리연산자 2014.06.18
3 140617_[java]연산자 2014.06.17
2 140616_[java]상수와 변수 2014.06.16
위로