Results 1 to 3 of 3
  1. #1
    scorpius1782 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default simple program help

    I'm suppose to be doing a factorial of a number within certain constraints (say a number between 5 and 10).
    I get the factorial easy enough but I'm getting it for every number no matter what I put in.
    textio is used instead of scanner for our class.

    Java Code:
    public class Factorial {
    	public static void main(String[] args) {
    		int max = 0;
    		int x = 0;
    		long y = 0;
    		System.out.println("Enter a number between 5 and 10.");
    		max = TextIO.getlnInt();
    		if (max > 4 || max <= 10) {
    			y = max;
    			for (x = 1; x <= max-1; x++){
    				y = y * x;
    			
    				}
    			TextIO.putln(y);
    		}
    		
    	}
    }
    Ideally I should be able to put in a number outside the range and the program will do nothing until I put a number in with the correct value. Thanks for the help.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: simple program help

    The problem is in your if-statement: "max > 4 || max <= 10"

    Consider my input: 3. Is it > 4? False! Is it <=10? True! So the condition is true since false || true == true.
    Consider my input: 13. Is it > 4? True! Is it <=10? False! So the condition is true since true || false == true.

    Replace the || with && and it will work as false && true == false.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    scorpius1782 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: simple program help

    Thank you! I dont know why I didn't immediately catch that.. or why I didn't use && to begin with.

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Help with simple program...
    By blink16j in forum New To Java
    Replies: 4
    Last Post: 01-15-2013, 06:59 PM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  4. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 01:44 AM
  5. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-15-2010, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •