Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# java calculator using swing and float for output answers

Lord Jeffrey Mina
Greenhorn
Posts: 6
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class exercise1
{

public static void main(String args[])
{
System.out.println("Selection from the choices below:\n");
System.out.println(" 2. Subtraction");
System.out.println(" 3. Multiplication");
System.out.println(" 4. Division");
System.out.println(" 5. Modulo\n");

if((choice<=5) && (choice>0))
{

System.out.print("\nEnter first number: ");

System.out.print("Enter second number: ");

System.out.println("");

switch (choice)
{

System.out.println(x + " plus " + y + " = " + (x + y) );
break;

case 2: //subtraction no decimal
System.out.println(x + " minus " + y + " = " + (x - y) );
break;

case 3: //multiplication 3 decimal
System.out.println(x + " times " + y + " = " + (x * y) );
break;

case 4: //division 4 decimal
System.out.println(x + " divided by " + y + " = " + (x / y) );
break;

case 5: //modulo
System.out.println(x + " modulo " + y + " = " + (x % y) );
//break;
}

}
else
{
System.out.println("Please enter a 1, 2, 3, 4 or 5.");
}
}
}

Lord Jeffrey Mina
Greenhorn
Posts: 6
please help me for my activity exam. my problem is how to output the required decimal place in each operator. and display it using swing. thanks

Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Hi,
Welcome to the Ranch!

Your question is not clear to me. I see that you read float values, do some calculation and are printing it. I would think the result will have decimal values too. So, you need to TellTheDetails on what the current output is and what is your expected output.

Also, you should first work out the logic and output in a normal Java program before attempting to build the GUI in Swing. You can start reading about building GUI applications here

Since you are new to ranch, I would like to point out a few things:
a) Please refrain from creating multiple posts for the same issue. I have locked your other post. You can continue here.

b) You to have remember that people trying to help you all are volunteers spending their time. So, you cannot expect answers quickly: PatienceIsAVirtue.

c) Please UseCodeTags when posting code. Also, use good brace alignments without which it makes it extremely difficult to read code.