《Java语言程序设计与数据结构》编程练习答案(第三章)(一)
程序员文章站
2024-02-02 13:37:52
...
《Java语言程序设计与数据结构》编程练习答案(第三章)(一)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
3.1
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
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 delta = b*b-4*a*c;
if(delta<0)
System.out.println("The equation has no real roots");
else if(delta==0)
System.out.println("The equation has one root "+(-1*b/2/a));
else
{
double r1 = (-1*b+Math.pow(delta,0.5))/2/a;
double r2 = (-1*b-Math.pow(delta,0.5))/2/a;
System.out.println("The equation has two roots "+r1+" and "+r2);
}
}
}
3.2
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
int number1 = (int)(System.currentTimeMillis()%10);
int number2 = (int)(System.currentTimeMillis()/10%10);
int number3 = (int)(System.currentTimeMillis()/100%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
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter 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 jb = a*d-b*c;
if(jb==0)
System.out.println("The equation has no solution");
else
System.out.println("x is "+(e*d-b*f)/jb+" and y is "+(a*f-e*c)/jb);
}
}
3.4
public class book {
public static void main(String[] args)
{
int month = (int)(Math.random()*12+1);
String[] names={"Feb","Jan","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
System.out.println("The number is "+month+", and the month is "+names[month-1]);
}
}
3.5
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter today's day:");
int day = input.nextInt();
System.out.print("Enter the number of days elapsed since today:");
int plus = input.nextInt();
int result = (day+plus)%7;
String[] days = {"Sun","Mon","Tues","Wed","Thu","Fri","Sat"};
System.out.println("Today is "+days[day]+" and the future day is "+days[result]);
}
}
3.6
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter weight in pounds:");
double pounds = input.nextDouble();
System.out.print("Enter feet:");
double feet = input.nextDouble();
System.out.print("Enter inches:");
double inches = input.nextDouble();
final double KILOS_PER_POUND = 0.45359237;
final double METERS_PER_INCH = 0.0254;
final double METERS_PER_FOOT = 0.3048;
double kilos = pounds*KILOS_PER_POUND;
double meters = feet*METERS_PER_FOOT+inches*METERS_PER_INCH;
double bmi = kilos/(meters*meters);
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
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double, for example 114.514:");
double amount = input.nextDouble();
int remainAmount = (int)(amount*100);
int numberOfDollars = remainAmount/100;
remainAmount%=100;
int numberOfQuarters = remainAmount/25;
remainAmount%=25;
int numberOfDimes = remainAmount/10;
remainAmount%=10;
int numberOfNickels = remainAmount/5;
remainAmount%=5;
int numberOfPennies = remainAmount;
System.out.println("Your amount "+amount+" consists of");
if(numberOfDollars>0)
{
System.out.print(numberOfDollars);
if(numberOfDollars>1)
System.out.println(" dollars");
else
System.out.println(" dollar");
}
if(numberOfQuarters>0)
{
System.out.print(numberOfQuarters);
if(numberOfQuarters>1)
System.out.println(" quarters");
else
System.out.println(" quarter");
}
if(numberOfDimes>0)
{
System.out.print(numberOfDimes);
if(numberOfDimes>1)
System.out.println(" dimes");
else
System.out.println(" dime");
}
if(numberOfNickels>0)
{
System.out.print(numberOfNickels);
if(numberOfNickels>1)
System.out.println(" nickels");
else
System.out.println(" nickel");
}
if(numberOfPennies>0)
{
System.out.print(numberOfPennies);
if(numberOfPennies>1)
System.out.println(" pennies");
else
System.out.println(" penny");
}
}
}
3.8
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
System.out.print("Input 3 integers:");
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
if(num1>num2)
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
if(num2>num3)
{
int tmp = num2;
num2 = num3;
num3 = tmp;
}
if(num1>num2)
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
System.out.println(num1+" "+num2+" "+num3);
}
}
3.9
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as integer: ");
int isbnNum = input.nextInt();
int old = isbnNum;
for(int i=9;i>=1;i--)
{
sum+=isbnNum%10*i;
isbnNum/=10;
}
int tail = sum%11;
System.out.printf("The ISBN-10 number is %09d",old);
if(tail<10)
System.out.println(tail);
else
System.out.println("X");
}
}
3.10
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
int number1 = (int)(Math.random()*100);
int number2 = (int)(Math.random()*100);
System.out.print("What is "+number1+" + "+number2+"? ");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
if(number1+number2==answer)
System.out.println("You are correct!");
else
{
System.out.println("Your answer is wrong.");
System.out.println(number1+" + "+number2+" should be "+(number1+number2));
}
}
}
3.11
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the month number: ");
int month = input.nextInt();
System.out.print("Enter the year number: ");
int year = input.nextInt();
int[] months={31,28,31,30,31,30,31,31,30,31,30,31};
String[] monthNames={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
boolean isLeapYear = (year%4==0&&year%100!=0)||(year%400==0);
if(isLeapYear)
months[1]=29;
System.out.println(monthNames[month-1]+" "+year+" has "+months[month-1]+" days.");
}
}
上一篇: javaweb-一个投票网页
下一篇: 记录自己做过的面试题