我在使用此代码时遇到了很多麻烦。在我尝试输入代码以捕获异常之前,代码已编译并运行。然后我再也无法编译了。如果用户输入负数,则假设捕获异常并产生错误消息。
import java.awt.event.ActionEvent; //Next group of lines import various Java classes
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
import java.io.IOException;
public class SquareRoot extends JFrame
{
public static void main(String[] args) {
//Creates Window
JFrame frame = new JFrame();
frame.setSize(450, 300);
frame.setTitle("Find the Square Root");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel Numberlbl = new JLabel("Enter a number:");
final JTextField NumberField = new JTextField(10);
NumberField.setText("");
JLabel Answerlbl = new JLabel("Square Root of your number is:");
final JTextField AnswerField = new JTextField(10);
AnswerField.setText("");
JLabel ButtonLabel = new JLabel("Calculate Square Root");
JButton button = new JButton("Find Square Root");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.add(Numberlbl);
panel.add(NumberField);
panel.add(ButtonLabel);
panel.add(button);
panel.add(Answerlbl);
panel.add(AnswerField);
frame.add(panel);
class CalculateListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double NumberX = Double.parseDouble(NumberField.getText());
try
{
if(NumberX >=0);
}
catch(IOException e)
{
System.out.println("Number can not be negative.");
}
double Answer = Math.sqrt(NumberX);
AnswerField.setText(String.valueOf(Answer));
}
}
ActionListener listener = new CalculateListener();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:2)
你不需要try-catch来检查否定,下面应该就够了。但是在进行格式化时可能需要try-catch。
double noX = 0;
try {
noX = Double.parseDouble(NumberField.getText());
} catch(NumberFormatException e) {
System.out.println("Not a valid number");
}
if(noX < 0) {
System.out.println("Number can not be negative.");
}
答案 1 :(得分:0)
更好用:
double NumberX = Double.parseDouble(NumberField.getText());
if(NumberX <=0){
System.out.println("Number can not be negative.");
}
double Answer = Math.sqrt(NumberX);
AnswerField.setText(String.valueOf(Answer));
答案 2 :(得分:0)
Netbeans说&#34;异常java.io.IOException永远不会在相应的if语句&#34;中抛出。
基本上,如果陈述不会抛出异常。
答案 3 :(得分:0)
抛出自定义异常..
try
{
if(NumberX >=0);
else
throw new Exception("Number Negative");
}
catch(Exception e)
{
System.out.println("Number can not be negative."+e.getMessage());
}
答案 4 :(得分:0)
运行良好。
package com.test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SquareRoot extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// Creates Window
JFrame frame = new JFrame();
frame.setSize(450, 300);
frame.setTitle("Find the Square Root");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel Numberlbl = new JLabel("Enter a number:");
final JTextField NumberField = new JTextField(10);
NumberField.setText("");
JLabel Answerlbl = new JLabel("Square Root of your number is:");
final JTextField AnswerField = new JTextField(10);
AnswerField.setText("");
JLabel ButtonLabel = new JLabel("Calculate Square Root");
JButton button = new JButton("Find Square Root");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(Numberlbl);
panel.add(NumberField);
panel.add(ButtonLabel);
panel.add(button);
panel.add(Answerlbl);
panel.add(AnswerField);
frame.add(panel);
class CalculateListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double NumberX;
try {
NumberX = Double.parseDouble(NumberField.getText());
if (NumberX < 0) {
AnswerField.setText("Please enter positive value.");
}
else if (NumberX >= 0) {
double Answer = Math.sqrt(NumberX);
AnswerField.setText(String.valueOf(Answer));
}
}
catch (NumberFormatException e) {
AnswerField.setText("Incorrect number entered.");
e.printStackTrace();
}
}
}
ActionListener listener = new CalculateListener();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}