Java實習一

來源:互聯網
上載者:User

標籤: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實習一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.