Example Of How To Create A Java Math Program

Photo by John Schnobrich on Unsplash








How to create a simple math java program

This program uses a while loop and java switch statements to create a java math program that will execute the basic math problems (multiplication, subtraction, addition, division) for users to complete. 


This is a java tutorial for creating a simple java program, and in order to get started I always start with a comment about what the program that I am building is. By using the // keys you are able to then create comments in Java.



//This will be a simple math program that does multiplication, addition, subtraction, and division

After the comment, I would usually then import the functions that I would utilize within the program. In this programming case, I will be utilizing the scanner and JOptionpane function.

import java.util.Scanner;
import javax.swing.JOptionPane;

Next, I will create our public class with the name Simple Math.

public class SimpleMath {

              public static void main(String[] args) {

The while loop continues to execute as long as the condition is true. Remember that in java the curly brackets are important and must be aligned correctly, or you will receive a syntax error.

                              
                                 while (true) {

                                           try {

                                    String output = "";
String input = JOptionPane.showInputDialog("Hello There Lets Do Some Math \n" + "\n 1. Addition \n" + "\n 2. Subtraction \n" + "\n 3. Division \n" + "\n 4. Multiplication \n" + "\n 5. Exit");

                                    int in = Integer.parseInt(input);
                                    Scanner keyboard = new Scanner(System.in);

                                     if (in > 5) {

           JOptionPane.showMessageDialog(null, "Try Again Pick A Number Between 1 and 5");
                                                          }
                                           
 if (in <= 0) {

          JOptionPane.showMessageDialog(null, "Try Again Pick A Number Between 1 and 5");
                                                          }

                                     switch (in) {




// This code utilizes cases that depend upon which selection the user selects the particular selected case would then be called into action.
                                                         
                        case 1:
                                  System.out.println("Enter two numbers to add");

                                  double num1 = keyboard.nextDouble();
                                  double num2 = keyboard.nextDouble();

                                  output += "The answer is " + (num1 + num2);

                                 JOptionPane.showMessageDialog(null, output);

                                 break;

These are some screenshots of a sample run of the program.





The rest of the code for this java program can be found in my video tutorial below.







No comments:

Post a Comment

Great Ways To Learn How To Start Coding

Good Places To Get Help When Learning How To Code There are many places to learn how to do many things not only code, and today in this a...