JAVA语言程序设计第12版第二章课后习题

本章节涵盖了Java语言的23个课后习题,涉及基础语法、控制结构和数据类型等内容,是深入理解Java编程的重要实践环节。
摘要由CSDN通过智能技术生成

2.1

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        System.out.print("Enter a degree in Celsius:");
        Scanner input=new Scanner(System.in);
        double celsius=input.nextDouble();
        System.out.println(celsius+" Celsius is "+((9.0/5)*celsius+32)+" Fahrenheit");
    }
}

2.2 

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        System.out.print("Enter the radius and length of a cylinder:");
        double pi = Math.PI;
        Scanner input=new Scanner(System.in);
        double radius=input.nextDouble();
        double length=input.nextDouble();
        System.out.println("The area is "+pi*Math.pow(radius,2));
        System.out.println("The volume is "+pi*Math.pow(radius,2)*length);
    }
}

2.3

import java.util.Scanner;

public class Test03 {
    public static void main(String[] args) {
        System.out.print("Enter a value for feet:");
        Scanner input=new Scanner(System.in);
        double feet=input.nextDouble();
        System.out.println(feet+" feet is "+feet*0.305+" meters");
    }
}

2.4

import java.util.Scanner;

public class Test04 {
    public static void main(String[] args) {
        System.out.print("Enter a number in pounds:");
        Scanner input=new Scanner(System.in);
        double pounds=input.nextDouble();
        System.out.println(pounds+" pounds is "+pounds*0.454+" kilograms");
    }
}

2.5 

import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
        System.out.print("Enter the subtotal and a gratuity rate:");
        Scanner input=new Scanner(System.in);
        double subtotal=input.nextDouble();
        double rate=input.nextDouble()/100;
        System.out.println("The gratuity is "+(subtotal*rate)+" and total is $"+(subtotal+rate));
    }
}

2.6 

import java.util.Scanner;

public class Test06 {
    public static void main(String[] args) {
        System.out.print("Enter a number between 0 and 1000 : ");
        Scanner input=new Scanner(System.in);
        int num=input.nextInt();
        while(true){
            if(num<1000&&num>0){
                break;
            }
            else {
                System.out.print("Error !!!  please Enter a number between 0 and 1000 :");
                num=input.nextInt();
            }
        }
        int sum=0;
        for (int i = num; num >0 ; ) { //999  9 99 9 9 9
            sum+=num%10;
            num=num/10;
        }
        System.out.println("The sum of the digits is "+sum);
    }
}

2.7

​
package chaper2;

import java.util.Scanner;

/**
 * 编写程序,提示用户输入分钟数(例如十亿)然后显示这些分钟代表多少年和多少天。
 * 为了简化问题,假设一年有 365天。
 * @author siyihe
 * @create 2022-06-23 14:34
 * @project javaexec0
 */
public class Years {
    public static void main(String[] args) {
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值