File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes print out random number and state odd/even Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print out random number and state odd/even" Watch "print out random number and state odd/even" New topic
Author

print out random number and state odd/even

ahgoon chow
Greenhorn

Joined: Nov 12, 2009
Posts: 2
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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 ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Welcome to javaranch ahgoon. This post belongs in the java in general forum. A moderator will move it for you there.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Hi,
What have you attempted so far ?


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
ahgoon chow
Greenhorn

Joined: Nov 12, 2009
Posts: 2
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please post a new version of that code with only the "Java" part.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

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

Joined: Oct 22, 2009
Posts: 237
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
I know it's only simple code, but surely they could have worked it out for themselves?
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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.
 
 
subject: print out random number and state odd/even