Java语言程序设计与数据结构(基础篇)梁勇第二章书中例题

2022/3/20 20:58:21

本文主要是介绍Java语言程序设计与数据结构(基础篇)梁勇第二章书中例题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

程序清单2-1:ComputeArea.java

public class ComputeArea {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double radius;
		double area;
		radius = 20;
		area = radius * radius * 3.14159; 
        System.out.println("The area for the circle of radius " + radius + " is " + area);
	}

}

 程序清单2-2:ComputeAreaWithConsoleInput.java

import java.util.Scanner;
 
public class ComputeAreaWithConsoleInput {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number for radius: ");
        double radius = input.nextDouble();
        double area = radius * radius * 3.14159;
        System.out.println("The area for the circle of radius " + radius + " is " + area);
        input.close();
	}
 
}

程序清单2-3:ComputeAverage.java

import java.util.Scanner;

public class ComputeAverage {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a number for radius: ");
		double number1 = input.nextDouble();
		double number2 = input.nextDouble();
		double number3 = input.nextDouble();
		double average = (number1 + number2 + number3) / 3;
		System.out.println("The average of " + number1 + " " + number2 + " " + number3 + " is " + average);
		input.close();
	}

}

 程序清单2-4:ComputeAreaWithConstant.java

import java.util.Scanner;

public class ComputeAreaWithConstant {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        final double PI = 3.14159;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number for radius: ");
        double radius = input.nextDouble();
        double area = radius * radius * PI;
        System.out.println("The area for the circle of radius " + radius + "  is " + area);
        input.close();
	}

}

 程序清单2-5:DisplayTime.java

import java.util.Scanner;

public class DisplayTime {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer for seconds: ");
        int seconds = input.nextInt();
        int minutes = seconds / 60;
        int remainingSeconds = seconds % 60;
        System.out.println(seconds + " seconds is " + minutes + " minutes and " + remainingSeconds + " seconds");
        input.close();
	}

}

 程序清单2-6:FahrenheitToCelsius.java

import java.util.Scanner;

public class FahrenheitToCelsius {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a degree in Fahrenheit: ");
        double fahrenheit = input.nextDouble();
        double celsius = (5.0 / 9) * (fahrenheit - 32);
        System.out.println("Fahrenheit " + fahrenheit + " is " + celsius +" in Celeius");
        input.close();
	}

}

  程序清单2-7:ShowCurrentTime.java

public class ShowCurrentTime {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        long totalMilliseconds = System.currentTimeMillis();
        long totalSeconds = totalMilliseconds / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        long currentHour = totalHours % 24;
        System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
	}

}

程序清单2-8:SalesTax.java

import java.util.Scanner;

public class SalesTax {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter purchase amount: ");
        double purchaseAmount = input.nextDouble();
        double tax = purchaseAmount * 0.06;
        System.out.println("Sales tax is $" + (int)(tax * 100) / 100.0);
        input.close();
	}

}

 程序清单2-9:ComputeLoan.java

import java.util.Scanner;

public class ComputeLoan {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter annual interest rate, e.g., 7.25: ");
		double annualInterestRate = input.nextDouble();
		double monthlyInterestRate = annualInterestRate / 1200;
		System.out.print("Enter number of years as an integer, e.g.,5: ");
		int numberOfYears = input.nextInt();
		System.out.print("Enter loan amount, e.g.,120000.95: ");
		double loanAmount = input.nextDouble();
		double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
		double totalPayment = monthlyPayment * numberOfYears * 12;
		System.out.println("The monthly payment is $" + (int)(monthlyPayment * 100) / 100.0);
		System.out.println("The total payment is $" + (int)(totalPayment * 100) / 100.0);
		input.close();
	}

}

 程序清单2-10: ComputeChange.java

import java.util.Scanner;

public class ComputeChange {

	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;
        System.out.println("Your amount " + amount + "consists of");
        System.out.println(" " + numberOfOneDollars + " dollars");
        System.out.println(" " + numberOfQuarters + " quarters");
        System.out.println(" " + numberOfDimes + " dimes");
        System.out.println(" " + numberOfNickels + " nickels");
        System.out.println(" " + numberOfPennies + " pennies");
        input.close();
	}

}



这篇关于Java语言程序设计与数据结构(基础篇)梁勇第二章书中例题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程