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 blog will feature free tutorials for Java and Python. These tutorials are for beginners to create their first program with these programming languages. Python and Java are good languages for anyone to learn. Disclosure: This page contains affiliate links. If you choose to make a purchase after clicking a link, I may receive a commission at no additional cost to you. Thank you for your support!
Example Of How To Create A Java Math Program
//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) {
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;
Subscribe to:
Post Comments (Atom)
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...
-
How to create a Java Coffee Shop Program This is a Java tutorial for creating a coffee shop program, the code below is how to set ...
-
How to create a python fortune-telling program This is a Python 3 tutorial for creating a fortune teller program or cootie ...
-
This is a JFrame tutorial java project is for anyone learning to code. This java tutorial for beginners is a demonstration of how to use t...
-
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...
-
Below I have a snippet of the code needed in order to create Java Grades using the NetBeans IDE. When utilizing the NetBeans IDE, Java GUI’s...
-
This is a JFrame tutorial for anyone beginning to code. This java tutorial for beginners is a demonstration of how to use the Java Textfield...
-
This is a JFrame tutorial java project is for anyone beginning to code. In this java program, the credit card limits can be different. This ...
-
How to create a python program that converts Celsius Temperature to Fahrenheit Temperature This is a Python 3 tutorial for ...
-
This Python tutorial full course is Python for beginners in order to create their first wage program that calculates taxes. In this program,...
No comments:
Post a Comment