vendredi 10 mars 2017

How do i compare strings with user input and repeat it with character until it match the user input?

So far i have been learning python, i have made a program that checks the user input to see if the computer random guess matched it and if not matched then it will loop and try until it get's a match. here's the python code

import string
import random
import time

possibleStrings = string.ascii_uppercase +\
                  string.ascii_lowercase +\
                  string.digits +\
                  " !@#$%^&*()_+<.>/?\\|:"

target = input("Enter your text here: ")
attemptThis = ''.join(random.choice(possibleStrings) for i in range(len(target)))
attemptNext = ''

completed = False
tried = 0
while not completed:
    print(attemptThis)
    attemptNext = ''
    completed = True
    for i in range(len(target)):
        if attemptThis[i] != target[i]:
            completed = False
            attemptNext += random.choice(possibleStrings)
        else:
            attemptNext += target[i]
    tried += 1
    attemptThis = attemptNext
    time.sleep(0.1)
print("Target matched ! This took ", tried, "try.")

So my question is how do i approach to make the same in Java ? I mean like how can i get the random function like in python i'm using python string.ascii function




Aucun commentaire:

Enregistrer un commentaire