mercredi 24 novembre 2021

if string does not equal int: Java [closed]

I am writing a program in which you need to guess a random number 1-10. Here is my code:

package sami3;

import java.util.Scanner;
import java.util.Random;

public class Samiosman {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random random = new Random();
        int r = random.nextInt(10)+1;
            System.out.println("guess a random number 1-10.");
            Scanner scanner = new Scanner(System.in);
            String how = scanner.next();
            if (String.valueOf(r).equals(how)) {
                System.out.println("right!");


            
                scanner.close();
    }

}
}

Alright so I want to find a way to say "wrong" if the number is wrong but it is not working. I tried to do

else {
 System.out.println("wrong.")
}

It did not really work. Also, I wanted there to be a way that if you get it wrong you can play again. I don't know how to make a person get it wrong so I cant start on that yet. Any suggestions are welcome! Thank you.




Aucun commentaire:

Enregistrer un commentaire