2014-01-10 53 views
0

我想創建一個Java程序,其中計算機隨機猜測1-100之間的數字,並允許用戶猜測數字。猜數字程序與Java

如果數大於隨機數的程序應該說下: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!"); 
    } 
    } 
    } 
} 
+2

什麼是你的問題?你正在運行一個無限循環? –

+0

你解釋了你想要做的和所有事情,但你從來沒有真正說出你需要什麼幫助。你有問題嗎?或者你只是懶惰,不想完成它? – BobbyD17

+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! 
+0

哇!非常感謝! – user3182418

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

} 

編輯:我目前無聊...添加計數器;)

+0

你可以放一個'else'而不是'else if(guess

+0

非常感謝! :) – user3182418

+0

@鄒鄒幹得好!謝謝;)不客氣user3182418 :) – NiziL

0

你需要祝賀部分遷出while循環。如果你猜的是正確的數字,它將不會進入循環,因此不會顯示該語句。如果您修復了縮進,這會更明顯。它的所有內容都處於相同縮進級別的糟糕形式。

0

好吧,我穿越過這個話題,想和大家分享我想通了,也因爲這是一個有趣的question.This甚至可以作爲一個簡單的game.below是它的僞代碼的代碼: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> 
-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"); 
    } 

}

+0

您是否意識到這個問題比一年多問了?另外你的答案與其他答案差別不大,請閱讀一下goo [回覆](http://stackoverflow.com/help/how-to-answer)是什麼。 –

+0

另請參閱*很好的答案。 ;) –