前言:本次博客主要是总结一下有关JAVA课程学习和作业的相关问题与心得。已经上课学习JAVA一个月左右啦,也做了三次大作业了,总体来说,题目还是有点难度的,题量还行,还是能在规定时间内写完地。下面举例具体分析我学会的和遇到的问题吧。
题目集1 7-8
判断三角形类型 (20 分)
输入三角形三条边,判断该三角形为什么类型的三角形。
输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。
输出格式:
(1)如果输入数据非法,则输出“Wrong Format”; (2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”; (3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”; (3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”; (5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”; (6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”; (7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。
输入样例1:
在这里给出一组输入。例如:
50 50 50.0结尾无空行
输出样例1:
在这里给出相应的输出。例如:
Equilateral triangle结尾无空行
输入样例2:
在这里给出一组输入。例如:
60.2 60.2 80.56结尾无空行
输出样例2:
在这里给出相应的输出。例如:
Isosceles triangle结尾无空行
输入样例3:
在这里给出一组输入。例如:
0.5 20.5 80结尾无空行
输出样例3:
在这里给出相应的输出。例如:
Wrong Format结尾无空行
错误1:编译错误
Main.java:29: error: bad operand types for binary operator '&&'
if(a = b && b = c) {
^
first type: double
second type: double
前面定义的是double类型,后面的比较比前面的优先,所以出了这个问题。是优先级的问题,可能受到赋值(=)运算符的影响,以为&的优先级比“==”大,实际上恰好相反。相对于位运算符,恒等运算符优先级大一点。而且,其实这里想表达的也并不是赋值,而是比较是否相等。
错误2:答案错误
勾股定理在这里应表示为:a * a + b * b - c * c < 0.000001,而非:a * a + b * b - c * c =0;
浮点数的运算是需要注意的,具体内容我在Java浮点数的计算与比较_ddqqdq的博客-CSDN博客我的这篇博客里有讲到过,可以跳转去看一下。
源代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
double a = scanner.nextFloat();
double b = scanner.nextFloat();
double c = scanner.nextFloat();
double p;
if(a > b) {
p = a;
a = b;
b = p;
}
if(a > c) {
p = a;
a = c;
c = p;
}
if(b > c) {
p = b;
b = c;
c = p;
}
if((a >= 1 && a <=200)&&(b >= 1 && b <=200)&&(c >= 1 && c <=200)) {
if(a + b > c) {
if(a == b && b == c) {
System.out.print("Equilateral triangle");
}
else if((a * a + b * b - c * c < 0.000001)&& (a == b)) {
System.out.print("Isosceles right-angled triangle");
}
else if((a == b)||(b == c)){
System.out.print("Isosceles triangle");
}
else if(a * a + b * b - c * c < 0.000001) {
System.out.print("Right-angled triangle");
}
else {
System.out.print("General triangle");
}
}
else {
System.out.print("Not a triangle");
}
}
else {
System.out.print("Wrong Format");
}
}
}
题目集2 7-4
求下一天 (30 分)
输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。
要求:Main类中必须含有如下方法,签名如下:
public static void main(String[] args);//主方法 public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值 public static void nextDate(int year,int month,int day) ; //求输入日期的下一天输入格式:在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:输入样例1:
- 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
- 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
在这里给出一组输入。例如:
2020 3 10结尾无空行
输出样例1:在这里给出相应的输出。例如:
Next date is:2020-3-11结尾无空行
输入样例2:在这里给出一组输入。例如:
2025 2 10结尾无空行
输出样例2:在这里给出相应的输出。例如:
Wrong Format结尾无空行
这道题,我一开始的时候并没有想到可以使用数组,我是大概就是一个个地去分析,类似于这样:
public static void nextDate(int year,int month,int day) {
if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10) {
if(day < 31) {
System.out.print("Next data is:" + year + "-" + month + "-" + (day + 1));
}
else {
System.out.print("Next data is:" + year + "-" + (month + 1) + "-" + "1");
}
}
else if(month == 4||month == 6||month == 9||month == 11) {
if(day < 30) {
System.out.print("Next data is:" + year + "-" + month + "-" + (day + 1));
}
else {
System.out.print("Next data is:" + year + "-" + (month + 1) + "-" + "1");
}
}
else if(month == 12) {
if(day < 31) {
System.out.print("Next data is:" + year + "-" + month + "-" + (day + 1));
}
else {
System.out.print("Next data is:" + (year + 1) + "-" + "1" + "-" + "1");
}
}
else if(month == 2) {
if(isLeapYear(year)) {
if(day < 29) {
System.out.print("Next data is:" + year + "-" + month + "-" + (day + 1));
}
else {
System.out.print("Next data is:" + year + "-" + (month + 1) + "-" + "1");
}
}
else {
if(day < 28) {
System.out.print("Next data is:" + year + "-" + month + "-" + (day + 1));
}
else {
System.out.print("Next data is:" + year + "-" + (month + 1) + "-" + "1");
}
}
}
}
得到的结果是:
我认真的看每一个函数,每一个分析,都觉得十分正确,但的确列的很复杂。问了室友,室友说这个用数组协会很简洁明了。我突然就觉得我是神经病。因为这样一个个分析确实容易混淆脑子里的思路,或者掉进逻辑的陷进出不来。苦恼了很久之后,我采用了数组的方法,没想到,一下子就正确了,也进行了进一步的优化,写了一个总的输出语句,看起来就更直接明了了。(当然,如果有大佬能指出我前面单独分析地BUG的话,评论告诉我,我万分感谢!)
源代码:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int year=x.nextInt();
int month=x.nextInt();
int day=x.nextInt();
if(!checkInputValidity(year, month, day))
System.out.println("Wrong Format");
else{
//System.out.printf("Next data is:");
nextDate(year, month, day);
}
}
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;
return checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0);
}
public static void nextDate(int year,int month,int day) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
arr[2] = 29;
int a = 0,b = 0,c = 0;
if(month == 12) {
if(day == arr[month]) {
a = year+1;
b = 1;
c = 1;
}
if(day < arr[month]) {
a = year;
b = month;
c =day +1;
}
}
if(month<12) {
if(day == arr[month]) {
a = year;
b = month + 1;
c = 1;
}
if(day < arr[month]){
a = year;
b = month;
c = day+1;
}
}
System.out.print("Next date is:" + a + "-" + b + "-" + c);
}
}
题目集2 7-5
求前N天 (30 分)
输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
输入格式:
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。
输出格式:输入样例1:
- 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
- 当输入数据合法时,输出“n days ago is:年-月-日”
在这里给出一组输入。例如:
2018 6 19 8结尾无空行
输出样例1:在这里给出相应的输出。例如:
8 days ago is:2018-6-11结尾无空行
输入样例2:在这里给出一组输入。例如:
2018 6 19 -8结尾无空行
输出样例2:在这里给出相应的输出。例如:
-8 days ago is:2018-6-27结尾无空行
这道题,和前面一道题极为相似,吸取了前面的经验,我毫不犹豫的选用了数组方法。但是,就是因为与前一题极为相似,我放低了谨慎,我因为一个函数方法的错误,徘徊犹疑了很久。如下:
if(month == 12) {
a = year + 1;
b = 1;
c = - days - day;
}
else {
a = year;
b = month + 1;
c = - days - day;
}
这是很明显的算法错误,应该改为:
if(month == 12) {
a = year + 1;
b = 1;
c = day - days - arr[month];
}
else {
a = year;
b = month + 1;
c = day - days - arr[month];
}
与上题不同的还有“输入一个取值范围在[-10,10] 之间的整型数n”,所以需要稍微改动非法输入条件,
year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0&&Math.abs(days)<=10&&Math.abs(days)>=0
或
year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0&&days<=10&&days>=-10
源代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int year=x.nextInt();
int month=x.nextInt();
int day=x.nextInt();
int days=x.nextInt();
if(!checkInputValidity(year, month, day, days))
System.out.println("Wrong Format");
else{
isLeapYear(year);
DaysAgo(year, month, day, days);
}
}
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,int days) {
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;
return checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0&&Math.abs(days)<=10&&Math.abs(days)>=0);
}
public static void DaysAgo(int year,int month,int day,int days) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
arr[2] = 29;
int a = 0,b = 0,c = 0;
if(days > 0) {
if(days < day) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 1) {
a = year -1;
b = 12;
c = arr[b] - days + day;
}
else {
a = year;
b = month - 1;
c = arr[b] - days + day;
}
}
}
else if(days == 0) {
a = year;
b = month;
c = day;
}
else if (days < 0){
if(day - days <= arr[month]) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 12) {
a = year + 1;
b = 1;
c = day - days - arr[month];
}
else {
a = year;
b = month + 1;
c = day - days - arr[month];
}
}
}
System.out.print(days + " days ago is:" + a + "-" + b + "-" + c);
}
}
题目集3 7-2
定义日期类 (28 分)
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:
输入格式:在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:输入样例1:
- 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
- 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
在这里给出一组输入。例如:
1912 12 25结尾无空行
输出样例1:在这里给出相应的输出。例如:
Next day is:1912-12-26结尾无空行
输入样例2:在这里给出一组输入。例如:
2001 2 30结尾无空行
输出样例2:在这里给出相应的输出。例如:
Date Format is Wrong结尾无空行
源代码:
import java.util.Scanner;
class Date{
static int year;
static int month;
static int day;
int[] maxnum=new int[]{0,31,29,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 int getMonth() {
return month;
}
public int getDay() {
return day;
}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void setDay(int day) {
this.day = day;
}
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>=1900&&year<=2000&&month>0&&month<=12&&day<=a[month]&&day>0);
return checkInputValidity;
}
public static void getnextDate(int year,int month,int day) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
int d=0,m=0;
if(isLeapYear(year))
arr[2] = 29;
if(checkInputValidity(year,month,day)) {
if(month==12) {
if(day==arr[month]) {
year = year+1;
m = 1;
d=1;
}
else{
m=month;
d =day +1;
}
}
else {
if(day==arr[month]) {
m = month + 1;
d = 1;
}
else{
m=month;
d = day+1;
}
}
System.out.println("Next day is:"+year+"-"+m+"-"+d);
}
else
System.out.println("Date Format is Wrong");
}
}
public class Main {
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
Date rq=new Date();
rq.setYear(x.nextInt());
rq.setMonth(x.nextInt());
rq.setDay(x.nextInt());
rq.getnextDate(rq.getYear(), rq.getMonth(),rq.getDay());
}
}
题目集3 7-3
一元多项式求导(类设计) (50 分)
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf
输入格式:在一行内输入一个待计算导函数的表达式,以回车符结束。
输出格式:
- 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
- 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
- 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
- 当输出结果第一项系数符号为“+”时,不输出“+”;
- 当指数符号为“+”时,不输出“+”;
- 当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
输出格式见输入输出示例。
输入样例1:在这里给出一组输入。例如:
-2* x^-2+ 5*x^12-4*x+ 12结尾无空行
输出样例1:在这里给出相应的输出。例如:
4*x^-3+60*x^11-4结尾无空行
输入样例2:在这里给出一组输入。例如:
2*x^6-0*x^7+5结尾无空行
输出样例2:在这里给出相应的输出。例如:
Wrong Format结尾无空行
源代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
import java.math.*;
public class Main{
public static void main(String[] args) {
int i,s1,flag=0,s2=0,K,s4=0;
long t;
long [] a=new long[3];
Scanner in =new Scanner(System.in);
String s = in.nextLine();
String b = s.replaceAll(" ","");
if(b.equals("")){
System.out.print("Wrong Format");
System.exit(0);
}
if(constant(b)||Wrong(b))
System.exit(0);
Match match = new Match();
match.setB(b);
match.setP();
match.setM();
match.setT();
while(match.getM().find()){
Matcher x = match.getT().matcher(match.getM().group());
for(i=1;i<=2;i++)
a[i]=0;
i=1;
while(x.find()){
a[i++] =Long.parseLong(x.group());
}
for(i=1,s1=0;i<=2&&a[i]!=0;i++)
{
s1++;
}
if(a[1]==1||a[2]==1){
if(a[1]==1){
if(flag==1&&!match.getM().group().contains("*")){
for(K=0,s4=0;K0&&a[2]>0)||(a[1]<0&&a[2]<0)){
System.out.print("+"+Math.abs(a[2])+"*x");
if(t!=1)
System.out.print("^"+t);
}
else{
System.out.print("-"+Math.abs(a[2])+"*x");
if(t!=1)
System.out.print("^"+t);
}
}
if(flag==0&&match.getM().group().contains("*")){
t=a[2]-1;
if((a[1]>0&&a[2]>0)||(a[1]<0&&a[2]<0)){
System.out.print(Math.abs(a[2])+"*x");
if(t!=1)
System.out.print("^"+t);}
else{
System.out.print("-"+Math.abs(a[2])+"*x");
if(t!=1)
System.out.print("^"+t);
}
flag=1;
}
}
else{
if(flag==1){
for(K=0,s4=0;K0&&a[2]>0)||(a[1]<0&&a[2]<0)){
String W=Long.toString(a[1]);
String E=Long.toString(a[2]);
BigInteger R=new BigInteger(W);
BigInteger U=new BigInteger(E);
System.out.print("+"+R.multiply(U).toString());
System.out.print("*x");}
else{
String W=Long.toString(a[1]);
String E=Long.toString(a[2]);
BigInteger R=new BigInteger(W);
BigInteger U=new BigInteger(E);
System.out.print(R.multiply(U).toString());
System.out.print("*x");
flag=1;
}
t=a[2]-1;
if(t!=1)
System.out.print("^"+t);
}
if(flag==0){
String W=Long.toString(a[1]);
String E=Long.toString(a[2]);
BigInteger R=new BigInteger(W);
BigInteger U=new BigInteger(E);
System.out.print(R.multiply(U).toString());
System.out.print("*x");
t=a[2]-1;
if(t!=1)
System.out.print("^"+t);
flag=1;
}
}
if(s1==1)
{
if(match.getM().group().contains("*")){
if(a[1]>0&&flag==1)
System.out.print("+"+a[1]);
if(a[1]>0&&flag==0){
System.out.print(a[1]);
flag=1;
}
if(a[1]<0){
System.out.print(a[1]);
if(flag==0)
flag=1;
}
}
if(match.getM().group().contains("^")){
for(K=0,s2=0;K



