(a) Study summary:
1. In Java, through the scanner class to complete console input, consult the JDK Help document, scanner class implementation of basic data input method? Can not only use text description, must write code, through specific examples to explain.
package study;import java.util.Scanner;public class Study { public static void main(String[] args) { // TODO Auto-generated method stub int x; Scanner input = new Scanner(System.in); x = input.nextInt(); System.out.println(x); }}
The random () method of the 2.Random class and the math class can produce a stochastic number, what is the difference between these two ways, and what are the characteristics of each? Consult the JDK Help documentation for an example.
(1): The Math class's random () can only return a double value between 0-1 if 20-30 needs to be enlarged by 10 times times to 0-10, followed by 10, and cast to an int value.
(2): The random class is the related method of working random class, there are many types of random value acquisition methods, such as Nextboolean (), Nextint (), etc.
3.3. What is the result of running the following procedure? Check the information and analyze why.
The result is not equal to 0.3.
The reason is that the computer generates errors when computing double type data.
(ii) Experimental summary:
1. Look at the price of goods
Package Study;import Java.util.random;import Java.util.scanner;public class Study {public static void main (string[] Ar GS) {//TODO auto-generated method stub int count,score,i; String C=null; Scanner input=new Scanner (system.in); Do {random arr=new random (); int arr1 = arr.nextint (100) +1; Count=1; System.out.println ("Please enter the product price, you can guess up to 5 times!") "); int Price=input.nextint (); for (i=0;i<5;i++) {if (PRICE>ARR1) {System.out.println ("Too big, please re-enter!") "); count++; } else if (PRICE<ARR1) {System.out.println ("too small, please re-enter!") "); count++; } else if (PRICE==ARR1) {System.out.println ("Congratulations, correct! "); Break } price=input. Nextint (); if (count==5) {System.out.printf ("5 times have not guessed right, the correct result is%d\n", arr1); Break }} score=100-count*10; System.out.printf ("Total guess%d times, your score is%d", count,score); System.out.println ("Go on to the next round?") Y (y) OrN (n) "); C=input.next (); } while (C.equals ("Y")); }}
2. Perpetual calendar
Package Calender;import Java.util.scanner;public class Calender {public static void main (string[] args) {//To Do auto-generated method stub int i,h=0,n,year,year1,month,zong = 0,sum,sum1,sum2; Scanner input = new Scanner (system.in); while (true) {System.out.println ("Please enter the Year"); Year = Input.nextint (); System.out.println ("Please enter the month"); month = Input.nextint (); if ((Year < 1900) | | (Month < 1) | | (Month > 12)) {System.out.println ("input error, please re-enter"); } else {} sum1=0; sum2=0; Year1=year; for (year1=year1-1;year1>=1900;year1--) {if (year1%4==0&&year1%100!=0| | year1%400==0) {sum1=sum1+366; } else {sum1=sum1+365; }} for (i=1;i<month;i++) {if (i==1| | i==3| | i==5| | i==7| | i==8| |i==10| | i==12) {sum2=sum2+31; } else if (i==4| | i==6| | i==9| | i==11) {sum2=sum2+30; } else if (i==2) {if (year%4==0&&year%100!=0| | year%400==0) {sum2=sum2+29; } else {sum2=sum2+28; }}} sum=sum1+sum2; SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month"); System.out.println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ Thursday \ t Friday \ t Saturday \ n"); Switch (sum%7) {case 0:system.out.printf ("\ t"); h=2;break; Case 1:system.out.printf ("\t\t"); h=3;break; Case 2:system.out.printf ("\t\t\t"); h=4;break; Case 3:system.out.printf ("\t\t\t\t"); h=5;break; Case 4:system.out.printf ("\t\t\t\t\t"); h=6;break; Case 5:system.out.printf ("\t\t\t\t\t\t"); h=7;break; Case 6:h=1;break; } if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| | month==12) {zong=31; } else if (month==4| | month==6| | month==9| | month==11) {zong=30; } else if (month==2) {if (year%4==0&&year%100!=0| | year%400==0) {zong=29; } else {zong=28; }} for (n=1;n<=zong;n++,h++) {System.out.printf ("%2d", N); if (h%7==0) {System.out.printf ("\ n"); }} System.out.printf ("\ n");}}}
JAVA first time job