Multiply if not zero Learn programming Java

Lesson:

Flow Control


Exercise:

Multiply if not zero


Objetive:

Write a java program to ask the user for a number; if it is not zero, then it will ask for a second number and display their sum; otherwise, it will display "0"


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int x, y;

		System.out.print("enter x:");
		x = Integer.parseInt(new Scanner(System.in).nextLine());

		if (x != 0)
		{
			System.out.print("enter y:");
			y = Integer.parseInt(new Scanner(System.in).nextLine());
			System.out.printf("the product of %1$s and %2$s is %3$s" + "\r\n", x, y, x * y);
		}

		if (x == 0)
		{
			System.out.print("0");
		}
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.