简体   繁体   中英

How do I get my dialog box to show the output of my program?

We recently went over dialog boxes in class and had homework assigned to us.

The assignment was to design and implement an application that uses dialog boxes to get two integer values (one dialog box for each value) and display the sum and product of the values. Use another dialog box to ask whether the user wants to process another pair of values.

So far I attempted to do the project but I am having issue with one line of code where I display my answer. I am using a NetBeans IDE and I used the display method JOptionPane.showConfirmDialog to show my answer. It kept giving me an error saying "no suitable method found for showMessageDialog ." I attempted to use System.out.println but it was giving me errors as well so I went back to the method. Would you be able to explain how to fix it and why my code was wrong?

Here is the code I have so far:

package DialogBoxes;

import javax.swing.JOptionPane;
/**
 *
 * @author Tony
 */

public class SumProduct {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String askNum1, askNum2, answerSum, answerPro;
    int num1, num2, repeat;

    do 
    { 
        askNum1 = JOptionPane.showInputDialog ("Enter your first integer:");
        num1 = Integer.parseInt(askNum1);

        askNum2 = JOptionPane.showInputDialog("Enter your second integer:");
        num2 = Integer.parseInt(askNum2);

        answerSum = "The sum is: " + ((num1 + num2));
        answerPro = "The product is: " + ((num1 * num2));

        JOptionPane.showMessageDialog(null, answerSum, answerPro);

        repeat = JOptionPane.showConfirmDialog(null, "Would you like to test another set of numbers?");           
    }
    while (repeat == JOptionPane.YES_OPTION);
}
}

You can use the JOptionPane.showMessageDialog(parentComponent, message); method

You nee to create a String with product and sum as follows and pass it to showMessageDialog

JOptionPane.showMessageDialog(null, answerSum + "  " + answerPro);

Take a look at the JOptionPane API: https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)

It doesn't look like there's a showMessageDialog method that takes only 3 parameters. You need to use one of the methods provided by the API.

Please try this out. Happy coding.

package com.pearson.nextgen.aggregatedsessionservice.web.rest;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

    public class stacktest {

        public static void main(String[] args) {
            // TODO code application logic here
            String askNum1, askNum2, answerSum, answerPro;
            int num1, num2, repeat;

            do 
            { 
                askNum1 = JOptionPane.showInputDialog ("Enter your first integer:");
                num1 = Integer.parseInt(askNum1);

                askNum2 = JOptionPane.showInputDialog("Enter your second integer:");
                num2 = Integer.parseInt(askNum2);

                answerSum = "The sum is: " + ((num1 + num2));
                answerPro = " The product is: " + ((num1 * num2));

                JFrame frame = new JFrame("TestFrame");
                JOptionPane.showMessageDialog(null, answerSum + answerPro);

                repeat = JOptionPane.showConfirmDialog(null, "Would you like to test another set of numbers?");           
            }
            while (repeat == JOptionPane.YES_OPTION);

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM