博主会一直更新完这本书的编程练习题答案,有问题的可以邮件联系,我和你们一起学习这本书,希望有志同道合的道友一起进步。觉得博主写的还可以就帮顶一下,给点鼓励嘛~QAQ
3.1代数:解一元二次方程
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner (System.in);
System.out.print("Enter a,b,c:");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double r1,r2;
if(Math.pow(b, 2)-4*a*c>0){
r1=((-b)+Math.pow((Math.pow(b, 2)-4*a*c), 0.5))/2*a;
r2=((-b)-Math.pow((Math.pow(b, 2)-4*a*c), 0.5))/2*a;
System.out.println("The equation has two roots"+r1+" and "+r2);
}
else if (Math.pow(b, 2)-4*a*c==0){
r1=r2=(-b)/2*a;
System.out.println("The equation has one root "+r1);
}
else if (Math.pow(b, 2)-4*a*c<0){
System.out.println("The equation has no real roots");
}
}
}
3.2游戏:三个数的加法
首先这一题我们先用System.currentTimeMillis()来创建随机数,Math.random()更加好用,但是既然书本上还没有讲到那么我们就先这么做吧。同学们也可以去拓展下Math.random()生成随机数的知识。
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number1=(int)-System.currentTimeMillis()%10;
//因为System.currentTimeMillis()获取当前系统时间是long型的,用int型接收会造成溢出,所以前面加一个负号
int number2=(int)System.currentTimeMillis()*7%10;
int number3=(int)-System.currentTimeMillis()/7%10;
Scanner input=new Scanner (System.in);
System.out.print("what is "+number1+"+"+number2+"+"+number3+"?");
int answer=input.nextInt();
System.out.println(number1+"+"+number2+"+"+number3+"="+answer+" is "+(number1+number2+number3==answer));
}
}
3.3代数:求解2×2线性方程
之前有写过这个程序,貌似在第一章节中,现在只需要加循环就好了。看清楚题目要求,就很简单了。
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);//先了解下控制台输入函数
System.out.println("please input value for a b c d e f:");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double d=input.nextDouble();
double e=input.nextDouble();
double f=input.nextDouble();
double x=(e*d-b*f)/(a*d-b*c);
double y=(a*f-e*c)/(a*d-b*c);
if(a*d-b*c!=0){
System.out.println("The x value is :"+x);
System.out.println("The y value is :"+y);
}else
System.out.println("The equation has no solution");
}
}
3.4随机月份
package nameyu;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int mouths=(int)(Math.random()*12+1);
switch(mouths){
case 1:System.out.println("January");break;
case 2:System.out.println("February");break;
case 3:System.out.println("March");break;
case 4:System.out.println("April");break;
case 5:System.out.println("May");break;
case 6:System.out.println("June");break;
case 7:System.out.println("July");break;
case 8:System.out.println("August");break;
case 9:System.out.println("September");break;
case 10:System.out.println("October");break;
case 11:System.out.println("November");break;
case 12:System.out.println("December");
}
}
}
3.5找到将来的日期
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter today's day instead of number 0-6:(0:sunday 1:monday...6:saturday)");
int weeks=input.nextInt();
System.out.print("Enter the number of days elapsed since today:");
int days=input.nextInt();
int newdaysweek=(weeks+days)%7;
String output=" ";
switch(newdaysweek){
case 0:output="Sunday";break;
case 1:output="Monday";break;
case 2:output="Tuesday";break;
case 3:output="Wednesday";break;
case 4:output="Thursday";break;
case 5:output="Friday";break;
case 6:output="Saturday";
}
String day=" ";
switch(weeks){
case 0:day="Sunday";break;
case 1:day="Monday";break;
case 2:day="Tuesday";break;
case 3:day="Wednesday";break;
case 4:day="Thursday";break;
case 5:day="Friday";break;
case 6:day="Saturday";
}
System.out.println("Today is "+day+" and the future day is "+output);
}
}
3.6医疗应用程序:BMI
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter weight in pounds:");
double weight=input.nextDouble();
System.out.print("Enter feet:");
double feet=input.nextDouble();
System.out.print("Enter inches");
double inches=input.nextDouble();
final int FEET_PER_INCHES =12;
final double KILOGRAM_PERPOUND=0.45359237;
final double METERS_PER_INCH=0.0254;
double weightInKilograms=weight*KILOGRAM_PERPOUND;
double heightInMeters=(double)(feet*12+inches)*0.0254;
double BMI=weightInKilograms/(heightInMeters*heightInMeters);
System.out.println("BMI is "+BMI);
if(BMI<18.5)
System.out.println("underweight");
else if(BMI<25)
System.out.println("normal");
else if(BMI<30)
System.out.println("overweight");
else
System.out.println("obese");
}
}
3.7整钱兑零
这道题,首先得认真看清楚题目的意思。题目的意思是,输出还是按照之前程序清单2-10那么输出,只是非零的币值单位将不再显示出来。理清楚思路就很好做了。虽然讲的很简单,但是本人也是走了很多弯路哎~惭愧
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter an amount in double,for example 11.56:");
double amount=input.nextDouble();
int remainingAmount=(int)(amount*100);
int numberOfOneDollars=remainingAmount/100;
remainingAmount=remainingAmount%100;
int numberOfQuarters=remainingAmount/25;
remainingAmount=remainingAmount%25;
int numberOfDimes=remainingAmount/10;
remainingAmount=remainingAmount%10;
int numberOfNickels=remainingAmount/5;
remainingAmount=remainingAmount%5;
int numberOfPennies=remainingAmount;
String output="Your amount"+amount+"consists of\n";
if (numberOfOneDollars>1){
output+="\t"+numberOfOneDollars+"dollars\n";
}else if(numberOfOneDollars==1){
output+="\t"+numberOfOneDollars+"dollar\n";
}else
output=output;
if (numberOfQuarters>1){
output+="\t"+numberOfQuarters+"quarters\n";
}else if(numberOfQuarters==1){
output+="\t"+numberOfQuarters+"quarter\n";
}else
output=output;
if (numberOfDimes>1){
output+="\t"+numberOfDimes+"dimes\n";
}else if(numberOfDimes==1){
output+="\t"+numberOfDimes+"dime\n";
}else
output=output;
if (numberOfNickels>1){
output+="\t"+numberOfNickels+"nickels\n";
}else if(numberOfNickels==1){
output+="\t"+numberOfNickels+"nickel\n";
}else
output=output;
if (numberOfPennies>1){
output+="\t"+numberOfPennies+"pennies\n";
}else if(numberOfPennies==1){
output+="\t"+numberOfPennies+"pennie\n";
}else
output=output;
System.out.println(output);
}
}
3.8对三个整数排序
本题要求采用非降序的形式显示这三个整数,所以我们采用升序来进行排列。
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter there numbers in int,for example 2 3 1:");
int number1=input.nextInt();
int number2=input.nextInt();
int number3=input.nextInt();
if(number1>number2&&number1>number3){
if(number2>number3){
System.out.println(number3+" "+number2+" "+number1);
}
else
System.out.println(number2+" "+number3+" "+number1);
}
else if(number2>number1&&number2>number3){
if(number1>number3){
System.out.println(number3+" "+number1+" "+number2);
}
else
System.out.println(number1+" "+number3+" "+number2);
}
else if(number3>number1&&number3>number2){
if(number1>number2){
System.out.println(number2+" "+number1+" "+number3);
}
else
System.out.println(number1+" "+number2+" "+number3);
}
}
}
3.9商业:检查ISBN-10
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter 9 byte number");
int numbers=input.nextInt();
int d1=numbers/100000000;
int d2=numbers/10000000%10;
int d3=numbers/1000000%10;
int d4=numbers/100000%10;
int d5=numbers/10000%10;
int d6=numbers/1000%10;
int d7=numbers/100%10;
int d8=numbers/10%10;
int d9=numbers%10;
int d10=(d1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
if (d10==10){
System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 + "" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + "X");}
else
System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 +"" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + d10);
}
}
3.10游戏:加法检测
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int number1=(int)(Math.random()*99+1);
int number2=(int)(Math.random()*99+1);
//System.out.println(number1+" "+number2);测试两个数字是否显示是在1-100之间
System.out.println(number1+"+"+number2+"= ?");
int answer=input.nextInt();
if(number1+number2==answer){
System.out.println("You aer correct");
}
else{
System.out.println("You answer is wrong");
System.out.println(number1+"+"+number2+"should be"+(number1+number2));
}
}
}
3.11给出一个月的总天数
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter a years and a mouth like 2012 12:");
int years=input.nextInt();
int mouths=input.nextInt();
switch(mouths){
case 1: System.out.println("January"+" "+ years +" "+"has 31 days");break;
case 2: if((years%4==0&&years%100!=0)||years%400==0){
System.out.println("February"+" "+ years +" "+"has 29 days");break;
}else
System.out.println("February"+" "+ years +" "+"has 28 days");break;
case 3: System.out.println("March"+" "+ years +" "+"has 31 days");break;
case 4: System.out.println("April"+" "+ years +" "+"has 30 days");break;
case 5: System.out.println("May"+" "+ years +" "+"has 31 days");break;
case 6: System.out.println("June"+" "+ years +" "+"has 30 days");break;
case 7: System.out.println("July"+" "+ years +" "+"has 31 days");break;
case 8: System.out.println("August"+" "+ years +" "+"has 31 days");break;
case 9: System.out.println("September"+" "+ years +" "+"has 30 days");break;
case 10: System.out.println("October"+" "+ years +" "+"has 31 days");break;
case 11: System.out.println("November"+" "+ years +" "+"has 30 days");break;
case 12: System.out.println("December"+" "+ years +" "+"has 31 days");
}
}
}
3.12回文数字
这道题会有个问题,先埋下伏笔,兄弟们可以思考下010这个数值。这里只能显示10是个回文数,输出的不是010是个回文数。
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter three-digit integer:");
int number=input.nextInt();
int b0=number/100;
int b1=number/10%10;
int b2=number%10;
if(b0==b2){
System.out.println(number+" "+"is a palindrome");
}else
System.out.println(number+" "+"is not a palindrome");
}
}
续上:改进了一下
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter three-digit integer:");
String number=input.nextLine();
if(number.charAt(0)==number.charAt(2)){
System.out.println(number+" "+"is a palindrome");
}else
System.out.println(number+" "+"is not a palindrome");
}
}
3.15游戏:彩票
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int lottery=(int)(Math.random()*1000);
Scanner input=new Scanner(System.in);
System.out.println("Enter your lottery pick(three digits):");
int guess=input.nextInt();
int guessDigit1=guess/100;
int guessDigit2=guess/10%10;
int guessDigit3=guess%10;
int lotteryDigit1=lottery/100;
int lotteryDigit2=lottery/10%10;
int lotteryDigit3=lottery%10;
System.out.println("The lottery number is "+lottery);
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if ((lotteryDigit1 == guessDigit1 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit2)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit2 && lotteryDigit3 == guessDigit1)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit3 && lotteryDigit3 == guessDigit1)
|| (lotteryDigit1 == guessDigit3 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit3)
|| (lotteryDigit1 == guessDigit2 && lotteryDigit2 == guessDigit1 && lotteryDigit3 == guessDigit2))
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3
|| guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry,no match");
}
}
3.16随机点
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int wide=(int)(Math.random()*101+(-50));
int high=(int)(Math.random()*201+(-100));
Scanner input=new Scanner(System.in);
System.out.println("The coordinate is " + "(" + wide + "," + high + ")");
}
}
3.17游戏:石头剪刀布
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("scissor (0), rock(1), paper(2):");
int numByUser = input.nextInt();
int numByComputer = (int)(Math.random() * 3);
if (numByUser == numByComputer){
switch(numByUser) {
case 0:System.out.println("The computer is scissor. You are scissor too.It is draw."); break;
case 1:System.out.println("The computer is rock. You are rock too.It is draw."); break;
case 2:System.out.println("The computer is paper. You are paper too.It is draw."); break;
}
}
else if ((numByUser == 0 && numByComputer == 2)
|| (numByUser == 1 && numByComputer == 0)
|| (numByUser == 2 && numByComputer == 1)){
switch(numByUser) {
case 0:System.out.println("The computer is paper. You are scissor.You win!"); break;
case 1:System.out.println("The computer is scissor. You are rock.You win!"); break;
case 2:System.out.println("The computer is rock. You are paper.You win!"); break;
}
}
else if ((numByUser == 0 && numByComputer == 1)
|| (numByUser == 1 && numByComputer == 2)
|| (numByUser == 2 && numByComputer == 0)
){
switch(numByUser) {
case 0:System.out.println("The computer is rock. You are scissor.You lose!"); break;
case 1:System.out.println("The computer is paper. You are rock.You lose!"); break;
case 2:System.out.println("The computer is scissor. You are paper.You lose!"); break;
}
}
}
}
3.18运输成本
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the weight of the package:");
int weight = input.nextInt();
if (weight > 0 && weight <= 1)
System.out.println("The cost is 3.5");
else if (weight > 1 && weight <= 3)
System.out.println("The cost is 5.5");
else if (weight > 3 && weight <= 10)
System.out.println("The cost is 8.5");
else if (weight > 10 && weight <= 20)
System.out.println("The cost is 10.5");
else
System.out.println("The package cannot be shipped");
}
}
3.19计算三角形的周长
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides:");
int side1 = input.nextInt();
int side2 = input.nextInt();
int side3 = input.nextInt();
if ((side1 + side2 > side3) && (side1 + side3 > side2)
&& (side2 + side3 > side1))
System.out.println("The circle is " + (side1 + side2 + side3));
else
System.out.println("The side is not legal.");
}
}
选择这一章节的课后练习有点多,综合题之前的基本上全都做完了,代码也自己测试过,综合题将再下一次put出来,最近事情有点多,哎,可能还是太懒了………综合题后续会补上的
3.28几何:两个矩形
首先这道题的要求不是很好,他要求的是判定第二个矩形是在第一个矩形内,还是和第一个矩形内或者没有任何交集。也就是相离,相交和包含着三种关系。我这题的意思是证明两个矩形之间的关系。本来想使用点与点之间的距离来解决问题,但是发现不能完全解决,所以换了一种方法。代码如下
package nameyu;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter r1's center x-,y-coordinates,width,and height:");
double r1_xCoordinates=input.nextDouble();
double r1_yCoordinates=input.nextDouble();
double r1_width=input.nextDouble();
double r1_height=input.nextDouble();
System.out.print("Enter r2's center x-,y-coordinates,width,and height:");
double r2_xCoordinates=input.nextDouble();
double r2_yCoordinates=input.nextDouble();
double r2_width=input.nextDouble();
double r2_height=input.nextDouble();
// double dis=Math.sqrt(Math.pow((r2_xCoordinates-r1_xCoordinates), 2)+Math.pow((r2_yCoordinates-r1_yCoordinates), 2));
//两点之间的距离计算公式
if(Math.abs(r1_xCoordinates-r2_xCoordinates)>(r1_width/2+r2_width/2)&&Math.abs(r1_yCoordinates-r2_yCoordinates)>(r1_height/2+r2_height/2)){
System.out.println("相离");
}else if(Math.abs(r1_xCoordinates-r2_xCoordinates)<=(Math.abs(r1_width-r2_width))*0.5&&Math.abs(r1_yCoordinates-r2_yCoordinates)<=(Math.abs(r1_height-r2_height))*0.5){
System.out.println("包含");
}else
System.out.println("相交");
}
}
如有其它题需要解决,在评论下留言就好,CSDN的手机端看私信反应迟钝,有时候好几天才提示。简易评论留下你想要解决的题目号就好。如有错误,希望大家指正 。谢谢