+ 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!"); } } }

12th Mar 2020, 12:59 PM
FerutiiA1t
FerutiiA1t - avatar
2 Respuestas
+ 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 🙂
12th Mar 2020, 1:17 PM
Ketan [#Be Happy 😄]
Ketan [#Be Happy 😄] - avatar
0
Ketan [#Be Happy 😄] thanck you! now it work!
12th Mar 2020, 1:27 PM
FerutiiA1t
FerutiiA1t - avatar