(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.
package test;import java.util.Scanner;public class Diyiti { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); String line=input.next(); System.out.println("你所输入的是"+line); }}
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.
The random number generated by the 1.Random class is evenly distributed according to the probability within its maximum value range.
2.math.random () returns only decimals from 0 to 1, if you want 50 to 100, zoom in 50 times times, that is, between 0 and 50, this is still a decimal, and if you want an integer, cast int.
package test;import java.util.Random;public class Dierti { public static void main(String[] args) { Random a=new Random(); int num1=a.nextInt(); double num2=Math.random(); System.out.println("Random()产生的随机数"+num1); System.out.println("Math.random()产生的随机数"+num2); }}
3. What is the result of running the following procedure? Check the information and analyze why.
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"); } } }
In order to deal with the loss of precision, you can use the Java.math.BigDecimal class, consult the JDK help document or the textbook p378 to modify the above procedure.
Run results
Post-Modification Program
package test;import java.math.BigDecimal;import java.math.MathContext;public class xiugai { 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"); } } }
4. The main points of this study other needs to summarize the content: individual, according to their own learning situation, record difficult to grasp or difficult to understand the content, as well as learn the content of the experience. There are still problems, you can also put forward, for students in the blog raised questions, we can actively discuss, peer education.
(ii) Experimental summary
Experimental content:
1. Look at the price of goods
1 generate the price of a commodity with a random number;
2 using the For loop for price comparison;
2. Perpetual calendar
1 First judge whether is a leap year, the number of days of the February peace years of the leap period February;
2 The month of 31 days and 30 days for judging the number of days;
'
Package pi;
Import java.util.*;
public class Choujiang {
public static void Main (string[] args) {
//TODO auto-generated method stub
Scanner In=new Scanner (system.in);
Scanner input=new Scanner (system.in);
Random cai=new random ();//Create a Random object cai
int x,a,b=0,c,huida=0,x=0;
String Str=null;
X=cai.nextint (100) +1;
String an= "n";
do{
jump:for(int i=0;i<=4;i++){ System.out.print("请输入猜的价格"); a=Integer.parseInt(input.next()); b++; if(a>x){ System.out.println("太大了"); } else if(a<x){ System.out.println("太小了"); } else if(a==x){ System.out.println("答对了"); break jump; } } System.out.println("您没有机会了"); System.out.println("您总共猜了"+(b)+"次"); System.out.println("继续请输入n"); an=input.next();}while("n".equals(an));if(b>10){ huida=0;}else if(b>=0&&b<=10){huida=100-10*b;}System.out.println("正确答案:"+x);System.out.println("分数:"+huida);
}
}
2.
Package pi;
Import java.util.*;
public class Wannianli {
public static void main(String[] args) { // TODO Auto-generated method stub int year, month, x,sum=0; Scanner input = new Scanner(System.in); System.out.println("请输入年:"); year = input.nextInt(); System.out.print("请输入月份:"); month = input.nextInt(); int day = 0; int firstday = 0; int days = 0; if(isLeap(year)){ System.out.println(""+year+"是闰年");}else{ System.out.println(""+year+"是平年");}day=days(year,month);System.out.println(year+"年"+month+"月有"+day+"天");printCalender(year,month);
}
public static Boolean isleap (int year) {//Judgment leap Year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; } else return false;}
public static int Day (int year, int month) {//Judge number of days in a month
int days=0;
if (month = = 2) {
if (year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) {
days = 29;
}
else {
days = 28;
}
}
else if (month = = 4 | | month = = 6 | | month = = 9 | | month = = 11) {
days = 30;
}
else {
days = 31;
}
return days;
}
public static void Printcalender (int year, int month) {//Calculate days Gap
int x,sum=0;
int day=0;
for (int i = 1900; i < year; i++) {
if ((i% 4 = = 0 && i% 100! = 0) | | (i% 400 = = 0)) {
Day = day + 366;
} else {
Day = day + 365;
}
}
int beforedays = 0;
for (int i = 1; i < month; i++) {
if (i = = 2) {
if (year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) {
sum + = 29;
}
else {
sum + = 28;
}
}
else {
if (i = = 4 | | i = = 6 | | i = = 9 | | month = = 11) {
sum + = 30;
}
else {
sum + = 31;
}
}
}
int days = 0;
if (month = = 2) {
if (year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) {
days = 29;
}
else {
days = 28;
}
}
else if (month = = 4 | | month = = 6 | | month = = 9 | | month = = 11) {
days = 30;
}
else {
days = 31;
}
System.out.println ("");
int temp =sum% 7;
/* Output */
System.out.println ("Sunday \ t Monday \ t Tuesday \ t Wednesday \ Thursday \ t Friday \ t Saturday");
for (int i = 1; i < temp; i++) {
System.out.print ("\ t");
}
for (int i=1;i<=days;i++) {
if(sum%7==0){ System.out.print(i+"\n"); } else{ System.out.print(i+"\t"); } sum+=1; } System.out.println(""); day=sum+day; System.out.println(""+year+"年"+month+"月距1900年1月1日"+day+"天");}
}
```
Java first time job