我在這裏新手 我有下面的代碼它很好,但當我給一個特殊字符(@,%,*等)作爲輸入它必須拋出異常,所以我怎麼能要做到這一點可以有人幫助我,因爲上午新手程序員java處理字符異常
/*代碼檢查值*/
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);
}
}
}
/代碼buttonbackground/
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);
}
}
}
請不要張貼代碼牆。取而代之的是一個[合適的例子](http://sscce.org/)。 – Adam