[英]How do I get my dialog box to show the output of my program?
最近,我們在課堂上翻閱了對話框,並分配了作業。
任務是設計和實現一個應用程序,該應用程序使用對話框獲取兩個整數值(每個值一個對話框),並顯示值的總和和乘積。 使用另一個對話框詢問用戶是否要處理另一對值。
到目前為止,我嘗試執行該項目,但是在顯示我的答案的一行代碼中遇到了問題。 我正在使用NetBeans IDE,並且使用了顯示方法JOptionPane.showConfirmDialog
來顯示我的答案。 它一直給我一個錯誤,提示“找不到適合showMessageDialog
方法”。 我嘗試使用System.out.println
但它也給我帶來了錯誤,因此我返回了該方法。 您能解釋一下如何解決它,以及為什么我的代碼是錯誤的嗎?
這是我到目前為止的代碼:
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);
}
}
您可以使用JOptionPane.showMessageDialog(parentComponent, message);
方法
您需要創建一個具有乘積和求和的String
,如下所示,並將其傳遞給showMessageDialog
JOptionPane.showMessageDialog(null, answerSum + " " + answerPro);
請嘗試一下。 快樂的編碼。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.