랜덤수 / 가위,바위,보 / 수입력배열

조수경·2021년 11월 18일

HTML

목록 보기
61/96
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" href = "../css/mystyle.css" type="text/css">

<script>

rand = parseInt(Math.random() * 100 + 1);

function proc1(){
	
	str = "";
	//입력한 값을 가져온다 - value (입력하거나 선택할 때 )
	invalue = document.querySelector('input').value;
	
	if(rand == invalue) str += "정답입니다";
	else if(rand > invalue) str += "더 크게 입력하세요";
	else str += "더 작은 수로 입력하세요";
	
	 document.querySelector('#result1').innerHTML = str;
 
	
}

function proc2(){
	
	 arr = [ "가위", "바위", "보"];
	 crand = parseInt(Math.random() * arr.length);
	 
	 com = arr[crand];//랜덤발생
	 user = prompt("가위바위보 입력");
	
	 str = "컴퓨터 : " + com + "<br>";
	 str += "user : " + user + "<br>";
	 
	 if(com == user){
		 str += "비김"; 
	 }else if(user == "가위" && com == "보" || 
			 user == "바위" && com == "가위"|| 
			 user == "보" && com == "바위"){
		 
		 str += "user 가 이김"
		 
	 }else{
		 str += "computer가 이김";
	 }
	 document.querySelector("#result2").innerHTML = str;
}

function proc3(){
	//배열 선언 
	
	//java - 크기가 늘어나지 않고 지정
	int lotte[] = new int[]
	
	//스크립트는 지정하든 안하든 조정가능
	var lotto = new Array();
	//6번째 이후에 푸쉬되지 않기 위해서 
	
	while(true){
		//랜덤발생
		rand = parseInt(Math.random() * 45 + 1);
		
		//배열에 저장 유부를 비교 - 위치 찾기, -1이면 배열에 저장
		//배열에 없으면 랜덤수를 배열에 저장
		if(lotto.indexOf(rand) == -1){
			lotto.push(rand)
		}
			
		//그만? 계속? 판단 - 배열의 개수로 판단
		if(lotto.length >= 6) break;
		
	}
	
	//배열을 출력한다
	document.querySelector("#result3").innerHTML = lotto;
}


</script>
</head>
<body>

<div class = "box">
  1~100사이의 랜덤수를 발생한다<br>
    발생된 랜덤 수를 입력하여 맞추기<br>
  result1에는 크다 작다 힌트와 결과를 출력한다<br>
   <br>
   <input type="text">
  <button type = "button" onclick="proc1()">확인</button>
  <div id = "result1"></div>
</div>

<div class = "box">
    가위 바위 보 게임을 할 수 있는 프로그램을 작성하시오. <br>
  (단, 컴퓨터는 랜덤을 이용하고, 사용자는 prompt로 입력 받아서 처리)<br>
  배열로 작성 arr = [ "가위", "바위", "보"]; arr[0], arr[1] arr[2] 0부터 2까지를 랜덤으로 발생시키는것
   <br>
   <input type="text">
  <button type = "button" onclick="proc2()">확인</button>
  <div id = "result2"></div>
</div>

<div class = "box">
  서로중복되지 않는 정수 5개를 입력받아<br>
  배열에 저장하고 출력하는 프로그램을 작성하시오<br>
   <br>
   <input type="text">
  <button type = "button" onclick="proc3()">확인</button>
  <div id = "result3"></div>
</div>

</body>
</html>
profile
신입 개발자 입니다!!!

0개의 댓글