Luke S.

asked • 03/27/15

Need help to finish this netbeans program?

I had a review and im stuck on this last question.I got a few but I don't know how to finish this. Any help would be appreciated. Im just trying to finish this so I can study it.
So far I have
//Program to input 2 numbers through the keyboard and print their sum and product
package slproject
import java.util.*;
Public class main{
Public static void main(String[]) args)
double x,y;

The rest I have to plug in are
System.out.println("x="+x):
x=a.nextDouble();
System.out.println("y="+y);
y=b.nextDouble();
Scanner b= new Scanner(System.in);
System.out.println("Please enter a value for x:");
System.exit(0);
{System.out.println("The product of "+ x +" and "+ y +"="+(x*y));
{System.out.println("The sum of "+ x +" and "+ y +"="+(x+y));
System.out.println("Please enter a value for y:");
Scanner a = new Scanner(System.in)

1 Expert Answer

By:

Hans K. answered • 03/27/15

Tutor
4 (4)

Benefit from Decades of Academic and Industry Experience in Computing

Luke S.

So after double x,y; should I put the two scanners Scanner a = new Scanner(System.in) and Scanner b = new Scanner(System.in)? Thats the problem for me now and should System.exit(0); be last?
Report

03/27/15

Hans K.

In Java, variables such as x, y, a and b must be declared before they are used. But they do not need to be declared at the beginning of the program. Common practice in Java is to declare them at the point where you first need them. That way, when you later find that you don't actually need them you're more likely to remove the declaration when you remove the code that uses them.
 
Calling the System method exit() causes your program to stop. So any instructions you put after the call to exit() will never be executed. Not much point in that.
Report

03/28/15

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.