当前位置:网站首页 > Java基础 > 正文 Java程序设计基础篇课本 Java基础 来源: 网络 编辑:小编 发布时间:2024-10-28 10:50:00 浏览量:17 //程序清单2-1 public class ComputeArea { //功能:实现计算圆面积 public static void main(String[] args) { double radius; //Declare radius double area; //Declare area //Assign a radius radius = 20; //Compute Area area = radius * radius * 3.14159; //Display results System.out.println("The area for the circle of radius "+ radius + " is " + area); } } //程序清单2-2 import java.util.Scanner; //功能:从标准输入设备读取Double类型数,实现计算圆面积 public class ComputeAreaWithConsuleInput { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); //Prompt the user to enter a radius System.out.print("Enter a number for radius: "); double radius = input.nextDouble(); //Compute area double area = radius * radius * 3.14159; //Display result System.out.println("The area for the circle of radius "+ radius + "is " +area); } } //程序清单2-3 //功能:读取三个数值,然后显示它们的平均值 import java.util.Scanner; public class ComputeAverage { public static void main(String[] args) { //Create a Scanner object Scanner input = new Scanner(System.in); //Prompt the user to enter three numbers System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); //Compute average double average = (number1 + number2 + number3)/3; //Display result System.out.println("The average of "+ number1 + "" + number2 + "" + number3 + " is " + average); } } //程序清单2-4 //功能:计算一个以秒为单位的时间量所包含的分钟数和剩余秒数的程序 import java.util.Scanner; public class DisplayTime { public static void main(String[] args) { Scanner input = new Scanner(System.in); //提示用户输入 System.out.println("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"); } } //程序清单2-5 //功能:利用公式将华氏温度转换成摄氏温度 import java.util.Scanner; public class FahrenheitToCelsius { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a degree in Fahrenheit: "); double fahrenheit = input.nextDouble(); //Convert Fahrenheit to Celsius double celsius = (5.0 / 9) * (fahrenheit - 32); System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + "in Celsius"); } } //程序清单2-6 ShouCurrentTime.java //功能:显示当前时间 //说明:本题是开发一个显示当前GMT(格林威治标准时间)的程序; public class ShowCurrentTime { public static void main(String[] args) { //获得从1970年1月1日0点到当前时间之间单位为毫秒的差值 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 % 60; //输出结果 System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + "GMT"); } } //程序清单2-8 ComputeLoan.java import java.util.Scanner; public class ComputeLoan { public static void main(String[] args) { //创建一个Scanner对象 Scanner input = new Scanner(System.in); //输入年利率 System.out.println("Enter yearly interest rate,for example 8.25:" ); double annualInterestRate = input.nextDouble(); //获取月利率 double monthlyInterestRate = annualInterestRate/1200; //输入年数 System.out.println( "Enter number of years as an integer,for example 5: "); int numberOfYears = input.nextInt(); //输入贷款总数 System.out.println("Enter loan amount,for example .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); } } //程序清单2-9 DisplayUnicode.java //程序功能:显示两个中文字符和三个希腊字母 import javax.swing.JOptionPane; public class DisplayUnicode { public static void main(String[] args){ JOptionPane.showMessageDialog(null, "欢迎 αβγ", "欢迎 welcome",JOptionPane.INFORMATION_MESSAGE); } } //程序清单2-10 //程序功能:整钱兑零 import java.util.Scanner; public class ComputeChange { public static void main(String[] args){ //创建一个Scanner Scanner input Java程序设计基础篇课本 = new Scanner(System.in); //从控制台输入钱数 System.out.print( "Enter an amount in double, for example 11.56: "); double amount = input.nextDouble(); //引入变量remainingAmount来存储变化的余额 int remainingAmount = (int)(amount * 100); //将钱数转换为1分币的个数 //求出1美元的个数 int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; //求出2角5分币的个数 int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; //剩余1分币的个数 //求出1角币的个数 int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; //剩余1分币的个数 //求出5分币的个数 int numberOfNickles = remainingAmount / 5; remainingAmount = remainingAmount % 5; //剩余1分币的个数 //一分币的个数 int numberOfPennies = remainingAmount; //输出结果 System.out.println("Your amount " + amount + " consist of "+ " " + numberOfOneDollars + " dollars " + " " + numberOfQuarters + " quarters " + " " + numberOfDimes + " dimes " + " " + numberOfNickles + " nickles " + " " + numberOfPennies + " pennies"); } } //程序清单2-11 ComputeLoanUsingInputDialog.java import javax.swing.JOptionPane; public class ComputeLoanUsingInputDialog { public static void main(String[] args){ //Enter yearly interest rate String annualInterestRateString = JOptionPane.showInputDialog( "Enter yearly interest rate,for example 8.25: "); //Covert string to double double annualInterestRate = Double.parseDouble(annualInterestRateString); //Obtain monthly interest rate double monthlyInterestRate = annualInterestRate / 1200; //Enter number of years String numberOfYearString = JOptionPane.showInputDialog( "Enter numbers of years as an integer, for example 5: "); //Convert string to int int numberOfYears = Integer.parseInt(numberOfYearString); //Enter loan amount String loanString = JOptionPane.showInputDialog( "Enter loan amount,for example .95:"); //Convert String to double double loanAmount = Double.parseDouble(loanString); //Calclate payment double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1/ Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); double totalPayment = monthlyPayment * numberOfYears *12; //Format to keep two digits after the decimal point monthlyPayment = (int)(monthlyPayment * 100) / 100.0; totalPayment =(int)(totalPayment * 100) / 100.0; //Display results String output = "The monthly payment is " + monthlyPayment + " The total payment is " + totalPayment; JOptionPane.showMessageDialog(null,output); } } 上一篇: java语言基础第8章 下一篇: 0基础学java数组 版权声明: 本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。 本文网址:https://www.bianchenghao6.com/h6javajc/24655.html 相关文章: java语言基础第8章2024-10-28 10:50:00 java基础扩展2024-10-28 10:50:00 java基础进阶书2024-10-28 10:50:00 java开发基础测试题2024-10-28 10:50:00 Java基础单元测试2024-10-28 10:50:00 0基础学java数组2024-10-28 10:50:00 数学基础很差能学java吗2024-10-28 10:50:00 java基础面试题spring2024-10-28 10:50:00 黑马15天学完JAVA基础2024-10-28 10:50:00 日本零基础java2024-10-28 10:50:00