编写程序如下:
import java.util.Scanner;
public class Exe21 {
public static void main(String[] args) {
double c,t;//c摄氏温度
System.out.println("Enter a degree in Celsius:");
Scanner in = new Scanner(System.in);
c = in.nextDouble();
t = (9.0 / 5) * c + 32;
System.out.println((int)c + "Celsius is " + t + "Fahrenheit");
}
}
其中要注意的是9 / 5 == 1
2.2
编写程序如下:
import java.util.Scanner;
public class Day21 {
public static void main(String[] args) {
double r,h,s,v;//定义半径为r,高为h
System.out.print("Enter the radius and length of a cylinder:");
Scanner in = new Scanner(System.in);
r=in.nextDouble();
double p = 3.1415926;
s = p*r*r;
System.out.printf("The area is %.4f" , s);
System.out.println ();
h = in.nextDouble();
v = s * h;
System.out.printf("The volume is %.1f" , v);
}
}
其中println是换行输出,print不换行,%.4f是保留小数点后四位。
编程如下:
import java.util.Scanner;
public class Day23 {
public static void main(String[] args) {
int num;//num 为这个数字
int sum = 0;
System.out.println("Enter a number between 0 and 1000:");
Scanner in = new Scanner(System.in);
num = in.nextInt();
do {
sum = sum + num % 10;
num = num / 10;
}while(num != 0);
System.out.println("The sum of digits is"+ sum);
}
}
定义sum为0然后每次取其个位数字 也就是%10,然后再除掉这个个位数字,直到这个数为零时结束循环
编写程序如下:
import java.util.*;
public class Day24 { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long total = (int) System.currentTimeMillis();
int change = in.nextInt();//读取时区
long totalseconds = total/1000;
//总秒数
long totalminutes = totalseconds/60;
//总分钟数
long seconds = totalseconds%60;
//当前秒数
long minutes = totalminutes%60;//当前分钟数
long totalhours = totalminutes/60;//总小时数
long hours = totalhours%24;//当前小时数
System.out.println(hours + ":"+minutes +":"+seconds);//输出
}
}
编写程序如下:
public class Day25 {
public static void main(String[] args) {
int amount;//定义amount为每月存入的钱数
System.out.print("Enter the monthly saving amount : ");//输入存入的钱数
Scanner in = new Scanner(System.in);
amount = in.nextInt();
double one = amount * (1 + 0.00417);
double two = (amount + one) * (1 + 0.00417);
double three = (amount + two) * (1 + 0.00417);
double four = (amount + three) * (1 + 0.00417);
double five = (amount + four) * (1 + 0.00417);
double six = (amount + five) * (1 + 0.00417);
System.out.printf("After the sixth month,the account value is $%.2f", six);
}
}
import java.util.Scanner;
public class Day26 {
public static void main(String[] args) {
double x1,y1,x2,y2;
System.out.print("Enter x1 and y1 :");
System.out.print("Enter x2 and y2 :");
//输入x1 y1 x2 y2
Scanner in = new Scanner(System.in);
x1 = in.nextDouble();
y1 = in.nextDouble();
x2 = in.nextDouble();
y2 = in.nextDouble();
double a = Math.pow(x2 - x1, 2);//求x2 - x1 的平方
double b = Math.pow(y2 - y1,2);//求y2 - y1的平方
double dist = Math.pow(a - b, 0.5);
System.out.print("The distance between the two points is : " + dist);
}
}
2.7
其代码为:
import java.util.Scanner;
public class Day27 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter three points for a tringle :");
double x1,y1,x2,y2,x3,y3;
x1 = in.nextDouble();
y1 = in.nextDouble();
x2 = in.nextDouble();
y2 = in.nextDouble();
x3 = in.nextDouble();
y3 = in.nextDouble();
double a = Math.pow((Math.pow((x1 - x2),2)) + (Math.pow((y1 - y2 ), 2)), 0.5);
double b = Math.pow((Math.pow((x1 - x3),2)) + (Math.pow((y1 - y3 ), 2)), 0.5);
double c = Math.pow((Math.pow((x3 - x2),2)) + (Math.pow((y3 - y2 ), 2)), 0.5);
double s = (a + b + c) / 2;
double k = s * (s - a) * (s - b)*(s - c);
double area = Math.pow(k, 0.5);
System.out.printf("The area of the triangle is %.1f", area);
}
}