Results 1 to 5 of 5

Thread: Higher or lower number

  1. #1
    Join Date
    Apr 2008
    Posts
    240

    Higher or lower number

    Hello
    I have done a simple program which will ask a end user to enter between 1 and 100. The idea is that the computer will guess the number 50 and the user has to guess higher or lower. The program is simple not much tough to code on. i wanted to ask weather the computer can guess a randomly given number between 1 to 100.
    Code:
    class HiLo
    {
    	public static void main(String [] args)
    	{
    		Scanner keyb= new Scanner(System.in);
    		
    		//declare variables
    		int comgue = 50;
    		int num;
    		String str;
    		System.out.print("Please type a num between 1 - 100 - ");
    		num = keyb.nextInt();
    		while(num != comgue)
    		{
    			System.out.print("The Computers  " + comgue + " Higher(h) or Lower(l)");
    			str = keyb.nextLine();	
    			if(str.equals("h"))
    			{
    				comgue++;	
    			}
    			else if(str.equals("l"))
    			{
    				comgue--;
    			}
    		}
    		System.out.println("The computer is correctly  " + num);
    	}
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Higher or lower number

    Hi
    Don’t you think it would be better if the computer guessed the midpoint of the range?
    Code:
    50? h
    (50-100]
    75? l
    (50-75)
    63? h
    (63-75)
    69? l
    (63-69)
    66? you get the point, let’s say it was 66
    Also, i think so it would be better if you do not prompt for numbers.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Higher or lower number

    Hi
    I have created a program which is some what similar to your needs. Please chekc the code i have posted it below
    Code:
    public static long rint (long min, long max) {
    
            long n = 1;
    
        n = (long) ((double) Math.random() * max);
        Math.round(n);
        return n;
    }
    If you want to call the method, then just do this
    Code:
    long n = randInt (1, 101);
    Hope this helps you.

  4. #4
    Join Date
    Apr 2008
    Posts
    240

    Re: Higher or lower number

    Hello
    I have updated my code, please check and if you have any ideas on this do post back. I need a help in my program logic. I have posted by code below.
    Code:
    public static void main(String[] args)
    	{
    		Scanner keyb = new Scanner(System.in);
    		int guess1, guess2, guess3;
    		int correct = 0;
    		int rn1 = 1; //(int)(Math.random() * 10);
    		int rn2 = 2; //(int)(Math.random() * 10);
    		int rn3 = 3; //(int)(Math.random() * 10);
    		System.out.print("Please providde the First guess between 0 an 9 - ");
    		guess1 = keyb.nextInt();
    		System.out.print("Please providde the Second guess between 0 an 9 - ");
    		guess2 = keyb.nextInt();
    		System.out.print("Please providde the Third guess between 0 an 9 - ");
    		guess3 = keyb.nextInt();
    		if(guess1 == rn1 || guess1 == rn2 || guess1 == rn3)
    		{
    			correct += 1;
    			if (guess2 == rn1 || guess2 == rn2 || guess2 == rn3)
    			{
    				correct += 1;
    				if (guess3 == rn1 || guess3 == rn2 || guess3 == rn3)
    				{
    					correct += 1;
    					if ( guess1 == rn1 && guess2 == rn2 && guess3 == rn3)
    					{
    						correct += 1;
    					}
    				}
    			}
    		}
    		switch(correct)
    		{
    			case 0:
    			{
    				System.out.println("You have won $0 because you matched NO Numbers in the sequence");
    				break;
    			}
    			case 1:
    			{
    				System.out.println("You have won $10 because you matched One Number in the sequence");
    				break;
    			}
    			case 2:
    			{
    				System.out.println("You have won $100 because you matched TWO Numbers in the sequence");
    				break;
    			}
    			case 3:
    			{
    				System.out.println("You have won $1000 because you matched THREE Numbers in the sequence");
    				break;
    			}
    			case 4:
    			{
    				System.out.println("You have won $1,000,000 because you matched THREE Numbers in PERFECT SEQUENCE");
    				break;
    			}
    		}
    		System.out.println("Random Number = " + rn1 + " Users Guess = " + guess1);
    		System.out.println("Random Number = " + rn2 + " Users Guess = " + guess2);
    		System.out.println("Random Number = " + rn3 + " Users Guess = " + guess3);
    	}

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Higher or lower number

    Hi
    I think if you use the below part of the code them I think your program may work more fast and appropriate.
    Code:
    if(g1 == rn1 || g1 == rn2 || g1 == rn3)
    {
    	   correct += 1;
    }
    if (g2 == rn1 || g2 == rn2 || g2 == rn3)
     {
    	   correct += 1;
    }
    if (g3 == rn1 || g3 == rn2 || g3 == rn3)
    {
    	   correct += 1;
    }
    if ( g1 == rn1 && g2 == rn2 && g3 == rn3)
    {
    	   correct += 1;
    }
    Hope this helps you.

Similar Threads

  1. Replies: 3
    Last Post: 30-01-2012, 06:01 PM
  2. Replies: 3
    Last Post: 16-09-2011, 10:56 AM
  3. Replies: 5
    Last Post: 27-08-2011, 10:28 PM
  4. Replies: 6
    Last Post: 16-08-2011, 10:51 PM
  5. Replies: 5
    Last Post: 21-03-2011, 10:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,489,890.16260 seconds with 17 queries