Skip to main content
Notice removed Reward existing answer by h.j.k.
Bounty Ended with rolfl's answer chosen by h.j.k.
Notice added Reward existing answer by h.j.k.
Bounty Started worth 100 reputation by h.j.k.
Tweeted twitter.com/#!/StackCodeReview/status/596755319407128576
Code indenting
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

Trouble with proper Guessing a number, but comments concerning

import java.util.*; //so I can use scanner

public class GuessingGame { public static void main(String[] args) {

import java.util.*; //so I can use scanner

public class GuessingGame {
   public static void main(String[] args) {
   
      Random rand = new Random ();
      int max = 100; //sets limit on the random number generator
      Scanner input = new Scanner(System.in);
      int guess;//so I can compare console input to random number
      boolean play = true;
      int totalGames = 0;  
      int totalGuesses = 0; 
      int bestGame = Integer.MAX_VALUE; 
      
      System.out.println("Can you guess the word?");
      System.out.println("I am sure you cannot guess!");
      System.out.println("Go ahead and try!");
      System.out.println();
      
      while (play) { //so game will restart after a win
      
         System.out.println("I'm thinking of a number between 1 and " + max + "...");
         int numberToGuess = rand.nextInt(max) + 1;
         int numberOfTries = 0;
         //so user can continue guessing until correct
         boolean win = false;  
         while (!win) {
         
            System.out.print("Your guess? " + numberToGuess);
            guess = input.nextInt();
            numberOfTries++;
             
            if (guess == numberToGuess) {
               win = true;  
            } else if (guess > numberToGuess) {
               System.out.println("It's lower.");
            } else if (guess < numberToGuess) {
               System.out.println("It's higher.");
            }      
            input.nextLine();
         }
         
         if (numberOfTries == 1) {
            System.out.println("You got it right in " + numberOfTries + " guess!");
         } else {
            System.out.println("You got it right in " + numberOfTries + " guesses!");
         }   
         
            totalGames++;
            totalGuesses+= numberOfTries;
            System.out.print("Do you want to play again? ");
            
            //so user can choose to play another game    
                  
            String answer = input.nextLine();
            char firstLetter = answer.charAt(0);
            if (firstLetter == 'y' || firstLetter == 'Y') {
            play = true;  
            } else {
            play = false;                               
         } 
          
         
         bestGame = Math.min(bestGame, numberOfTries);
         System.out.println();               
      }
      
      System.out.println("Overall results:");
      System.out.println("Total games   = " + totalGames);  
      System.out.println("Total guesses = " +  totalGuesses);
      System.out.printf("Guesses/game  = %.1f \n", totalGuesses/(double)totalGames);
      System.out.println("Best game     = " + bestGame);
   }  
}

}
}

Trouble with proper comments

import java.util.*; //so I can use scanner

public class GuessingGame { public static void main(String[] args) {

  Random rand = new Random ();
  int max = 100; //sets limit on the random number generator
  Scanner input = new Scanner(System.in);
  int guess;//so I can compare console input to random number
  boolean play = true;
  int totalGames = 0;  
  int totalGuesses = 0; 
  int bestGame = Integer.MAX_VALUE; 
  
  System.out.println("Can you guess the word?");
  System.out.println("I am sure you cannot guess!");
  System.out.println("Go ahead and try!");
  System.out.println();
  
  while (play) { //so game will restart after a win
  
     System.out.println("I'm thinking of a number between 1 and " + max + "...");
     int numberToGuess = rand.nextInt(max) + 1;
     int numberOfTries = 0;
     //so user can continue guessing until correct
     boolean win = false;  
     while (!win) {
     
        System.out.print("Your guess? " + numberToGuess);
        guess = input.nextInt();
        numberOfTries++;
         
        if (guess == numberToGuess) {
           win = true;  
        } else if (guess > numberToGuess) {
           System.out.println("It's lower.");
        } else if (guess < numberToGuess) {
           System.out.println("It's higher.");
        }      
        input.nextLine();
     }
     
     if (numberOfTries == 1) {
        System.out.println("You got it right in " + numberOfTries + " guess!");
     } else {
        System.out.println("You got it right in " + numberOfTries + " guesses!");
     }   
     
        totalGames++;
        totalGuesses+= numberOfTries;
        System.out.print("Do you want to play again? ");
        
        //so user can choose to play another game    
              
        String answer = input.nextLine();
        char firstLetter = answer.charAt(0);
        if (firstLetter == 'y' || firstLetter == 'Y') {
        play = true;  
        } else {
        play = false;                               
     } 
      
     
     bestGame = Math.min(bestGame, numberOfTries);
     System.out.println();               
  }
  
  System.out.println("Overall results:");
  System.out.println("Total games   = " + totalGames);  
  System.out.println("Total guesses = " +  totalGuesses);
  System.out.printf("Guesses/game  = %.1f \n", totalGuesses/(double)totalGames);
  System.out.println("Best game     = " + bestGame);

}
}

Guessing a number, but comments concerning

import java.util.*; //so I can use scanner

public class GuessingGame {
   public static void main(String[] args) {
   
      Random rand = new Random ();
      int max = 100; //sets limit on the random number generator
      Scanner input = new Scanner(System.in);
      int guess;//so I can compare console input to random number
      boolean play = true;
      int totalGames = 0;  
      int totalGuesses = 0; 
      int bestGame = Integer.MAX_VALUE; 
      
      System.out.println("Can you guess the word?");
      System.out.println("I am sure you cannot guess!");
      System.out.println("Go ahead and try!");
      System.out.println();
      
      while (play) { //so game will restart after a win
      
         System.out.println("I'm thinking of a number between 1 and " + max + "...");
         int numberToGuess = rand.nextInt(max) + 1;
         int numberOfTries = 0;
         //so user can continue guessing until correct
         boolean win = false;  
         while (!win) {
         
            System.out.print("Your guess? " + numberToGuess);
            guess = input.nextInt();
            numberOfTries++;
             
            if (guess == numberToGuess) {
               win = true;  
            } else if (guess > numberToGuess) {
               System.out.println("It's lower.");
            } else if (guess < numberToGuess) {
               System.out.println("It's higher.");
            }      
            input.nextLine();
         }
         
         if (numberOfTries == 1) {
            System.out.println("You got it right in " + numberOfTries + " guess!");
         } else {
            System.out.println("You got it right in " + numberOfTries + " guesses!");
         }   
         
            totalGames++;
            totalGuesses+= numberOfTries;
            System.out.print("Do you want to play again? ");
            
            //so user can choose to play another game    
                  
            String answer = input.nextLine();
            char firstLetter = answer.charAt(0);
            if (firstLetter == 'y' || firstLetter == 'Y') {
            play = true;  
            } else {
            play = false;                               
         } 
          
         
         bestGame = Math.min(bestGame, numberOfTries);
         System.out.println();               
      }
      
      System.out.println("Overall results:");
      System.out.println("Total games   = " + totalGames);  
      System.out.println("Total guesses = " +  totalGuesses);
      System.out.printf("Guesses/game  = %.1f \n", totalGuesses/(double)totalGames);
      System.out.println("Best game     = " + bestGame);
   }  
}
Source Link
Eric Irwin
  • 859
  • 7
  • 3

Trouble with proper comments

I always get marked down for my comments and I just wanted to see if these comments are acceptable or what I should include/where I should include them.

import java.util.*; //so I can use scanner

public class GuessingGame { public static void main(String[] args) {

  Random rand = new Random ();
  int max = 100; //sets limit on the random number generator
  Scanner input = new Scanner(System.in);
  int guess;//so I can compare console input to random number
  boolean play = true;
  int totalGames = 0;  
  int totalGuesses = 0; 
  int bestGame = Integer.MAX_VALUE; 
  
  System.out.println("Can you guess the word?");
  System.out.println("I am sure you cannot guess!");
  System.out.println("Go ahead and try!");
  System.out.println();
  
  while (play) { //so game will restart after a win
  
     System.out.println("I'm thinking of a number between 1 and " + max + "...");
     int numberToGuess = rand.nextInt(max) + 1;
     int numberOfTries = 0;
     //so user can continue guessing until correct
     boolean win = false;  
     while (!win) {
     
        System.out.print("Your guess? " + numberToGuess);
        guess = input.nextInt();
        numberOfTries++;
         
        if (guess == numberToGuess) {
           win = true;  
        } else if (guess > numberToGuess) {
           System.out.println("It's lower.");
        } else if (guess < numberToGuess) {
           System.out.println("It's higher.");
        }      
        input.nextLine();
     }
     
     if (numberOfTries == 1) {
        System.out.println("You got it right in " + numberOfTries + " guess!");
     } else {
        System.out.println("You got it right in " + numberOfTries + " guesses!");
     }   
     
        totalGames++;
        totalGuesses+= numberOfTries;
        System.out.print("Do you want to play again? ");
        
        //so user can choose to play another game    
              
        String answer = input.nextLine();
        char firstLetter = answer.charAt(0);
        if (firstLetter == 'y' || firstLetter == 'Y') {
        play = true;  
        } else {
        play = false;                               
     } 
      
     
     bestGame = Math.min(bestGame, numberOfTries);
     System.out.println();               
  }
  
  System.out.println("Overall results:");
  System.out.println("Total games   = " + totalGames);  
  System.out.println("Total guesses = " +  totalGuesses);
  System.out.printf("Guesses/game  = %.1f \n", totalGuesses/(double)totalGames);
  System.out.println("Best game     = " + bestGame);

}
}