This is a Java program that prompts the user to enter two float values for x and y, and then performs various arithmetic operations on those values, printing the results to the console.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner someObj = new Scanner(System.in);
System.out.println("---------------");
System.out.println("Enter values for x and y: ");
float x = someObj.nextFloat();
float y = someObj.nextFloat();
System.out.println("Add: " + (x + y));
System.out.println("Sub: " + (x - y));
System.out.println("Mul: " + (x * y));
System.out.println("Div: " + (x / y));
}
}
Explanations:
Scanner someObj = new Scanner(System.in);
This line creates a new instance of the Scanner class, and assigns it to a variable named someObj. The System.in parameter tells the Scanner object to read input from the console.
System.out.println("---------------");
System.out.println("Enter values for x and y: ");
These two lines print messages to the console, asking the user to enter values for x and y. The first line just prints a line of dashes for formatting purposes.
float x = someObj.nextFloat();
float y = someObj.nextFloat();
These two lines read in the user's input for x and y using the nextFloat() method of the Scanner class. The input is stored as float variables named x and y.
System.out.println("Add: " + (x + y));
System.out.println("Sub: " + (x - y));
System.out.println("Mul: " + (x * y));
System.out.println("Div: " + (x / y));
These four lines perform arithmetic operations on x and y, and print the results to the console. The +, -, *, and / operators are used to perform addition, subtraction, multiplication, and division, respectively. The results are concatenated with strings that describe the operation being performed, using the + operator.