Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Angjelin Malaj
5,983 PointsI am trying to do a project in java, but i am having some difficulties with it, Can someone help me out with it please?
Develop an algorithm and write the program for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits.
For example, if the secret code is 13720, and the user guesses 83521, the digits 3 and 2 are in the correct position. Thus, the program should respond with 2 and 5. Allow the user to guess only 10 times.
[MOD: edited for better readability]
3 Answers
Mallikarjuna A S
Python Web Development Techdegree Student 7,017 Points@ Angjelin Malaj : Specify the particular difficulty you are facing . We can address a solution to your query
Angjelin Malaj
5,983 PointsI think this should work, but I would like to see a shorter way of doing it?
String code = "37823";
int totalGuesses = 1;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your 5-digit guess");
String guess = keyboard.next();
while(totalGuesses < 10)
{
int correct = 0,
sum = 0;
for(int i = 0; i < code.length(); i++)
{
if(code.charAt(i) == guess.charAt(i))
{
correct++;
sum += Integer.parseInt(code.charAt(i) + "");
}
}
System.out.println(correct + " and " + sum);
if(guess.equals(code))
{
System.out.println("You guessed right!");
totalGuesses = 10;
}
else
{
guess = keyboard.next();
totalGuesses++;
}
}
[MOD: edited code block]
Mallikarjuna A S
Python Web Development Techdegree Student 7,017 Points@ Angjelin Malaj : I dont known about short cut to implement the functionality , But you can Change Functionality to Application it can make user interactive
1) After guess made by user , Application return sum and correct values -- Where you can enhance the functionality to display progress of his guess
Example Input String : 37123 User Input : 32178
Output is Sum is 4 and correct position is 2 3-1-- (functionality Enhancement)
Angjelin Malaj
5,983 PointsAngjelin Malaj
5,983 PointsHi, Mallikarjuna A S, Thanks for replying I think should work, but I would to like to learn a short way of doing it