Java实习一

简单的二元一次方程求解

import java.lang.Math;
import java.util.Scanner;
public class Solve{
	public static void main(String args[]){
		Scanner reader = new Scanner(System.in);
		int a = reader.nextInt();
		int b = reader.nextInt();
		int c = reader.nextInt();
		double ans1 = 0.0, ans2 = 0.0;
		double det = b * b - 4 * a * c;
		if(a == 0 && b != 0){
			System.out.println("该方程只有一个根:" + -1*c/b);
			return ;
		}
		if(a == 0 && b == 0){
			System.out.println("没有解");
			return ;
		}		
		if(det < 0){
			System.out.println("没有解");
			return ;
		}
		else if(det == 0){
			ans1 = -1*b/(2*a);
			System.out.println("该方程只有一个根:" + ans1);
		}else{
			det = Math.sqrt(det);
			ans1 = -1*b + det;
			ans1 /= 2.0*a;
			ans2 = -1*b - det;
			ans2 /= 2.0*a;
			System.out.println("该方程有且仅有两个:");
			System.out.println("第一个根为:" + ans1);
			System.out.println("第二个根为:" + ans2);
		}
	}
	
}

简单的学生类

package student;

import java.lang.String;
import java.util.Scanner;

class SchoolStudent{
	String name;
	int grade;	
	void set(String name, int grade){
		this.name = name;
		this.grade = grade;
	}
	SchoolStudent get(SchoolStudent st){
		st.name = this.name;
		st.grade = this.grade;
		return st;
	}
	SchoolStudent(String name, int grade){
		this.name = name;
		this.grade = grade;
	}
	SchoolStudent(){
		this.name = "";
		this.grade = 0;
	}
}
	public class Student{
		public static void main(String args[]){
			while(true){
				Scanner reader = new Scanner(System.in);
				System.out.println("姓名:");
				String name = reader.nextLine();
				System.out.println("成绩:");
				int grade = reader.nextInt();
				SchoolStudent tom = new SchoolStudent(name,grade);
				System.out.println("学生姓名:" + tom.name);
				System.out.println("成绩:" + tom.grade);	
			}		
		}	
}

五子棋

五子棋文件

import java.util.Scanner;

public class wuziqi
{
    static int maxn = 16;
    private static char[][] map = new char[maxn][maxn];
    private static char[] baihei= {'○','●'};
    private static int[] tox = {-1,0,-1,-1,1,1,0,1};
    private static int[] toy = {0,-1,-1,1,-1,1,1,0};

    public static void init()
    {
        for(int i=1; i<maxn; i++)
            for(int j=1; j<maxn; j++)
                map[i][j] = '+';
    }
    public static void paint()
    {
        for(int i=1; i<maxn; i++)
        {
            for(int j=1; j<maxn; j++)
            {
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
    public static boolean isLegal(int i, int j)
    {
        if(i>15 || i<1 || j>15 || j<1)
        {
            return false;
        }
        return true;
    }

    private static int isWin(int p,int x,int y)
    {
        for(int i=0; i<4; i++)
        {
            int sum = 1;
            int xx = x+tox[i];
            int yy = y+toy[i];
            while(map[xx][yy]==baihei[p%2] && isLegal(xx,yy))
            {
                sum++;
                xx = xx+tox[i];
                yy = yy+toy[i];
            }
            xx = x+tox[7-i];
            yy = y+toy[7-i];
            while(map[xx][yy]==baihei[p%2] && isLegal(xx,yy))
            {
                sum++;
                xx = xx+tox[7-i];
                yy = yy+toy[7-i];
            }
            if(sum>=5)
                return 1;
        }
        return 0;
    }
    public static void main(String[] args)
    {
        init();
        int cnt = -1; //白先
        while(true)
        {
            System.out.println("请输入落棋的坐标,以X Y的形式:");
            Scanner reader = new Scanner(System.in);
            int x1 = reader.nextInt();
            int y1 = reader.nextInt();
            while(!isLegal(x1,y1) || map[x1][y1] != '+')
            {
                System.out.println("请您重新输入落棋的坐标:");
                reader = new Scanner(System.in);
                x1 = reader.nextInt();
                y1 = reader.nextInt();
            }
            cnt++;
            map[x1][y1] = baihei[cnt%2];
            paint();
            if(isWin(cnt,x1,y1)==1)
            {
                System.out.println("恭喜您赢了");
                break;
            }
            if(cnt == 15*15-1)
            {
                System.out.println("平局");
                break;
            }
        }
    }
}
posted @ 2017-12-02 23:24  pprp  阅读(241)  评论(0编辑  收藏  举报