Java first time job

Source: Internet
Author: User

(a) Learning summary
The 1.Scanner class implements basic data input methods
Scanner uses the delimiter mode to break its input into markup, which by default matches whitespace. You can then use a different next method to convert the resulting token to a different type of value
Importing import Java.util.Scanner;
Use

     Scanner sc = new Scanner(System.in);     int i = sc.nextInt();

Enables the user to import a number from system.in, depending on the data type Nextint () can be written as nextfloat (), nextdouble (), Nextboolean ()
2.Random class
Importing import java.util.Random;

    Random rand = new Random();     

The algorithm implemented by the Random class uses a protected utility method that can provide up to 32 pseudo-randomly generated bits each time it is called
Math class

    int a=(int)Marh.random()

3. Running the program

public class Test {    public static void main(String args[]) {         double a = 0.1;        double b = 0.1;        double c = 0.1;        if((a + b + c) == 0.3){            System.out.println("等于0.3");        }else {            System.out.println("不等于0.3");        }    }     }

The result is not equal to 0.3 because the floating-point type has a very small error when operating
In order to handle the problem of loss of precision, you can use the Java.math.BigDecimal class
The modified program

import java.math.BigDecimal;import java.math.MathContext;public class jisuan {    public static void main(String[] args) {        @SuppressWarnings("unused")        // TODO 自动生成的方法存根                class Test {            public void main(String args[]) {                 BigDecimal a = new BigDecimal(0.1);                BigDecimal b = new BigDecimal(0.1);                BigDecimal c = new BigDecimal(0.1);                if(a.add(b).add(c).round(new MathContext(1)).equals(new BigDecimal("0.3"))){                    System.out.println("等于0.3");                }else {                    System.out.println("不等于0.3");                }            }             }    }}

(ii) Experimental summary
1. Look at the price of goods

import java.util.Random;import java.util.Scanner;public class Price {    public static void main(String[] args) {        // TODO 自动生成的方法存根        int a, x,i;        Random rand = new Random();        a = rand.nextInt(100) + 1;        @SuppressWarnings("resource")        Scanner input = new Scanner(System.in);        System.out.println("商品:手表");        System.out.println("输入你猜的价格:");        x = input.nextInt();        for (i = 1;; i++) {            if (x>a) {                System.out.println("高了");            } else if (x<a) {                System.out.println("低了");            } else {                System.out.println("恭喜你,猜对了!一共猜了"+i+"次");                break;            }                        if(i>9){                System.out.println("商品的价格为"+a);                break;            }            x = input.nextInt();        }    }}

Design ideas:
Use the random class to produce a stochastic number as the price of a commodity, by entering data from the keyboard to represent the guessed price and comparing it to the original price of the commodity if the equivalent jumps out of the loop
2. Perpetual calendar

package perpetual calendar; Import Java.util.scanner;public class Date {private static final Boolean ture = false;        public static void Main (string[] args) {//TODO auto-generated method stub System.out.println ("Please enter year and month");        @SuppressWarnings ("resource") Scanner input=new Scanner (system.in);        int Year=input.nextint ();        int Month=input.nextint ();    Print (Year,month); } public static Boolean isleap (int year) {if (year%4==0&&year%100!=0| |        year%400==0) {return true;        }else{return false;        }} public static int days (int year,int month) {@SuppressWarnings ("unused"), Boolean y=isleap (year); if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| |        MONTH==12) {return 31;            }else if (month==2) {if (y=ture) {return 29;            }else{return 28;        }}else{return 30; }} public static int totaldays (iNT Year,int month) {int sum=0,sum1=0,sum2=0;            for (int i=1900;i<year;i++) {@SuppressWarnings ("unused") Boolean x=isleap (i);            if (x=ture) {sum1=sum1+366;            }else{sum1=sum1+365; }} for (int j=1;j<month;j++) {if (j==1| | j==3| | j==5| | j==7| | j==8| |j==10| |            j==12) {sum2=sum2+31; }else if (j==4| | j==6| | j==9| |            j==11) {sum2=sum2+30;                }else{@SuppressWarnings ("unused") Boolean x=isleap (year);                if (x=ture) {sum2=sum2+29;                }else{sum2=sum2+28;        }}} sum=sum1+sum2;    return sum;        } public static void print (int year,int month) {int sum=totaldays (year,month);        int x=sum%7+1;        System.out.printf ("%d years%d months \ n", year,month); System.out.printf ("Sunday Monday Tuesday Wednesday Thursday   Friday Saturday \ n ");            int t=days (year,month);            for (int y=x;y>0;y--) {System.out.printf ("\ t");                } for (int i=1;i<=t;i++) {System.out.printf ("%d\t", I);                int m=i+x;                if (m%7==0) {System.out.println (); }            }        }   }

Design ideas:
Enter the year and month, first judge is not leap years, 1,3,5,7,8,10,12 month is 31 days, 4,6,9,11 month is 30 days leap February 29 days, Common year 28 days, because the known January 1, 1900 is Monday,
So the calculation of the number of days from 1900.1.1 to 7 to calculate the remainder of this month 1th is the number of weeks, determine the number of days of the month, the output of
3. Scoring system

Package score; Import Java.util.scanner;public class Mark {public static void main (string[] args) {//TODO auto-generated method        stub int mark[] = null;        Mark = new INT[10];        int max = 0, min = 0;        Double ave[] = null;        Ave = new Double[5];            for (int i = 0; i < 5; i++) {System.out.printf ("Please score%d players", I + 1); for (int j = 0; J <; J + +) {@SuppressWarnings ("resource") Scanner input = new Scanner (                system.in);                            MARK[J] = Input.nextint ();            } max = max (Mark);            min = min (mark);            Mark[max] = Mark[min] = 0;        Ave[i] = average (mark, 8);        } System.out.println ("Five players score from high to Low");    print (ave);        } public static int max (int mark[]) {int max = mark[0], t = 0;                for (int j = 0; J <; J + +) {if (Mark[j] > max) {max = mark[j];            t = j; }        }       return t;        public static int min (int mark[]) {int min = mark[0], t = 0;                for (int i = 0; i < i++) {if (Mark[i] < min) {min = mark[i];            t = i;    }} return t;        } public static double average (int mark[], int n) {Double Ave = 0;        int sum = 0;        for (int i = 0; i < mark.length; i++) {sum = sum + mark[i];        Ave = (double) sum/(double) n;    Return Ave; } public static void print (double ave[]) {for (Int. i=0;i<ave.length;i++) for (int j=0;j<ave.length;j+                +) {if (Ave[i]>ave[j]) {double t=ave[i];                AVE[I]=AVE[J];            ave[j]=t;        }} for (int j=0;j<ave.length;j++) {System.out.println (ave[j]); }    }}

Design ideas:
Declare a one-dimensional array, by looping five times to enter the scores of five players, each time you enter a player's score, and find the maximum minimum value, and calculate the average in another array, followed by the order from large to small

(c) Code hosting

Summary: The first time to write Java experiment, is not very familiar with Java content, and C language or there are many different places, many times is in accordance with the C language habit to write, feel the loop array and method of the problem is not very small, but the various Java classes are not very familiar with, The overall sense of Java is more than the C language can achieve, but also more convenient, more accurate.

Java first time job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.