Question

Hello i was wondering how i get this message dialog box to work. Keeps saying i...

Hello i was wondering how i get this message dialog box to work. Keeps saying i cant convert from string to void or something.

Here is my program:

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

public class Main
{
   public static void main(String[] args)
   {
       int num1 = 0;
       int num2 = 0;
       int answer = 0;
       int userInput = 0;
       Scanner sc = new Scanner(System.in)
   Object object = new JFrame();       
       String msg = JOptionPane.showMessageDialog(object, "Hello, this is a program where you can choose to add, subtract, multiply or divide two numbers.");
       //System.out.println("Hello, this is a program where you can choose to add, subtract, multiply or divide two numbers.");
      
       while(true)
       {
          
           System.out.println("Would you like to add, subtract, multiply, or divide?");
           System.out.println("1 for Add || 2 for Subtract || 3 for Multiply || 4 for Division || 5 to Exit.");
           userInput = sc.nextInt();
      
      
           if(userInput == 1)
           {
          
               add(num1, num2, sc, answer);
          
           }
      
           if(userInput == 2)
           {
          
               subtract(num1, num2, sc, answer);
          
           }
      
           if(userInput == 3)
           {
          
               multiply(num1, num2, sc, answer);
          
           }
      
           if(userInput == 4)
           {
          
               divide(num1, num2, sc, answer);
          
           }
      
           if(userInput == 5)
           {
          
               System.out.println("Thank you! Have a nice day.");
               break;
          
           }
  
           }
  
       }

   public static void add(int num1, int num2, Scanner sc, int answer)
   {
       System.out.println("Please input your first number to add.");
       num1 = sc.nextInt();
      
       System.out.println("Please input your second number to add.");
       num2 = sc.nextInt();
      
       answer = num1 + num2;
       System.out.println("Your answer is " + answer);
   }
  
   public static void subtract(int num1, int num2, Scanner sc, int answer)
   {
      
       System.out.println("Please input your first number to subtract.");
       num1 = sc.nextInt();
      
       System.out.println("Please input your second number to subtract.");
       num2 = sc.nextInt();
      
       answer = num1 - num2;
       System.out.println("Your answer is " + answer);
      
   }
  
   public static void multiply(int num1, int num2, Scanner sc, int answer)
   {
       System.out.println("Please input your first number to multiply.");
       num1 = sc.nextInt();
      
       System.out.println("Please input your second number to multiply.");
       num2 = sc.nextInt();
      
       answer = num1 * num2;
       System.out.println("Your answer is " + answer);
   }
  
   public static void divide(double num1, double num2, Scanner sc, double answer)
   {
      
       System.out.println("Please input your first number to divide.");
       num1 = sc.nextDouble();
      
       System.out.println("Please input your second number to divide.");
       num2 = sc.nextDouble();
      
       answer = num1 / num2;
       System.out.println("Your answer is " + answer);
      
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import javax.swing.*;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        int num1 = 0;
        int num2 = 0;
        int answer = 0;
        int userInput = 0;
        Scanner sc = new Scanner(System.in);
        JOptionPane.showMessageDialog(null, "Hello, this is a program where you can choose to add, subtract, multiply or divide two numbers.");
        //System.out.println("Hello, this is a program where you can choose to add, subtract, multiply or divide two numbers.");

        while(true)
        {

            System.out.println("Would you like to add, subtract, multiply, or divide?");
            System.out.println("1 for Add || 2 for Subtract || 3 for Multiply || 4 for Division || 5 to Exit.");
            userInput = sc.nextInt();


            if(userInput == 1)
            {

                add(num1, num2, sc, answer);

            }

            if(userInput == 2)
            {

                subtract(num1, num2, sc, answer);

            }

            if(userInput == 3)
            {

                multiply(num1, num2, sc, answer);

            }

            if(userInput == 4)
            {

                divide(num1, num2, sc, answer);

            }

            if(userInput == 5)
            {

                System.out.println("Thank you! Have a nice day.");
                break;

            }

        }

    }

    public static void add(int num1, int num2, Scanner sc, int answer)
    {
        System.out.println("Please input your first number to add.");
        num1 = sc.nextInt();

        System.out.println("Please input your second number to add.");
        num2 = sc.nextInt();

        answer = num1 + num2;
        System.out.println("Your answer is " + answer);
    }

    public static void subtract(int num1, int num2, Scanner sc, int answer)
    {

        System.out.println("Please input your first number to subtract.");
        num1 = sc.nextInt();

        System.out.println("Please input your second number to subtract.");
        num2 = sc.nextInt();

        answer = num1 - num2;
        System.out.println("Your answer is " + answer);

    }

    public static void multiply(int num1, int num2, Scanner sc, int answer)
    {
        System.out.println("Please input your first number to multiply.");
        num1 = sc.nextInt();

        System.out.println("Please input your second number to multiply.");
        num2 = sc.nextInt();

        answer = num1 * num2;
        System.out.println("Your answer is " + answer);
    }

    public static void divide(double num1, double num2, Scanner sc, double answer)
    {

        System.out.println("Please input your first number to divide.");
        num1 = sc.nextDouble();

        System.out.println("Please input your second number to divide.");
        num2 = sc.nextDouble();

        answer = num1 / num2;
        System.out.println("Your answer is " + answer);

    }

}
Add a comment
Know the answer?
Add Answer to:
Hello i was wondering how i get this message dialog box to work. Keeps saying i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT