码迷,mamicode.com
首页 > 编程语言 > 详细

Java实习一

时间:2017-12-03 00:27:13      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:string   一个   bool   pre   ext   amp   sum   public   次方   

简单的二元一次方程求解

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;
            }
        }
    }
}

Java实习一

标签:string   一个   bool   pre   ext   amp   sum   public   次方   

原文地址:http://www.cnblogs.com/pprp/p/7956409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!