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"); } }
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");
}
}
+ 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.
+ 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
+ 1
Please how are my going to account for the second input?? Jayakrishnađźđł and @Ipang
+ 1
Ipang please how am I going to do that because I need to read two inputs here.
+ 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....*/
0
Thank you Pariket Jayakrishnađźđł
0
You really helped