第一次博客作业
一、前言
知识点考到了输入输出,题量多,难度依次递增
二、设计与分析
题目集1的7-8
import java.util.Scanner; public class work1 { public static void main(String[] args) { Scanner x=new Scanner(System.in); double a,b,c,temp; a=x.nextFloat();b=x.nextFloat();c=x.nextFloat(); if(a>b){ temp=a;a=b;b=temp; } if(a>c){ temp=a;a=c;c=temp; } if(b>c){ temp=b;b=c;c=temp; } if(a>=1&&a<=200&&b>=1&&b<=200&&c>=1&&c<=200){ if(a+b>c){ if(a==b&&b==c) System.out.println("Equilateral triangle"); else if(a*a+b*b-c*c==0&&a==b) System.out.println("Isosceles right-angled triangle"); else if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a)) System.out.println("Isosceles triangle"); else if(a*a+b*b-c*c==0) System.out.println("Right-angled triangle"); else System.out.println("General triangle"); } else System.out.println("Not a triangle"); } else System.out.println("Wrong Format"); } }
60.2 60.2 80.56
Isosceles triangle
大体思路
1、先确定出三角形最长的边,假定为c
2、判断输入是否合法,合法区间为【1,200】
3、判断三条边能否构成一个三角形,嵌套一个if即可
4、写出等边三角形,等腰直角三角形,等腰三角形,直角三角形,普通三角形的判断条件
容易出错的地方
1、三角形的边应初始化为浮点数类型而非整数型。
2、确定三角形中最长的边要用temp作为一个踏板,而不是直接交换。
3、if的嵌套注意括号的位置,这里要把逻辑理清楚,优先判断哪个条件,需要思考一下。
题目集2的7-4
import java.util.Scanner; class work1 { //主函数 public static void main(String[] args) { Scanner x = new Scanner(System.in); int year = x.nextInt(); int month = x.nextInt(); int day = x.nextInt(); nextDate(year,month,day); } //判断year是否为闰年,返回boolean类型 public static boolean isLeapYear(int year) { boolean isLeapYear; isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0; return isLeapYear; } //判断输入日期是否合法,返回布尔值 public static boolean checkInputValidity(int year,int month,int day) { boolean checkInputValidity; int[] a=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; if(!isLeapYear(year)) a[2] = 28; checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0); return checkInputValidity; } //求输入日期的下一天 public static void nextDate(int year,int month,int day) { int[] a=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; int d=0,m=0; if(!isLeapYear(year))//如果不是闰年 a[2] = 28; if(checkInputValidity(year,month,day)) {//如果输入的数字合法 if(month==12) {//如果是12月 if(day==a[month]) {//如果是12月的最后一天 year = year+1; m = 1; d=1; } else{//如果不是12月的最后一天 m=month; d =day +1; } } else {//如果不是12月 if(day==a[month]) {//如果是该月的最后一天 m = month + 1; d = 1; } else{//如果不是该月的最后一天 m=month; d = day+1; } } System.out.println("Next date is:"+year+"-"+m+"-"+d); } else//如果输入的数字非法 System.out.println("Wrong Format"); } }
2025 2 10
Wrong Format
大体思路
本题要求给了4个方法,按要求写下来即可
1、主方法里写输入和求出下一天的方法
2、判断闰年的条件
3、判断输入合法性,根据题目条件年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] ,要注意先判断是否为闰年。
4、求输入日期的下一天,先判断是否为闰年,再判段输入合法性,然后嵌套一个if,判断是否为12月,再判断是不是该月的最后一天。
容易出错的地方
1、闰年的判断条件不只是能整除4
2、定义一个数组的时候一开始有问题,后来查了资料才发现是这样的
3、判断输入日期,没有先判断闰年,导致最后有错误
4、判断日期前要先判断输入合法性
题目集2的7-5
import java.util.Scanner; public class work1{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean f=true; int y=0,m=0,d=0,n=0; if (sc.hasNextInt()) y= sc.nextInt(); else f=false; if (sc.hasNextInt()) m= sc.nextInt(); else f=false; if (sc.hasNextInt()) d= sc.nextInt(); else f=false; if (sc.hasNextInt()) n= sc.nextInt(); else f=false; if (checkInputValidity(y,m,d,n)&&f){ nextDate(y,m,d,n); }else System.out.println("Wrong Format"); } public static boolean isLeapYear(int year) { boolean ret=false; if ((year%100!=0&&year%4==0)||(year%400==0)){ ret=true; } return ret; } public static boolean checkInputValidity(int year,int month,int day,int n){ boolean ret=false; int[] mm={31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year))mm[1]=29; if ((year>=1820&&year<=2020)&&(month>=1&&month<=12)&&(day>=1&&day<=mm[month-1])){//此处的month-1用来判断不同年份的不同月份的数据是否合法 ret=true; } return ret; } public static void nextDate(int year,int month,int day,int n) { int[] mm={0,31,28,31,30,31,30,31,31,30,31,30,31}; if (isLeapYear(year))mm[2]=29; day-=n; if (n>0){ while (day<=0){ //这边是读错题写的while循环,实际上n在-10到10之间,不需要循环,但是上面的判断n的条件我没有加, //也就是可以判断大于10和小于-10范围以外的n,但是没有详细去测试过,单这道题的测试点来讲是没有问题的 month--; if (month==0){//这边因为是mm有0的索引,因此要提前判断是否等于0 month+=12; year--; } day+=mm[month]; } }else if (n<0){ while (day>mm[month]) { day-=mm[month]; month++; if (month==13){ month-=12; year++; } } } System.out.printf("%d days ago is:%d-%d-%d\n",n,year,month,day); } }
2018 6 19 -8 -8 days ago is:2018-6-27
大体思路
1、先判断输入是否为整数
2、其他同题目集2的7-4
容易出错的地方
同题目集2的7-4
题目集3的7-2
import java.util.Scanner; public class work1{ public static void main(String[]args) { Scanner in=new Scanner(System.in); Date a=new Date(); a.year=in.nextInt(); a.month=in.nextInt(); a.day=in.nextInt(); a.getNextDate(a.getYear(),a.getMonth(),a.getDay()); } } class Date { int year; int month; int day; int[] maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; public Date(int year,int month,int day) { this.year=year; this.month=month; this.day=day; } public Date() { int year,month,day; } public int getYear() { return year; } public void setYear(int year) { this.year=year; } public int getMonth() { return month; } public void setMonth(int month) { this.month=month; } public int getDay() { return day; } public void setDay(int day) { this.day=day; } public boolean isLeapYear(int year) { boolean isLeapYear; isLeapYear=(year % 4 == 0 && year % 100 !=0 ||year % 400 == 0); return isLeapYear; } public boolean checkInputValidity(int year,int month,int day) { boolean checkInputValidity; int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear(year)) { a[2]=29; } checkInputValidity = (year>=1900&&year<=2000&&month>0&&month<=12&&day<=a[month]&&day>0); return checkInputValidity; } public void getNextDate(int year,int month,int day) { int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; int d=0,m=0; if(isLeapYear(year)) { a[2]=29; } if(checkInputValidity(year,month,day)) { if(month==12) { if(day==a[month]) {//如果是12月的最后一天 year = year+1; m = 1; d=1; } else{//如果不是12月的最后一天 m=month; d =day +1; } } else {//如果不是12月 if(day==a[month]) {//如果是该月的最后一天 m = month + 1; d = 1; } else{//如果不是该月的最后一天 m=month; d = day+1; } } System.out.println("Next day is:"+year+"-"+m+"-"+d); } else if(checkInputValidity(year,month,day)==false)//如果输入的数字非法 System.out.println("Date Format is Wrong"); } }
1912 12 25
Next day is:1912-12-26
大体思路
1、main方法里new一个date类的对象,和年月日的输入
2、定义date类
1)get set方法,这里不作详细赘述
2)判断闰年
3)判断输入合法性
4)得到下一天的日期
容易出错的地方
1、闰年的判断条件不只是能整除4
2、定义一个数组的时候一开始有问题,后来查了资料才发现是这样的
3、判断输入日期,没有先判断闰年,导致最后有错误
4、判断日期前要先判断输入合法性
题目集3的7-3
import java.math.BigInteger; import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; public class work1 { public static void main(String[] args){ Scanner in=new Scanner(System.in); String temp=in.nextLine(); DvForString dv=new DvForString(); dv.setPolynthic(temp);//预处理输入的字符串 dv.print(); //根据合法性作出不同输出 // System.out.println(); // dv.printEveryElement(); } } class DvForString{ private static LinkedList<String> item;//存储各项,此题并不需要 private static String sign,number,regex,regex1,regex2,regex3,concat,concatend,specialnum,end;//各项正则 //静态区,字符串 static { //静态区只在类被加载时被执行一次 item=new LinkedList<>(); sign="(?:[+|-])"; number="(?:([1-9](\\s*\\d*\\s*)*))"; //全是常数项时的特判 specialnum="((?:(\\s*([+|-]?[1-9]+\\s*\\d*)\\s*))*)"; //带x项,允许系数中间内有空格 //有不为0的系数和指数 regex="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; //x只有指数 regex1="(?:(x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))"; //x只有系数 regex2="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x))"; //x前面系数,指数都没有 regex3="(?:x)"; concat="("+regex+"|"+regex1+"|"+regex2+"|"+regex3+")"; //数字和带x项或在一起构成多项式中的一项 concatend="("+concat+"|"+number+")"; //多项式表达式 ,首项可以无+,-, 后续项必须有 end="(?:("+"\\s*"+sign+"?\\s*"+concatend+"(?:\\s*"+sign+"\\s*"+concatend+"\\s*)*"+"))"; } //Polynthic 多项式 private String Polynthic=null; public LinkedList<String> getItem() { return item; } //无参构造 public DvForString(){} //有参构造,可以直接通过字符串数组名赋值另一个相同大小和类型的数组 public DvForString(String polynthic) { this.Polynthic = polynthic; } //对私有变量获取和,赋值的方法 public String getPolynthic() { return Polynthic; } public void setPolynthic(String polynthic) { this.Polynthic = polynthic; } //合法性检验 private boolean isLegal() { boolean flag=true; String s=Polynthic; //全常数检验 if(s.matches(specialnum)) return true; //复杂多项式检验 if(!s.matches(end)) flag=false; return flag; } //打印合法输入中每一项 public void printEveryElement() { System.out.println(); if(isLegal()) { for(String e: item) System.out.println(e);} else System.out.println("Wrong Format"); } //打印 public void print() { if(isLegal()) printLegal(); else printNotLegal();//不合法结果打印 } //打印不合法输出 private void printNotLegal() { System.out.println("Wrong Format"); } //打印合法输出 private void printLegal() { //拷贝一份给字符串s String s=Polynthic; s=s.replace(" ",""); char[] t=s.toCharArray(); for(int i=0;i<s.length()-1;i++) if(t[i]=='^'&&t[i+1]=='-') t[i+1]='#'; //直接通过tostring转换字符数组会出错,所以一个个字符拼接 String s1=""; for(int i=0;i<t.length;i++) s1=s1+t[i]; //再来整体替换- s1=s1.replace("^+","^"); s1=s1.replace("-","+-"); //如果+出现在第一个位置会报异常,要用\\+ String [] id=s1.split("\\+"); //加号输出标记 int flag=0; //无项时输出标记,标记若最终为0,最后输出0 int lazy=0; //所有常数直接打印不需要特意放+,带x项,对于大于0项,前面有其他项要打印出来 for(String e :id) { //切开后对每个元素进行分析处理,#要替换成- e=e.replace("#","-"); if(e!=null) item.addLast(e); int start=e.indexOf("x"); int mid=e.indexOf("*"); int end=e.indexOf("^"); //x,^都有 if(start!=-1&&end!=-1) { //系数不为1,存在* if(mid!=-1) { BigInteger pro=new BigInteger(e.substring(0,mid)); BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pro.multiply(pos);//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } lazy=1; if(flag==0) flag=1; } //没有*,系数为负 else { if(e.charAt(0)=='-') { BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pos.multiply(new BigInteger(-1+""));//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } } //没*,系数不为负 else { BigInteger pos=new BigInteger(e.substring(end+1,e.length())); BigInteger xishu=pos.multiply(new BigInteger(1+""));//乘法 BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法 if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu); else { if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu); else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu); else { if( xishu.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(xishu+"*x^"+zhishu); } } } lazy=1; if(flag==0) flag=1; } }//^,x都存在的分界线 //有x,没有指数 else if(start!=-1) { //有* if(mid!=-1) { BigInteger num=new BigInteger(e.substring(0, mid)); // if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(num); } //没有* else { BigInteger num=null; if(e.charAt(0)=='x') num=new BigInteger("1"); else if(e.charAt(0)=='-') num=new BigInteger("-1"); else num=new BigInteger(e.substring(0, start)); // if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+"); System.out.print(num); } lazy=1; if(flag==0) flag=1; } //常数 else System.out.print(""); }//大for结尾 if(lazy==0) System.out.println(0); } }
-2* x^-2+ 5*x^12-4*x+ 12 4*x^-3+60*x^11-4
大体思路
1、有参构造,可以直接通过字符串数组名赋值另一个相同大小和类型的数组
2、无项时输出标记,标记若最终为0,最后输出0
3、所有常数直接打印不需要特意放+,带x项,对于大于0项,前面有其他项要打印出来
容易出错的地方
1、静态区只在类被加载时被执行一次
2、直接通过tostring转换字符数组会出错,所以一个个字符拼接
三、总结
刚开始学习Java,从一开始什么都不懂的菜鸟到现在了解了java的输入输出和c的不同,java是面向对象的,也让我体会到了java的魅力和好处,我知道了类 对象 方法 属性的定义,这对看懂代码是很有必要的。我还需要去多看一些java书上的知识点,我现在懂的还是很少的,争取这学期能提升一下自己写代码的水平。

浙公网安备 33010602011771号