用Java猜猜一个数字程序

时间:2014-01-10 16:03:25

标签: java loops if-statement while-loop

我正在尝试用Java创建一个程序,其中计算机随机猜测1-100之间的数字,并允许用户猜测数字。

如果该数字低于随机数,程序应该说:lower!如果更高,程序应该说:higher!

如果用户猜到了正确的号码,则应该说congratulations you guessed the right number in X amount of tries

这就是我到目前为止,任何帮助都将不胜感激!

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!");
   }
   }
  }
}

6 个答案:

答案 0 :(得分:3)

你忘了在每个循环中从扫描仪中获取一个新的int:)

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!");
  }

}

编辑:我目前很无聊......添加计数器;)

答案 1 :(得分:1)

你没有得到另一个输入,或保持计数。试试这个

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!");
}

输出

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!

答案 2 :(得分:1)

尝试一下,希望对您有所帮助。

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;
}}

答案 3 :(得分:0)

您需要将祝贺部分移出while循环。如果您猜测正确的数字,它将不会进入循环,因此将不会显示该语句。如果您修改了缩进,这将更加明显。它的形式很糟糕,所有东西都在同一个缩进级别。

答案 4 :(得分:0)

我跨过了这个主题并想分享我想出的代码,因为它是一个有趣的问题。这甚至可以用作一个简单的游戏。下面是它的伪代码: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>

答案 5 :(得分:-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");
    }

}