|
| ||||||||||
| Tags: higher number, java, lower number, number system, programming language |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| Higher or lower number
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
| ||||
| ||||
| 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 |
|
#3
| ||||
| ||||
| 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;
} Code: long n = randInt (1, 101); |
|
#4
| ||||
| ||||
| 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
| ||||
| ||||
| 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;
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |