Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Higher or lower number

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 16-12-2009
Member
 
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);
	}
}

Reply With Quote
  #2  
Old 16-12-2009
Member
 
Join Date: Apr 2008
Posts: 1,944
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.
Reply With Quote
  #3  
Old 16-12-2009
Member
 
Join Date: May 2008
Posts: 2,283
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.
Reply With Quote
  #4  
Old 16-12-2009
Member
 
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);
	}
Reply With Quote
  #5  
Old 16-12-2009
Member
 
Join Date: Apr 2008
Posts: 1,993
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Higher or lower number"
Thread Thread Starter Forum Replies Last Post
How to change the number to private number in BlackBerry 8520? M@ndy Portable Devices 3 30-01-2012 05:01 PM
Should I buy US number or European number hp ink cartridge for HP printer 5160? Kash123 Hardware Peripherals 3 16-09-2011 10:56 AM
In Empires & Allies my neighbor trashed by the higher level may be I stay protected in lower level Pamina Video Games 5 27-08-2011 10:28 PM
Number of Installs reported by Console and the Number of Sales for Paid Apps doesnít match? PhoebeM Portable Devices 6 16-08-2011 10:51 PM
Vodafone gets the maximum number of users after the mobile number portability Antariksh Portable Devices 5 21-03-2011 10:24 AM


All times are GMT +5.5. The time now is 11:26 PM.