• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java calculator using swing and float for output answers

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(" 1. Addition");
System.out.println(" 2. Subtraction");
System.out.println(" 3. Multiplication");
System.out.println(" 4. Division");
System.out.println(" 5. Modulo\n");

System.out.print(" Your choice? ");


Scanner kbReader = new Scanner(System.in);
int choice = kbReader.nextInt();


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

System.out.print("\nEnter first number: ");
float x = kbReader.nextFloat();


System.out.print("Enter second number: ");
float y = kbReader.nextFloat();




System.out.println("");

switch (choice)
{

case 1: //addition 1 decimal
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic