0

Please can someone help me out?? I don't Know why the code isn't working.

public class Program { public static void main(String[] args) { System.out.println ("Enter your points"); Scanner pointGotten = new Scanner (System.in); System.out.println ("Enter gun price"); Scanner gun = new Scanner (System.in); int pointGotten, gun ; int answer = pointGotten.nextInt() / gun.nextInt(); if (answer >= 12) System.out.println ("Buy it"); else System.out.println ("Try again"); } }

15th Jun 2021, 1:47 PM
Akanlegum John
Akanlegum John - avatar
9 Answers
+ 2
Try it like this import java.util.Scanner; public class Practice { public static void main(String[] args) { Scanner reader = new Scanner (System.in); System.out.println("Enter your points"); int pointGotten = reader.nextInt(); System.out.println("Enter gun price"); int gun = reader.nextInt(); int answer = pointGotten / gun; if (answer >= 12) System.out.println("Buy it"); else System.out.println("Try again"); } }
15th Jun 2021, 2:11 PM
Ipang
+ 1
You have doubly declaration of variable <pointGotten> and <gun>. Each of which were a `Scanner` and `int` In SoloLearn, we can't use multiple `Scanner` object, so use just one `Scanner` object to read all inputs necessary.
15th Jun 2021, 1:52 PM
Ipang
+ 1
You dont need 2 scanner object. Can do work with single scanner object. Remove 2nd gun object. Instead use first pointGitten object edit : you are redeclaring pointGotten,gun variable again for int type variables. use different names.. but as per your code ,no need of int variables ,if there is no need to save input data.Akanlegum John
15th Jun 2021, 1:53 PM
Jayakrishna 🇼🇳
+ 1
Please how are my going to account for the second input?? Jayakrishna🇼🇳 and @Ipang
15th Jun 2021, 2:01 PM
Akanlegum John
Akanlegum John - avatar
+ 1
Ipang please how am I going to do that because I need to read two inputs here.
15th Jun 2021, 2:03 PM
Akanlegum John
Akanlegum John - avatar
+ 1
//corrected code . Read comments..Akanlegum John import java.util.Scanner; public class Program { public static void main(String[] args) { System.out.println ("Enter your points"); Scanner point = new Scanner (System.in); //object creating with point name. System.out.println ("Enter gun price"); //Scanner gun = new Scanner (System.in); no need of this /*int pointGotten, gun ; //no need these since you directly doing calculation without steering in next statement.*/ int answer = point.nextInt() /point.nextInt(); if (answer >= 12) System.out.println ("Buy it"); else System.out.println ("Try again"); } } /* or else you can store values if you need it again later, like, int pointGotten, gun ; pointGotten =point.nextInt(); gun= point.nextInt(); answer=pointGotten/gun; this works same as above code. first actually you need to import Scanner class. see 1st line. Hope it helps....*/
15th Jun 2021, 2:27 PM
Jayakrishna 🇼🇳
15th Jun 2021, 5:20 PM
Akanlegum John
Akanlegum John - avatar
0
You really helped
15th Jun 2021, 5:20 PM
Akanlegum John
Akanlegum John - avatar