gogoWebsite

Java Enter two numbers and output their sum

Updated to 8 hours ago
Question description

Enter two integers and output their sum.

Enter a description

Enter two integers on the same line, separated by spaces

Output description

The sum of two integers and output a new line

Enter sample
3 -5
Output sample
-2
Program code
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); 
        int a = scanner.nextInt();   // Keyboard input a
        int b = scanner.nextInt();   // Keyboard input b
        System.out.println(a + b);  // Output their sum
    }
}  

Question description

Read two double types of numbers greater than or equal to 0 through the keyboard, find their sum, and output the two numbers and their sum in an independent line in the specified format.

Enter a description

Two non-negative double types, separated by spaces in the middle

Output description

Output in separate lines:
a+b=sum
Among them, a, b, sum all retain two decimal places

Enter sample
33.245 10.1
Output sample
33.25+10.10=43.35
Program code
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        double a, b, sum;
        a = scanner.nextDouble();
        b = scanner.nextDouble();
        sum = a + b;
        System.out.printf("%.2f+%.2f=%.2f", a, b, sum);
    }
}

Question description

Enter two double types of numbers, find their sums, and output the sums of the two numbers in the specified format.

Enter a description

Two double types of numbers separated by spaces

Output description

Output (retain 1 decimal number):
The sum of a and b is c

Enter sample
44.156 -21.234
Output sample
The sum of 44.2 and -21.2 is 22.9
Program code
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        double a, b, c;
        a = scanner.nextDouble();
        b = scanner.nextDouble();
        c = a + b;
        System.out.printf("The sum of %.1f and %.1f is %.1f", a, b, a + b);
    }
}