Reputation: 11
I am trying to create a program in Java in which the computer randomly guesses a number between 1-100 and allows the user to guess to the number.
If the number is lower than the random number the program should say: lower!
and if higher, the program should say: higher!
If the user guesses the right number, it should say congratulations you guessed the right number in X amount of tries
.
This is what I have so far, any help would be appreciated!
import java.util.Scanner;
public class QuestionOne
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
int a = 1 + (int) (Math.random() * 99);
int guess;
System.out.println("I am thinking of a number from 1 to 100 ... guess what it is ?");
guess = keyboard.nextInt();
while(guess != a){
if (guess > a)
{
System.out.println("lower!");
}
else if (guess < a)
{
System.out.println("higher!");
}
else
{
System.out.println("Congratulations. You guessed the number with X tries!");
}
}
}
}
Upvotes: 0
Views: 59725
Reputation: 11
Try this, hope it will help.
public class BinarySearch
{ static Scanner sc = new Scanner(System.in);
public static void main(String a[]) {
int count = 100;
BinarySearch bs = new BinarySearch();
int[] inputArr = bs.takeInputsInCount(count);
System.out
.println("For every question, press Enter if answer is YES, else you can press any other key");
System.out
.println("Think of a number between 1 to 100, press Enter to continue");
String input = sc.nextLine();
if (input.isEmpty()) {
int element = bs.getElementByBinarySearch(inputArr, count);
System.out.println("Your number is : " + element);
} else {
System.out.println("exiting... ");
}
}
private int getElementByBinarySearch(int[] arr, int len) {
int first = 0;
int last = len - 1;
if (isEqual(arr[first]))
return arr[first];
if (isEqual(arr[last]))
return arr[last];
while (last > first) {
int middle = (first + last) / 2;
if (isEqual(arr[middle]))
return arr[middle];
if (isGreater(arr[middle])) {
first = middle + 1;
if (isEqual(arr[first]))
return arr[first];
} else {
last = middle - 1;
if (isEqual(arr[last]))
return arr[last];
}
}
return 0;
}
private boolean isEqual(int m) {
Boolean equalFlag = false;
System.out.println("Is your number :" + m + " ?");
String input = sc.nextLine();
if (input.isEmpty()) {
equalFlag = true;
}
return equalFlag;
}
private boolean isGreater(int m) {
Boolean equalFlag = false;
System.out.println("Is your number greater than :" + m + " ? ");
String input = sc.nextLine();
if (input.isEmpty()) {
equalFlag = true;
}
return equalFlag;
}
private int[] takeInputsInCount(int count) {
int length = count;
int tempArray[] = new int[length];
for (int i = 0; i < length; i++) {
try {
tempArray[i] = i + 1;
} catch (Exception e) {
break;
}
}
return tempArray;
}}
Upvotes: 1
Reputation: 1
import java.util.Random;
public static void main(String[] args)
{
int a = 1 + (int)(Math.random()*99);
int guess = 0;
int count = 0;
while(guess != a)
{
guess = Integer.parseInt(JOptionPane.showInputDialog("Enter a Number"));
count++;
if (guess > a)
{
JOptionPane.showMessageDialog(null,"lower!");
}
else if (guess < a)
{
JOptionPane.showMessageDialog(null,"higher!");
}
}
JOptionPane.showMessageDialog(null,"Congratulations. You guessed the number with " +count+ " tries");
}
}
Upvotes: -2
Reputation: 77
Well I crossed across this topic and would like to share the code I figured out too since it was a fun question.This can even be used as a simple game.below is the psuedocode for it :D
do{
number=(int)(Math.random()*100);
}while(number<10);
do
{
lottery=Integer.parseInt(JOptionPane.showInputDialog(null,"guess the two digit number which is in my mind!!!! \nenter the number"));
if(number<lottery)
{
JOptionPane.showMessageDialog(null,"guess a more lower number");
count ++;
}
else if(number>lottery)
{
JOptionPane.showMessageDialog(null,"guess a more higher number");
count ++;
}
else
JOptionPane.showMessageDialog(null,"you have guessed the correct number "+number+" in"+count+" tries");
}while(lottery!=number);</i>
Upvotes: 0
Reputation: 201437
You weren't getting another input, or keeping count. Try this
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("I am thinking of a number from 1 to 100"
+ " ... guess what it is ?");
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess > a) {
System.out.println("lower!");
} else if (guess < a) {
System.out.println("higher!");
}
}
System.out.println("Congratulations. You guessed the number with "
+ count + " tries!");
}
Output
I am thinking of a number from 1 to 100 ... guess what it is ?
50
higher!
75
lower!
62
Congratulations. You guessed the number with 3 tries!
Upvotes: 1
Reputation: 5140
You forgot to get a new int from the scanner in each loop :)
import java.util.Scanner;
public class QuestionOne
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
int a = 1 + (int) (Math.random() * 99),
guess,
count = 0;
System.out.println("I am thinking of a number from 1 to 100 ... guess what it is ?");
while((guess = keyboard.nextInt()) != a){
if (guess > a)
{
System.out.println("lower!");
}
else
{
System.out.println("higher!");
}
count++;
}
System.out.println("Congratulations. You guessed the number with "+ count +" tries!");
}
}
edit : I'm currently bored... Add the counter ;)
Upvotes: 4
Reputation: 505
You need to move the congratulations part out of the while loop. If you guess the correct number, it will not go into the loop and therefore will not ever display that statement. This would be more obvious if you fixed your indentations. Its bad form to have everything at the same indentation level.
Upvotes: 0