+ 1
[SOLVED] Whats wrong with this code? (Help pls)
Code: import java.util.Scanner; public class MyClass { public static void main(String args[]) { int random = (int)(Math.random() * 50 + 1); System.out.println("On cords "+ random +" was detected ship! Destroy it!"); System.out.println("Enter cords to shoot the ship"); Scanner FirstShootScan = new Scanner(System.in); String FirstShoot = FirstShootScan.nextLine(); if(FirstShoot.equals (random)) { System.out.println("You destroy the ship! Tutorial Complete!"); } else { System.out.println("You miss! Try again!"); } } }
2 Réponses
+ 5
First Thing:-
Here:
if(FirstShoot.equals (random))
You are comparing a String with integer type.
So first you should convert that integer to a string.
You can use this way:
String random=Integer.toString(ran);
Where,ran is integer variable.
Also you are using random function so output will be random too and most times it might land in else part.
Happy Coding 🙂
0
Ketan [#Be Happy 😄] thanck you! now it work!