(a) Learning 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.
import java.util.*;public class ABC { public static void main(String[] args) { int a; Scanner input = new Scanner(System.in); a = input.nextInt(); System.out.println(a);}
This is the number of the input int, and if you want to enter a double number, change the int to double after next. If you want to enter a string, next you don't have to write anything.
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.
Math class: int x= (int) (Math.random () *x+y); is to produce a number between X-Y.
Random class:
Random rand = new Random ();
int X=rand.nextint (x) +y;
is to produce a number between X-Y.
3. What is the result of running the following procedure? Check the information and analyze why.
The output is not equal to 0.3 because there is an error when calculating double type data.
When comparing data equal to two double types, see if the subtraction of the two is nearly 0.
public class ABC { 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
Experimental content:
1. Look at the price of goods
Import java.util.*;p Ublic class ABC {public static void main (string[] args) {int x = 0, a = 0, i = 0, j = 0, K = 0, n = 0; String T; Scanner input = new Scanner (system.in); Ww:while (True) {x = (int) (Math.random () * 100); k = 100; for (i = 5; i > 0; i--) {System.out.println ("Please enter the commodity price, and" + i + "chance"); A = Input.nextint (); if (a = = x) {System.out.println ("guessed right"); j + = k; Break } if (a > x) {System.out.println ("guess Big"); K-= 20; } if (a < x) {System.out.println ("guess small"); K-= 20; } if (i = = 1) {System.out.println ("correct answer is" + x); }} n++; System.out.println ("Do you want to proceed to the next round?" Enter Yes to proceed to the next round, enter No to exit the game"); while (true) {T = Input.next (); if (t.equals ("yes")) {System.out.println ("next round"); Break } else if (T.equals ("no")) {System.out.println ("Exit Game"); Break WW; } else {System.out.println ("input error, please re-enter"); }}} System.out.println ("Altogether guessed" + N + "times, must be divided into" + j); }}
2. Perpetual calendar
Import java.util.*;p Ublic class ABC {public static void main (string[] args) {int year, year1, month, sum, sum1 , sum2, I, n, zong = 0, h = 0; 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 >) {System.out.println ("input error, please re-enter"); } else {break; }} sum1 = 0; sum2 = 0; YEAR1 = year; for (year1 = year1-1; year1 >= 1900; year1--) {if (year1% 4 = = 0 && year1%! = 0 | | year1% + = = 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 = = 1 0 | | i = = 12) {sum2 = sum2 + 31; } else if (i = = 4 | | i = = 6 | | i = = 9 | | i = = one) {sum2 = sum2 + 30; } else if (i = = 2) {if (year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) { sum2 = sum2 + 29; } else {sum2 = sum2 + 28; }}} sum = Sum1 + sum2; System.out.println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ Thursday \ t Friday \ t Saturday"); Switch (sum% 7) {case 0:system.out.print ("\ t"); h = 2; Break Case 1:system.out.print ("\t\t"); h = 3; Break Case 2:system.out.print ("\t\t\t"); h = 4; Break Case 3:system.out.print ("\t\t\t\t"); h = 5; Break Case 4:system.out.print ("\t\t\t\t\t"); h = 6; Break Case 5:system.out.print ("\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 = = | | month = = 12) { Zong = 31; } else if (month = = 4 | | month = = 6 | | month = = 9 | | month = =) {zong = 30; } else if (month = = 2) {if (year% 4 = = 0 && year%! = 0 | | year% = = 0) {Zong = 29; } else {zong = 28; }} for (n = 1; n <= Zong; n++, h++) {System.out.print (n + "\ T"); if (h% 7 = = 0) {System.out.println (""); } } }}
3. Scoring system
Import java.util.*;p Ublic class ABC {public static void main (string[] args) {int a[][] = new INT[5][10]; int b[][] = new INT[5][8]; int i = 0, j = 0, k = 0, t = 0; Double n, x, y; Double c[] = new DOUBLE[5]; int d[] = new INT[5]; Scanner input = new Scanner (system.in); for (i = 0; I <= 4, i++) {for (j = 0; J <= 9; j + +) {System.out.println ("please" + (j + 1) + "The judges to the" + (i + 1) + "Number of players score"); A[I][J] = Input.nextint (); }} for (i = 0; I <= 4, i++) {for (j = 0; J <= 9; j + +) {for (k = 0; K < = 8; k++) {if (A[i][k] > a[i][k + 1]) {t = a[i][k]; A[i][k] = a[i][k + 1]; A[i][k + 1] = t; }}}} for (i = 0, I <= 4; i++) {for (j = 0; J <= 7; j + +) { B[i][j] = a[i][j + 1]; }} for (i = 0; I <= 4; i++) {n = 0; x = 0; for (j = 0; J <= 7; j + +) {n++; x + = B[i][j]; } C[i] = x/n; D[i] = i + 1; } for (i = 0; I <= 4, i++) {for (k = 0; k <= 3; k++) {if (C[k] < C[k + 1]) { y = c[k]; C[k] = c[k + 1]; C[k + 1] = y; t = d[k]; D[k] = d[k + 1]; D[k + 1] = t; }}} for (i = 0; I <= 4; i++) {System.out.println ("+" + (i + 1) + "First name is" + D[i] + "Number of players, score" + c[i]); } }}
Java technology first time job