常见异常
import java.util.Scanner;
import java.lang.*;
import static java.lang.System.exit;
public class Main {
public static void main(String[]args)
{
Scanner scanner=new Scanner(System.in);
int []arr=new int[5];
while (true){
String s=scanner.next();
if(s.equals("arr")){
int n=scanner.nextInt();
try{
arr[n]=0;
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
}
else if(s.equals("null")){
try{
int b[]=null;
b[0]=0;
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
else if(s.equals("cast")){
Object s1=new String("s");
try{
Integer n1=(Integer)(s1);
}
catch (ClassCastException e)
{
System.out.println(e);
}
}
else if(s.equals("num")){
String s2=scanner.next();
try {
int n2=Integer.parseInt(s2);
}
catch (NumberFormatException e){
System.out.println(e);
}
}
else exit(0);
}
}
}
三子棋
import javax.swing.*;
import java.util.Random;
public class ThreeChess {
public static boolean judge(String m)
{
if(m.equals("*")||m.equals("o")){
return false;
}
else return true;
}
public static void com(String a[])
{
//电脑输入
Random random=new Random();
int loc_c= random.nextInt(8);
if(judge(a[loc_c]))
a[loc_c]="o";
else
{
while (!judge(a[loc_c]))
loc_c= random.nextInt(8);
a[loc_c]="o";
}
}
public static boolean end(String a[])
{
if(a[0].equals(a[1])&&a[1].equals(a[2])||a[3].equals(a[4])&&a[4].equals(a[5])||a[6].equals(a[7])&&a[7].equals(a[8])
||a[0].equals(a[3])&&a[3].equals(a[6])||a[1].equals(a[4])&&a[4].equals(a[7])||a[2].equals(a[5])&&a[5].equals(a[8])
||a[0].equals(a[4])&&a[4].equals(a[8])||a[2].equals(a[4])&&a[4].equals(a[6]))
return true;
else return false;
}
public static boolean fair(String a[])
{
int sum=0;
for(int i=0;i<9;i++)
{
if(a[i].equals("*")||a[i].equals("o"))
{
sum++;
}
else {
sum=sum;
}
}
if(sum==9)
return true;
else return false;
}
public static void main(String[]args){
String a[]=new String[9];
for(int i=0;i<9;i++)
a[i]=i+1+"";
String mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
String user="*";
String computer="o";
String m;
int loc_u;
while(true)
{
//用户输入
m=JOptionPane.showInputDialog(null,mess+"请输入位置");
loc_u=Integer.parseInt(m);
if(judge(a[loc_u-1]))
{
a[loc_u-1]="*";
if(end(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"恭喜你赢了!");
break;
}
else {
if(fair(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"平局了");
break;
}
else
{
com(a);
if(end(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"呀!你输了");
break;
}
else
{
if(fair(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"平局了");
break;
}
else
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
continue;
}
}
}
}
}
else
{
JOptionPane.showMessageDialog(null, "此处不可放,请重新输入", "错误",JOptionPane.ERROR_MESSAGE);
while (!judge(a[loc_u-1]))
{
m=JOptionPane.showInputDialog(null,mess+"请输入位置");
loc_u=Integer.parseInt(m);
}
a[loc_u-1]="*";
if(end(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"恭喜你赢了!");
break;
}
else
{
if(fair(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"平局了");
break;
}
else
{
com(a);
if(end(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"呀!你输了");
break;
}
else
{
if(fair(a))
{
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
JOptionPane.showMessageDialog(null,mess+"平局了");
break;
}
else {
mess="-------------------------------------\n"+
"| "+a[0]+" | "+a[1]+" | "+a[2]+" |\n"+
"| "+a[3]+" | "+a[4]+" | "+a[5]+" |\n"+
"| "+a[6]+" | "+a[7]+" | "+a[8]+" |\n"+
"-------------------------------------\n";
continue;
}
}
}
}
}
}
}
}
函数图像绘制
import javax.swing.*;
import java.awt.*;
class MyJFram extends JFrame{
public MyJFram(){
JFrame frame=new JFrame();
c_jpanel f=new c_jpanel();
add(f);
}
}
class c_jpanel extends JPanel{
public c_jpanel() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x[]=new int[41];
int y[]=new int[41];
for(int i=280;i<=320;i++)
{
x[i-280]=(int)(i);
y[i-280]=(200-((x[i-280]-300)*(x[i-280]-300)));
}
g.drawPolyline(x,y,40);//绘制关联线
g.drawLine(0,200,600,200);
g.drawLine(600,200,590,210);
g.drawLine(600,200,590,190);
g.drawLine(300,0,300,400);
g.drawLine(300,0,290,10);
g.drawLine(300,0,310,10);
g.drawString("x",600,220);
g.drawString("Y",340,20);
}
}
public class Fx_image {
public static void main(String[]args){
MyJFram f=new MyJFram();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(630,400);
}
}