# The first job of Java technology

Source: Internet
Author: User
Tags stub

(a) Learning summary

How to use 1.Scanner class data
Need to import Java.util.Scanner
Each type of data has a corresponding input method:
Note that the string type, Next () method hit the space or enter the end of the input, if you need to enter a whole line, including spaces, should be used nextline ();
Char type, no input method;

package java练习;import java.util.Scanner;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        int x=in.nextInt();  // 10        System.out.println(x);   //10        double y=in.nextDouble();  //2.2        System.out.println(y);  //2.2        float z=in.nextFloat();  //1.2        System.out.println(z);  //1.2        String a=in.nextLine(); //hello world        System.out.println(a);  //hello world        String b=in.next(); //hello world        System.out.println(b); //hello    }}

The random () method of the 2.Random class and the math class
Java.lang.math,math.random () returns a double with a positive number that is greater than or equal to 0.0 and less than 1.0;
If you want to produce an integer between 20~100, you should

package java练习;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        int x=(int)Math.random()*20+80;        System.out.println(x); //80    }}

If you use the random class, the resulting integer between 20~100 should be

package java练习;import java.util.Random;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Random rand = new Random();         int x=rand.nextInt(20)+80;         System.out.println(x); //93    }}

If a double type random number is produced, the nextdouble (x) is used, and the parameter x is the upper limit, and the range is 0~x;
3. Correct the following procedures

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 output structure of this program is "not equal to 0.3" because the floating-point type has a minimal error when operating.
Modify method One: Use the BigDecimal class
You can use the Equals method and the CompareTo method
Difference: For the size comparison of BigDecimal, the Equals method will not only compare the size of the value, but also compare the accuracy of the two objects, while the CompareTo method will not compare accuracy, only compare the size of the numerical value. The return value of equals is Boolean, and the return value of CompareTo is int, returning 0 for equality;

package java练习;import java.math.BigDecimal;import java.math.MathContext;public class S1 {       public static 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");            }        }      }

Result: "Equals 0.3";
Note: Using the Round method to determine the accuracy range, you need to use the Mathcontext class.
When defining new BigDecimal ("0.3"), constructed with the string type, see the following example with the int type:

package java练习;import java.math.BigDecimal;import java.math.MathContext;public class S1 {       public static void main(String args[]) {            BigDecimal a = new BigDecimal(0.3);           BigDecimal b = new BigDecimal("0.3");           System.out.println(a);           System.out.println(b);        }      }

Results:

Modify method Two:

package java练习;import java.math.BigDecimal;import java.math.MathContext;public class S1 {        public static void main(String args[]) {             double a = 0.1;            double b = 0.1;            double c = 0.1;            if((a + b + c) -0.3<1e-6){                System.out.println("等于0.3");            }else {                System.out.println("不等于0.3");            }        }         }
(ii) Experimental summary

1. Guessing games
Program Design Ideas:
1. Generate Random Price
2. Enter the guessed price
3. Compare two prices, if wrong and the opportunity is not exhausted, back to 2
4. Enter whether or not to continue, if continue, go back to 1;
2. Perpetual calendar
Program Design Ideas:
1. Enter year, month
2. Call Yeardays to find the number of days between 1990 and year
3. Call Monthdays the number of days between 1 and moth for the entire month
4. The total number of days%7 the remainder to determine the number of weeks 1th
3. Mutual assessment Results
Program Design Ideas:
1. Designing array-valued functions
2. In the averaging function, call the value function, sum each element and subtract the maximum value for averaging
3. Call the sort function order, output the score
Note: When outputting tabs, double quotation marks should be used

(c) Code hosting

# The first job of Java technology

Related Article

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.