Jump to content

Help with very, very basic JAVA task

FMQ203

So, first, I want to make it clear, I'm not into programming, I've never been, but I'm taking a course about JAVA, and we got a task assigned about making a little game.

Basically, the program makes a 5 digits random number (every digit is different) and the user has to guess it, and, after every attempt the program prints out the amount of digits that are right in the correct spot, and the amount that its right but not in the correct spot. For the the first ones I make this and it works! (How to look for them after the user enters a number)(It's in Spanish because I'm from Uruguay :P) :

 

public static int cantBien (String ingreso, String numero) {
        int contador = 0;
        for (int i = 0; i < largo-1; i++) {
           if (ingreso.charAt(i) == numero.charAt(i)) {
                contador++;
           } 
        }
        return contador;
 
But I'm struggling to get the second ones work, I tried this but its returns is wrong data:
 
   public static int cantReg (String ingreso, String numero) {
        int contador = 0;
        for (int j=0; j < largo-1; j++) {
            for (int k=0; k < largo-1; k++) {
                if(ingreso.charAt(k) == numero.charAt(k)) {
                    contador++;
                }
            }
        }
        return (contador-cantBien(ingreso,numero));
 
Hope you can help me!
 
Link to comment
Share on other sites

Link to post
Share on other sites

@FMQ203

 

Use the code tags.

 

Anyway, it seems the function, oh wait, it's method in Java, that returns the number of wrong elements at the same possition doesn't work correctly, am I right?

Lemme make a quick one myself. I don't remember a whole lot of java, but I tested this and it works.

public static int wrongCount(String original, String guess){int k = 0;for(int i = 0; i < original.length(); i++)if(original.charAt(i)!=guess.charAt(i)) k++;return k;}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I think he meant that it should return the amount of characters that are correct, but in the wrong spot.

Here is a simple method for that  :)

public static int wrongCount(String original, String guess) {    int count = 0;    for (int i = 0; i < original.length(); i++) {        for (int j = 0; j < guess.length(); j++) {            if (original.charAt(i) == guess.charAt(j) && i != j) count++;        }    }    return count;}

This method is quite simple, it checks for any correct guesses, and then it checks if the correct guess is in a different spot as the original. If both are true, then add 1 to the final count.

 

Hope this helps!  :D

In programming there are only two really hard problems:
Naming things, cache invalidation and off-by-one errors.

Link to comment
Share on other sites

Link to post
Share on other sites

 public static int cantReg (String ingreso, String numero) {        int contador = 0;        for (int j=0; j < largo-1; j++) {            for (int k=0; k < largo-1; k++) {                if(ingreso.charAt(k) == numero.charAt(k)) {//check this line                    contador++;                }            }        }        return (contador-cantBien(ingreso,numero));}

simple swapping a k for a j will do the trick :)

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

simple swapping a k for a j will do the trick 
:)

 

 

Ah, yes, if the goal is to use the first method to solve the last method, then yes, this is the answer!  :)

In programming there are only two really hard problems:
Naming things, cache invalidation and off-by-one errors.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 public static int cantReg (String ingreso, String numero) {        int contador = 0;        for (int j=0; j < largo-1; j++) {            for (int k=0; k < largo-1; k++) {                if(ingreso.charAt(k) == numero.charAt(k)) {//check this line                    contador++;                }            }        }        return (contador-cantBien(ingreso,numero));}

simple swapping a k for a j will do the trick :)

 

 

I used this, thanks!!

Link to comment
Share on other sites

Link to post
Share on other sites

I think he meant that it should return the amount of characters that are correct, but in the wrong spot.

Aaah.Didn't quite understand that.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×