gpt4 book ai didi

java - 我的程序需要帮助

转载 作者:行者123 更新时间:2023-12-02 00:48:05 26 4
gpt4 key购买 nike

我在 Java 中有这个程序 import java.util.*;

public class Euclid {

private static final String EXCEPTION_MSG =
"Invalid value (%d); only positive integers are allowed. ";

public static int getGcd( int a, int b)
{
if (a < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, a));
}
else
if (b < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, b));
}

while (b != 0)
{


if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
}
<小时/>
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class EuclidGui
{
private static final String PROMPT_A = "#1";
private static final String PROMPT_B = "#2";
private static final String BUTTON_TEXT = "Get GCD >>";
private static final String EXCEPTION_TITLE = "Input Exception";
private static final String INSTRUCTIONS = "Type to integer and press 'Get GCD'";
private static final String DIALOG_TITLE = "Euclid's Algorithm";
private static final int FIELD_WIDTH = 6;

public static void main (String[] args)
{
final JTextField valueA = new JTextField (FIELD_WIDTH);
final JTextField valueB = new JTextField (FIELD_WIDTH);
final JTextField valueGcd = new JTextField (FIELD_WIDTH);
JLabel labelA = new JLabel(PROMPT_A);
JLabel labelB = new JLabel(PROMPT_B);
JButton computeButton = new JButton(BUTTON_TEXT);
Object[] options = new Object[] {labelA, valueA, labelB, valueB, computeButton, valueGcd};
valueGcd.setEditable (false);
computeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{ try
{
int a = Integer.parseInt(valueA.getText());
int b = Integer.parseInt(valueB.getText());
int gcd = Euclid.getGcd(a , b);

valueGcd.setText(Integer.toString(gcd));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), EXCEPTION_TITLE, JOptionPane.ERROR_MESSAGE);

}
}
});
JOptionPane.showOptionDialog(null, INSTRUCTIONS, DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, null);
}
}

我想在上面添加计算时间代码,但我不知道如何添加,以及我使用的正确代码是什么。如果有人有想法,请帮助我。

最佳答案

System.currentTimeMillis()就可以了:

long start = System.currentTimeMillis();
// do stuff
long timeTaken = System.currentTimeMillis() - start;

关于java - 我的程序需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com