The game runs fine, however I have to include how many times each letter that is guessed occurs within the word. For example, if the guess is e, the program should print out: The character e occurs in 3 positions. How would I go about doing this??
/* * A program that plays simple word guessing game. In this game the user provides a list of words to the program. * The program randomly selects one of the words to be guessed from this list. * The player then guesses letters in an attempt to figure out what the hidden word might be. * The number of guesses that the user takes are tracked and reported at the end of the game. */
I tried this program, its guessing game. Where program decide 1 number and user will guess number. I am getting 1 problem. When user want to play game again.the random number remain same. So where i can put reset random in code..? And 1 more question if I want to write driver code for this. What should i transfer to driver code.
import java.util.Random; import java.util.Scanner; public class GuessGame { public static void main(String args[]) { String choice="y";
The problem occurs after the user inputs the guess.... it either runs the for loop if guess = numtoguess and reveals the answer even if the user input is wrong.... or it always runs the first if statement in the while loop if guess!= to numtoguess... heres the code
public static void main(String[] args) { String[] Answers = {"yes", "Yes", "No", "no"}; String Name = JOptionPane.showInputDialog(null, "Hello, What is your name?","Random Game", JOptionPane.QUESTION_MESSAGE); String UI = JOptionPane.showInputDialog(null, Name + " do you want to play a game", "Random Game", JOptionPane.QUESTION_MESSAGE);
I've been working on a program where you can interact with the computer and play a guessing game of numbers between 1 and 100. It's mainly finished, I just need to add in my comments and stuff. My one problem about it that I cannot figure out is trying to get the results to give the correct int. At the end of the game, it's supposed to return the user information about how well they did like this:
Overall results: total games = int total guesses = int guesses/game = double best game = int
best game should give you the least amount of guesses you had during a particular round of the game. Here is what I have so far:
import java.util.*; public class Guess { public static final int MAX = 100; public static void main(String[] args) { intro(MAX); Scanner console = new Scanner(System.in); int tempBestGame = 0;
The project is to develop the game Translate the Word .... It is asking user to translate a word proposed to and check if the input response is correct. At the end of the game score will be calculated and displayed.
Game Play :
1 - Ask the user to specify , through the console , its name and the number of words to offer . It is up to you to handle exceptions (eg number of words greater than the number you provided ) 2 - Recover user response ( the word translated ) and check whether to continue . (eg you want to continue (y / n)) after each proposal. 3 - compare the response of the user with that which is preset for the word in question . 4 - Show the score at the end ( or at the breakpoint ) . 5 - Save the file in a user name , the score , the number of questions and the start date and end of the game played .
Some notes to consider :
1 - The language (eg, English - French , English - Arabic , etc. . ): It is up to you to specify the language adopted in the game and inform the user of your choice. 2 - The word bank to offer : It is up to you to develop the appropriate means to get the words to propose to the user. That said , the words and their translations can be retrieved :
a. a TXT file b . an XML file . ( Tutorials DOM and SAX ) c . CSV file ( OpenCSV Tutorial ) d. a database ( Tutorial Access) e . through APIs (eg Wordnet and google translate etc . ) . f . a combination of the previous options a, bc , d and / or e . (eg words stored in a txt file and answers retrieved from the api google translate) g . etc. .
Examples of files and databases are attached to the project statement . You will need to add one or more external libraries to your project. Click here for details on adding external libraries to Netbeans .
3 - A user will be associated with the question score if he can translate the word correctly. The score for each question can be calculated based on the number of words / questions to be proposed .
Development : In this project you will need at least a class called Question to encapsulate the word and its translations and provide all necessary methods to manipulate the object type Question.
An interface called IParser to make extensible project. Any class that implements IParser is a parser file (XML , TXT , CSV , etc.). / Database. In your project there will be a single class that implements IParser and will be used to retrieve words and their translations.
Add the ability to store the questions and answers of the user on the hard disk. Make the class Serializable Question
I need to force the user to enter an integer between 1 an 11. The code below works great.... It will work fine if the user inputs any letter or character and reprompt for a number. However once a number is entered(a number out of range-or it would just accept the number) and then the user enters a letter or character that isnt an int the program crashes with a input mismatch exception. I see WHY it is doing this but I cant figure out how to put my while loops to fix this! Here is the code. Someone suggested to Catch the exceptions, then prompt for retry if I encounter one.. however I havn't used the try - catch for exception handling before. Here is my code.
public static int getDecadeSelection() { int decadeChoice = 0; System.out.println( " Choose your decade: " ); System.out.println( " 1 - 1900-1909 " ); System.out.println( " 2 - 1910-1919" ); System.out.println( " 3 - 1920-1929" ); System.out.println( " 4 - 1930-1939" ); System.out.println( " 5 - 1940-1949" );
[Code] ....
It is because I am setting decadeChoice to console.nextInt, so once it passes that if the user inputs a letter it will crash, just not sure how to make it work.
I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.
First, convert letter to number. Second, restore number to word.
Words list: a = 1, b = 2, f = 6 and k = 11.
I have word: "baafk"
So, for first step, it must be: "211611"
Number "211611" must be converted to "baafk".
But, I failed at second step.
Code I've tried:
public class str_number { public static void main(String[] args){ String word = "baafk"; String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11"); System.out.println(word);
[Code] .....
Result for converting to number: baafk = 211611 But, result for converting above number to letter: 211611 = bkfk
What do I miss here?
How to distinguish if 11 is for "aa" and for "k"? D
Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.
import java.util.Random; import java.util.Scanner; public class TicTacToe { private static Scanner keyboard = new Scanner(System.in); private static char[][] board = new char[3][3]; public static int row, col;
I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.
First, convert letter to number. Second, restore number to word.
Words list: a = 1, b = 2, f = 6 and k = 11.
I have word: "baafk"
So, for first step, it must be: "211611"
Number "211611" must be converted to "baafk".
But, I failed at second step.
Code I've tried:
public class str_number { public static void main(String[] args){ String word = "baafk"; String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11"); System.out.println(word);
[Code] ...
Result for converting to number: baafk = 211611
But, result for converting above number to letter: 211611 = bkfk
How to distinguish if 11 is for "aa" and for "k"? Do you have any solutions or other ways for this case?
I wrote a java program for my gui number converter app, the main purpose of this program is to make sure that user enters only a binary number for conversion from binary to other number formats. the problem is my program keeps prompting me to enter correct binary number, no matter if i enter correct binary number, it will still keep prompting me to enter correct number. Here is the code
import java.util.*; public class test { Scanner key = new Scanner(System.in); String in; int b; public test()
I have a Database with Data of people (name, adress, phone number).
If a user enters a phone number (CLIENT) -> the correct person with the name and adress should appear.
I build up the infrastructure and now I'm at the point that I have to create a Webmethod for it. I just worked on projects like addition of two integers, hello "name", ... I think this example now is harder, BUT i can learn a lot!
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
trying to write a program that takes a user inputted number and converts it to a binary number.
Here's what I have:
package com.java2novice.algos; import java.util.Scanner; public class Converter { static Scanner console = new Scanner(System.in); public void printBinaryFormat(int number){ int binary = console.nextInt();
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
- The user wins the first prize if the user input matches all five digits in the lottery number in exact order. -The user wins the second prize if the user input matches any four digits in the lottery number in exact positions. -The user wins the third prize if the user input matches any three digits in the lottery number in its exact position. -The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner; import java.util.Random; class Hw5 { static int getPrize(int g1, int g2, int g3, int g4, int g5, int u1, int u2, int u3, int u4, int u5) {
I am using eclipse for this project. Every time I run this program the choose method will always choose 0. Ignore the Sleep.sleep() method, there is another class in the program that performs it.
import java.util.Scanner; public class gamestart { public static int num; public static String name; public static String diff; public static String choice;
class blackjack { public static void main ( String[] args ) { Random r = new Random(); Scanner keyboard = new Scanner(System.in); String money; String name;
[Code]...
I know people will want me to tidy it up, or do it "their" way, but honestly I just need method of getting the user to press h or s and have the program continue under it's own steam.
What's wrong with my code. It says 'you win' even when I guess an incorrect number. And then the 'win' message keeps repeating. I'm thinking I need a way to generate a new input? And then maybe take the 'win' message out of the loop, but that breaks it too.
I was told to write a program which generates a random number between 0 to 5 *including 5* and give the user 3 chances to guess this number:
-If the user enters a wrong number within 0 to 5, the user has lost one opportunity. -If the user enters a number out of range, the program should prompt the user to enter a number within the range -If the user has 3 unsuccessful attempts, the program will print the number.
I was also instructed to use :
Random randomNumber=new Random(); int i= randomNumber.nextInt(6)
I have attempted it and this is what i have so far.
import java.util.Random; import java.util.Scanner; public class Exercise1 { public static void main(String[] args) { Random randomNumber=new Random();
I wrote the following code for a lottery game. The problem comes when I run it. If one number of a 3 digit number matches, it's supposed to print "you win 1000" But regardless if a number matches or not, it says "sorry, no matches" ...
import java.util.Scanner; public class Lottery { public static void main(String[] args){ //generate lottery number int lottery = (int)(Math.random() * 1000); //Prompt the user to enter numbers Scanner input = new Scanner (System.in); System.out.print("Enter your numbers (3 digits): "); int guess = input.nextInt();
I am making a very simple 2D array game where the player is asked what size they would like the game board to be. After entering, it displays the board and the player 'P' starts at index [0][0]. After that, they are asked for an action, which can be "up", "down", "left", "right", or "exit". I will be including some extra later (like a treasure at the end, or random obstacles), but for now this is what the game consists of.
I was instructed to "do nothing" when/if the player attempts to go out of bounds. I am trying to simply print an error, such as "Out of bounds! Try again.", then prompt the player again for an action. I even tried to make a boolean method to catch it, but to no avail.
I don't want the exception to occur at all. I just simply want the error message to print to the player and ask for another action. I would prefer not to use try/catch, or try/catch/finally since I already tried that and it still gave the exception error.This program consists of two classes. I will show the class containing the main first, then the client-server type class second.
import java.util.Scanner; public class Driver { public static void main(String[] args) { World world = new World(); boolean keepPlaying; keepPlaying = true; boolean isOutOfBounds; isOutOfBounds = false; int height = 0; int width = 0; int x = 0; int y = 0;
I am creating a hangman code. So the game i am creating is 2 player, first player enters the word and second player guesses. i made the code but i don't know how to restrict the first player from adding random characters into the secret word because for my project the word can only contain letters. so i need making the code so the player 1 can only enter letters.
My code is below:
/** * Auto Generated Java Class. */ import java.util.Scanner; public class Hangman { public static void main(String[] args) { Scanner input= new Scanner(System.in); String player1,player2; int wrongguessamount = 0; //keeps the count of the amount of wrong guesses int correctguessamount = 0; //keeps count of the amount of correct guesses int maxincorrect=6;//makes sure the user cant guess more than 6 times
I've run into some problems with my Quiz-game program. What I basicly want it to do is generate a random number that is tied to the other windows I have for the program (I want a new random question to be generated once I have guessed on the first one). I've read about a few different ways to approach this but everything I get is a pure number generator.
I imported
import java.util.Random;
into my program but I don't know where to go from there.