我是新手,我有以下代码运行良好,但是当我输入特殊字符(@、%、* 等)时,它必须抛出异常,所以我怎么能做到这一点,有人可以帮我吗?新手程序员
/* 检查值的代码*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CheckVal
{
public static void main(String args[])
{
int i=0;
double x=0;
System.out.println("Enter your angle");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(i);
x=Math.sin(Math.toRadians(i));
System.out.println(x);
if(x>=0 && x<=0.5)
{
ButtonBackground frame = new ButtonBackground("green");
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setResizable(false);
frame.setVisible(true);
}
else{
ButtonBackground frame = new ButtonBackground("red");
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setResizable(false);
frame.setVisible(true);
}
}
}
/按钮背景代码/
import java.awt.*;
import javax.swing.*;
public class ButtonBackground extends JFrame
{
public ButtonBackground(String x)
{
setLayout( new FlowLayout() );
//JButton normal = new JButton(" ");
// add(normal);
if(x.equals("green")) {
JButton test1 = new JButton(" ")
{
@Override
public void paintComponent(Graphics g)
{
g.setColor( Color.GREEN );
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent(g);
}
};
test1.setContentAreaFilled(false);
test1.setBackground(Color.GREEN);
add(test1);
}
else
{
JButton test1 = new JButton(" ")
{
@Override
public void paintComponent(Graphics g)
{
g.setColor( Color.RED );
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent(g);
}
};
test1.setContentAreaFilled(false);
test1.setBackground(Color.RED);
add(test1);
}
}
}