본문 바로가기
WEB Archive

[HTML] 두 개의 정수와 연산자를 입력받아 데이터를 전송하고 수신하는 실습

by universedevelope 2024. 8. 22.

 Send03.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send03.html</title>
</head>
<body>

<!-- 
	송수신실습 03
	 - 두개의 정수와 연산자 입력받는 html페이지
 -->
 
 <div>
 	<h1>연산</h1>
 	<hr>
 </div>
 <div>
 	<form action="Receive03.jsp" method="post">
		정수1 
		<input type="text" name="su1">
		<select name="cal">
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select> 	
		정수2 
		<input type="text" name="su2"><br>
		<button type="submit">결과 확인</button> 	
		<button type="reset">취소</button> 	
 	</form>
 </div>
 
 
 
 
 
</body>
</html>

 

 

Receive03.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%
	String s1Str = request.getParameter("su1");
	String s2Str = request.getParameter("su2");
	double s3 = 0;
	
	int result = 0;
	double result2= 0;
	String cal = request.getParameter("cal");
	String msg = "";	
	
	try
	{
		int s1 = Integer.parseInt(s1Str);	
		int s2 = Integer.parseInt(s2Str);
		
		if(cal.equals("+"))
		{
			result = s1+s2;
			msg = String.format("입력하신 %d와 %d의 연산 결과는 %d 입니다.", s1, s2, result);
		}
		else if(cal.equals("-"))
		{
			result = s1-s2;
			msg = String.format("입력하신 %d와 %d의 연산 결과는 %d 입니다.", s1, s2, result);
		}
		else if(cal.equals("*"))
		{
			result = s1*s2;
			msg = String.format("입력하신 %d와 %d의 연산 결과는 %d 입니다.", s1, s2, result);
		}
		else if(cal.equals("/"))
		{
			s3 = (double)s2;
			result2 = s1/s3;
			msg = String.format("입력하신 %d와 %f의 연산 결과는 %.1f 입니다.", s1, s3, result2);
		}
		else
			msg = "확인 불가";
	}
	catch(Exception e)
	{
		System.out.println(e.toString());		
	}
	
	

	
	
		
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Receive03.jsp</title>
</head>
<body>
<%=msg %>
</body>
</html>
728x90

댓글