java语言程序设计(基础篇)第二章程序练习题答案

2.1(将摄氏温度转化为华氏温度)
import java.util.*;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a degree in celsius: ");
        double Celsius = input.nextDouble();
       
        double Fahrenheit;
       
        Fahrenheit = (9.0/5) * Celsius + 32;
        System.out.println(Celsius + " Celsius is" + Fahrenheit + " Fahrenheit");
    }
}
2.2(计算圆柱体的体积)
import java.util.*;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the radius and length of  a cylinder: ");
        double radius = input.nextDouble();
        double length =input.nextDouble();
       
       
        double arearadius * radius * Math.PI;
        double volume = area * length;
        System.out.println("The area is " + area);
        System.generatedout.println("The volume is " + volume);
    }
}
2.3(将英尺转换为米)
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a value for feet: ");
        double feet = input.nextDouble();
       
        double meters = feet * 0.305;
        System.out.println(feet+ " feet is " + meters + " meters");
    }
}
2.4(将磅转换为千克)
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number in pounds: ");
        double pounds = input.nextDouble();
       
        double kilograms = pounds * 0.454;
        System.out.println(pounds + " pounds is " + kilograms + " kilograms");
    }
}
2.5(财务应用程序:计算消费)
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the subtotal and gratuity rate: ");
        double subtotal = input.nextDouble();
        double Gratuity = input.nextDouble();
        double gratuity = subtotal * Gratuity * 0.01;
        double total = gratuity + subtotal;
        System.out.println("The gratuity is $" + gratuity + " and total is " + total);
    }
}
2.6(求一个整数个位数的和)
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000: ");
        int number = input.nextInt();
       
        int sum = number % 10 + (number / 10) % 10 + (number / 100) % 10;
        System.out.println("The sum of the digits is " + sum);
    }
}
2.7(求出年数)
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of minutes: ");
        double minutes = input.nextDouble();
       
        int years = (int)minutes / (60*24*365);
        int days = (int)minutes / (60*24) - ((int)minutes / (60*24*365)) * 365;
        System.out.println(minutes + "minutes is approximately " + years + " years and " + days + " days");
    }
}
2.8(当前时间)
import java.util.*;
public class ShowXureentTime {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long totalMilliseconds = System.currentTimeMillis();//得到1970年1月1日到现在的毫秒数
       
        long totalSeconds = totalMilliseconds / 1000;//将总毫秒转化为总秒
       
        long currentSecond = totalSeconds % 60;//当前的秒数
       
        long totalMinutes = totalSeconds / 60;//得到总的分钟
       
        long currentMinute = totalMinutes % 60;//当前的分钟数
       
        long totalHours = totalMinutes / 60;//得到总小时

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。