File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print out random number and state odd/even

 
ahgoon chow
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Need help in how to write a code to check odd or even numbers from a randomly generated number. The generated number should be printed out to say that this is an even or odd number between the preceding number and the number past. E.g. the random number is 6 it is an even number between 5 and 7.

Please help!

 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the Math class to generate a random number. The numbers to the immediate left and right should be the even / odd numbers you are looking for. Is this home work ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to javaranch ahgoon. This post belongs in the java in general forum. A moderator will move it for you there.
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What have you attempted so far ?
 
ahgoon chow
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried the following but got error message and can't proceed.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="luckyN" value="Page Value" scope="page" />
<%@ page import="java.util.*"%>
<% Random r=new Random();
int num = "r.nextInt(12)";
System.out.println(num);
int pno=num-1;
int nno=num+1;
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lucky Number Advisor</title>
</head>
<body>
<% int luckyNumber=(new java.util.Random()).nextInt(12)+1;
//The value will be in the range between 1 (inclusive) to 12 (inclusive)
pageContext.setAttribute("luckyN", "" + luckyNumber);
//set luckyNumber as a page scope variable named luckyN
%>
<h2>Don't Forget to consult me before stepping out from your house</h2>
Hello! Your Lucky Number today is
<c:out value="${luckyN}"/>

<c:set var="luckyN" value="<%=num%>"/>
<c:if test="${luckyN%2==0}">It is an even number and it is between <%=pno%>
and <%=nno%>
<br />
</c:if>
<c:if test="${luckyN%2!=0}">It is an odd number, and it is between <%=pno%> and <%=nno%>
<br />
</c:if>
<c:if test="luckyN"=1>It is an odd number, and it is right before 2.<br />
</c:if>
<c:if test="${luckyN=12}">It is an even number, and it is right after 11.<br />
</c:if>
</body>
</html>
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post a new version of that code with only the "Java" part.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why is everything in the JSP ? Process the data inside a servlet and pass the result to the JSP. Better still, write a plain old java object program that will do the calculation for you
 
Embla Tingeling
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahgoon chow wrote:
Need help in how to write a code to check odd or even numbers from a randomly generated number.


 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it's only simple code, but surely they could have worked it out for themselves?
 
Embla Tingeling
Ranch Hand
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I know it's only simple code, but surely they could have worked it out for themselves?


Simple things are often the hardest conceptually.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic