Java语言程序设计-基础篇-第八版-第二章

1

 1 public class ComputeArea {
 2 
 3 public static void main(String[] args){
 4 
 5     double radius;
 6 
 7     double area;
 8 
 9     radius = 20;
10 
11     area = radius * radius * 3.14159;
12 
13     System.out.println("The area for the circle of radius " + radius + " is " + area);
14 
15   }
16 
17 }
18 
19 //Output:The area for the circle of radius 20.0 is 1256.636

2

 1 import java.util.Scanner;
 2 
 3 public class ComputeAverage {
 4 
 5   public static void main (String[] args) {
 6 
 7   Scanner input = new Scanner(System.in);
 8 
 9   System.out.print("Enter three numbers: ");
10 
11   double number1 = input.nextDouble();
12 
13   double number2 = input.nextDouble();
14 
15   double number3 = input.nextDouble();
16 
17   double average = (number1 + number2 + number3) / 3;
18 
19   System.out.println("The average of " + number1 + " " + number2 + " "+ number3 + " is " + average);
20 
21   }
22 
23 }
24 
25 /*Output:
26 Enter three numbers: 6 7 8
27 The average of 6.0 7.0 8.0 is 7.0*/

3

 1 import java.util.Scanner;
 2 public class ComputerAreaWithConsoleInput {
 3     public static void main(String[] args){
 4         Scanner input = new Scanner(System.in);
 5         System.out.print("Enter a number for radius: ");
 6         double radius = input.nextDouble();
 7         double area = radius * radius *3.14159;
 8         System.out.println("The area for the circle of radius " + radius + " is " + area);
 9     }
10 }
11 /*output:Enter a number for radius: 30
12 The area for the circle of radius 30.0 is 2827.431*/

4

 1 import java.util.Scanner;
 2 public class DisplayTime {
 3   public static void main(String[] args){
 4       Scanner input = new Scanner(System.in);
 5       System.out.println("Enter an integer for seconds: ");
 6       int seconds = input.nextInt();
 7       int minutes = seconds / 60;
 8       int remainingSeconds = seconds % 60;
 9       System.out.println(seconds + " seconds is " + minutes + " minutes and " + remainingSeconds + " seconds");
10   }
11 }
12 /*output:Enter an integer for seconds: 
13 500
14 500 seconds is 8 minutes and 20 seconds*/

5

 1 import java.util.Scanner;
 2 public class FahrenheitToCelsius {
 3   public static void main(String[] args){
 4       Scanner input = new Scanner(System.in);
 5       System.out.println("Enter a degree in Fahrenheit");
 6       double fahrenheit = input.nextDouble();
 7       double celsius = (5.0 / 9) * (fahrenheit - 32);
 8       System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius");
 9   }
10 }
11 /*
12 output:Enter a degree in Fahrenheit
13 70
14 Fahrenheit 70.0 is 21.11111111111111 in Celsius
15 */

6

 1 public class ShowCurrentTime {
 2   public static void main(String[] arga){
 3       long totalMilliseconds = System.currentTimeMillis();
 4       long totalSeconds = totalMilliseconds / 1000;
 5       long currentSecond = (int)(totalSeconds % 60);
 6       long totalMinutes = totalSeconds / 60;
 7       long currentMinute = (int)(totalMinutes % 60);
 8       long totalHours = totalMinutes / 60;
 9       long currentHour = (int)(totalHours % 24);
10       System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
11   }
12 }
13 //output:Current time is 13:14:55 GMT

7

 1 import java.util.Scanner;
 2 public class ComputeLoan {
 3     public static void main(String[] args){
 4         Scanner input = new Scanner(System.in);
 5         System.out.println("enter yearly interest rate, for example 8.25: ");
 6         double annualInterestRate = input.nextDouble();
 7         double monthlyInterestRate = annualInterestRate / 1200;
 8         System.out.print("Enter number of years as an integer, for example 5: ");
 9         int numberOfYears = input.nextInt();
10         System.out.print("Enter loan amount, for example 120000.95: ");
11         double loanAmount = input.nextDouble();
12         double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
13         double totalPayment = monthlyPayment * numberOfYears * 12;
14         System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) / 100.0);
15         System.out.println("The total payment is " + (int)(totalPayment * 100) / 100.0);
16     }
17 }
18 /*output:
19  enter yearly interest rate, for example 8.25: 
20 9
21 Enter number of years as an integer, for example 5: 7
22 Enter loan amount, for example 120000.95: 60000
23 The monthly payment is 965.34
24 The total payment is 81088.95
25  */

8

 

1 import javax.swing.JOptionPane;
2 public class DisplayUnicode {
3   public static void main(String[] args){
4       JOptionPane.showMessageDialog(null,"\u6B22\u8FCE \u03b1 \u03b2 \u03b3", "\u6B22\u8FCE Welcome",JOptionPane.INFORMATION_MESSAGE);
5   }
6 }

 

 

 9

 

 1 import java.util.Scanner;
 2 public class ComputeChange {
 3   public static void main(String[] args) {
 4     Scanner input = new Scanner(System.in);
 5     System.out.print("Enter an amount in double, for example 11.56: ");
 6     double amount = input.nextDouble();
 7     int remainingAmount = (int)(amount * 100);
 8     int numberOfOneDollars = remainingAmount / 100;
 9     remainingAmount = remainingAmount % 100;
10     int numberOfQuarters = remainingAmount / 25;
11     remainingAmount = remainingAmount % 25;
12     int numberOfDimes = remainingAmount / 10;
13     remainingAmount = remainingAmount % 10;
14     int numberOfNickels = remainingAmount / 5;
15     remainingAmount = remainingAmount % 5;
16     int numberOfPennies = remainingAmount;
17     String output = "Your amount " + amount + " consists of \n" +
18       "\t" + numberOfOneDollars + " dollars\n" +
19       "\t" + numberOfQuarters + " quarters\n" +
20       "\t" + numberOfDimes + " dimes\n" +
21       "\t" + numberOfNickels + " nickels\n" +
22       "\t" + numberOfPennies + " pennies";
23     System.out.println(output);
24   }
25 }
26 /*
27  Enter an amount in double, for example 11.56: 90
28 Your amount 90.0 consists of 
29     90 dollars
30     0 quarters
31     0 dimes
32     0 nickels
33     0 pennies 
34  */

10

 

 1 import javax.swing.JOptionPane;
 2 public class ComputeLoanUsingInputDialog {
 3   public static void main(String[] args){
 4     String annualInterestRateString = JOptionPane.showInputDialog("enter yearly interest rate, for example 8.25:");
 5     double annualInterestRate = Double.parseDouble(annualInterestRateString);
 6     double monthlyInterestRate = annualInterestRate / 1200;
 7     String numberOfYearsString = JOptionPane.showInputDialog("enter number of years as an integer, \nfor example 5:");
 8     int numberOfYears = Integer.parseInt(numberOfYearsString);
 9     String loanString = JOptionPane.showInputDialog("Enter loan amount, for example 120000.95:");
10     double loanAmount = Double.parseDouble(loanString);
11     double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
12     double totalPayment = monthlyPayment * numberOfYears * 12;
13     monthlyPayment = (int)(monthlyPayment * 100);
14     totalPayment = (int)(totalPayment * 100) / 100.0;
15     String output = "the monthly payment is " + monthlyPayment + "\nythe total payment is " + totalPayment;
16     JOptionPane.showMessageDialog(null, output);
17   }
18 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/bailun/archive/2012/12/21/2828614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值