指出并修改以下代碼中的錯(cuò)誤:
目前成都創(chuàng)新互聯(lián)公司已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、高陽網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。public class Test{public void main(string[] args){? double i = 50.0;
? double k = double i + 50.0;
? double j = k + 1;
? System.out.println("j is " + j + " and
? k is " + k);
? }
}
public class Test{public static void main(String[] args){double i = 50.0;
double k = i + 50.0;
double j = k + 1;
System.out.println("j is " + j + " and k is " + k);
}
}
2.3習(xí)題如何編寫一條語句,讓用戶從鍵盤輸人一個(gè)雙精度值? 在執(zhí)行下面代碼的時(shí)候,如果你輸入5a, 將發(fā)生什么?
將發(fā)生一個(gè)運(yùn)行時(shí)錯(cuò)誤
下面兩個(gè)import語句之間有什么執(zhí)行的不同嗎?
Import java.util.Scanner;
Import java.util.*;
2.4習(xí)題.Scanner是明確導(dǎo)入,導(dǎo)入util包中Scanner類;.*是通配符導(dǎo)入,導(dǎo)入util包中所有的類
以下標(biāo)識(shí)符哪些是合法的?哪些是Java的關(guān)鍵字?
合法的:miles、Test、$4、x、y、radius、apps
非法的:a++、–a、4#R、#44、int
關(guān)鍵/保留字:class、public
2.5習(xí)題請指出并修改下面代碼中的錯(cuò)誤:
int i = k + 2;
System.out.println(i);
int k = 0;
int i = k + 2;
System.out.println(i);
2.6習(xí)題請指出并修改下面代碼中的錯(cuò)誤:
int i = j = k = 2;
int j;
int k;
int i = j = k = 2;
2.8習(xí)題使用常量的好處是什么? 聲明一個(gè)int類型的常量SIZE, 并且值為20。
可以不必頻繁的寫數(shù)值,代碼改動(dòng)只需修改一處
final int SIZE = 20;
類名、方法名、常量和變量的命名習(xí)慣是什么? 按照J(rèn)ava的命名習(xí)慣,以下哪些項(xiàng)可以作為常量、方法 、變量或者一個(gè)類?
類名:每個(gè)單詞首字母大寫,直接連接
變量和方法:第一個(gè)單詞小寫,后面每一個(gè)單詞首字母大寫,直接連接
常量:全部大寫,下劃線連接
類名:Test
變量和方法:read、readDouble
常量:MAX_VALUE
將以下算法翻譯成Java代碼
第一步:聲明一個(gè)雙精度型變量miles, 初始值為100
?第二步:聲明一個(gè)雙精度型常量KILOMETERS_PER_MILE, 初始值1.609
?第三步:聲明一個(gè)雙精度型變量kilometers, 將miles和KILOMETERS_PER_MILE相乘,并且將結(jié)果賦值給 kilometers
第四步:在控制臺(tái)顯示 kilometers
第四步之后,kilometers 是多少?
public class Main{public static void main(String[] args){double miles = 100;
final double KILOMETERS_PER_MILE = 1.609;
double kilometers = miles * KILOMETERS_PER_MILE;
System.out.print(kilometers);
}
}
160.9
2.9習(xí)題找到大和最小的byte、short、int、long、float以及double。這些數(shù)據(jù)類型中,哪個(gè)需要的內(nèi)存最小?
byte需要的內(nèi)存最小
給出以下求余計(jì)算的結(jié)果。
相對(duì)簡單
假設(shè)今天是周二,100天后將是周幾?
(2 + 100)% 7 = 2,所以是星期4
25/4的結(jié)果是多少? 如果你希望得到浮點(diǎn)數(shù)結(jié)果,如何重寫表達(dá)式?
6,需要將至少一個(gè)數(shù)字改成浮點(diǎn)數(shù)
給出以下代碼的結(jié)果:
System.out.println(2 * (5 / 2 + 5 / 2));
System.out.println(2 * 5 / 2 + 2 * 5 / 2);
System.out.println(2 * (5 / 2));
System.out.println(2 * 5 / 2);
8
10
4
5
下面的語句正確嗎? 如果正確的話,給出輸出
System.out.printIn("25 / 4 is " + 25 / 4);
System.out.printIn("25 / 4.0 is " + 25 / 4.0);
System.out.println("3.0 * 2 / 4 is " + 3 * 2 / 4);
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);
25 / 4 is 6
25 / 4.0 is 6.25
3.0 * 2 / 4 is 1
3.0 * 2 / 4 is 1.5
寫一個(gè)顯示 2 3.5 2^{3.5} 23.5的計(jì)算結(jié)果的語句
public class Main{public static void main(String[] args){System.out.println(Math.pow(2, 3.5));
}
}
假設(shè)m和r是整數(shù)。編寫一個(gè)Java表達(dá)式,使得 m r 2 mr^2 mr2可以得到一個(gè)浮點(diǎn)數(shù)類型的結(jié)果
public class Main{public static void main(String[] args){//已經(jīng)指定了double,進(jìn)行運(yùn)算的結(jié)果是小數(shù)
//沒有變量,直接打印結(jié)果,必須至少有1位是小數(shù),結(jié)果才是小數(shù)
double m = 1;
double r = 2;
System.out.print(m * r * r);
}
}
4.0
2.10習(xí)題在float和double類型的變量中保存了多少個(gè)精確位?
float中有7-8小數(shù)位,double有15-17小數(shù)位
以下哪些是正確的浮點(diǎn)數(shù)類型直接量?
全都是 12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39F, 40D 2.20
以下哪些和52.534是等價(jià)的?
等價(jià):5.2534e+1, 0.52534e+2, 525.34e-1
不等價(jià):5.2534e+0
哪些是正確的直接量?
2.11習(xí)題正確:
5.2534e+1,
5_2
錯(cuò)誤:
_2534,
5_
如何在 Java 中表達(dá)以下算術(shù)表達(dá)式?
4 3 ( r + 34 ) ? 9 ( a + b c ) + 3 + d ( 2 + a ) a + b d \frac{4}{3(r + 34)} - 9(a + bc) + \frac{3 + d(2+a)}{a+bd} 3(r+34)4??9(a+bc)+a+bd3+d(2+a)?
$5.5 $ x $ (r+2.5)^{2.5 + t}$
public class Main{public static void main(String[] args){int a;
int b;
int c;
int d;
int r;
int t = a = b = c = d = r = 1;
System.out.println(4/(3 * (r+34)) - 9 * (a + b * c) + (3 + d * (2 + a)) / (a + b * d));
System.out.print(5.5 * Math.pow(r + 2.5, 2.5 + t));
}
}
-15
441.164791259315
2.12 習(xí)題public class Main{public static void main(String[] args){//讀取當(dāng)前毫秒時(shí)間
long totalMilliseconds = System.currentTimeMillis();
//計(jì)算總秒數(shù)
long totalSeconds = totalMilliseconds / 1000;
//計(jì)算當(dāng)前秒數(shù)
long currentSeconds = totalSeconds % 60;
//計(jì)算總分鐘數(shù)
long totalMinutes = totalSeconds / 60;
//計(jì)算當(dāng)前分鐘數(shù)
long currentMinutes = totalMinutes % 60;
//計(jì)算總小時(shí)數(shù)
long totalHours = totalMinutes / 60;
//計(jì)算當(dāng)前小時(shí)數(shù)
long currentHours = totalHours % 24;
System.out.print("The current time is " + currentHours + ":" + currentMinutes + ":" + currentSeconds);
}
}
2.13習(xí)題給出以下代碼運(yùn)行的結(jié)果:
double a = 6.5;
a = a+1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
7.5
3.0
2.14習(xí)題下面的說法哪個(gè)為真?
a.任何表達(dá)式都可以用作一個(gè)語句。
b.表達(dá)式 x++ 可以用作一個(gè)語句。
c.語句x = x + 5也是一個(gè)表達(dá)式
d.x = y = x = 0是非法的。
a、b、c正確,d是合法的
給出以下代碼的輸出
public class Main{public static void main(String[] args){int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
int c = 6;
int d = ++c;
System.out.println(c);
System.out.println(d);
}
}
7
6
7
7
2.15習(xí)題在一次計(jì)算中,各種類型的數(shù)值可以一起使用嗎?
可以
將一個(gè)double類型數(shù)值顯式類型轉(zhuǎn)換為int時(shí),是如何處理double值的小數(shù)部分的? 類型轉(zhuǎn)換改變被類型轉(zhuǎn)換的變量嗎?
直接抹去小數(shù)部位;不改變
給出以下代碼片段的輸T出:
float f = 12.5F;
int i = (int)f;
System.out.println("f is " + f);
System.out.println("i is " +i);
f is 12.5
i is 12
給出以下代碼的輸出:
double amount = 5;
System.out.println(amount / 2);
System.out.println(5 / 2);
2.5
2
2.16習(xí)題如何編寫下面的數(shù)學(xué)表達(dá)式的代碼?
? b + b 2 ? 4 a c 2 a \frac{-b + \sqrt{b^2 - 4ac}}{2a} 2a?b+b2?4ac ??
public class Main{public static void main(String[] args){int a = 1;
int b = 4;
int c = 2;
System.out.println((-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a));
}
}
2.17習(xí)題import java.util.Scanner;
public class Main{public static void main(String[] args){//1)
Scanner input = new Scanner(System.in);
System.out.print("Please type your total money: ");
double amount = input.nextDouble();
System.out.println("You have " + amount + " dollars");
//2)
int remainingAmount = (int)(amount * 100);
//3)
int currentDollar = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
//4)
int currentQuarter = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
//5)
int currentDime = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
//6)
int currentNickel = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
//7)
System.out.println("You have " + currentDollar + " dollars");
System.out.println("You have " + currentQuarter + " quarters");
System.out.println("You have " + currentDime + " dimes");
System.out.println("You have " + currentNickel + " nickels");
System.out.println("You have " + remainingAmount + " pennies");
input.close();
}
}
Please type your total money: 1.99
You have 1.99 dollars
You have 1 dollars
You have 3 quarters
You have 2 dimes
You have 0 nickels
You have 4 pennies
2.18習(xí)題可以將一個(gè)變量聲明為int類型,之后重新將其聲明為double類型嗎?
int a = 5;
double b = (double)a;
什么是整數(shù)溢出? 浮點(diǎn)數(shù)操作會(huì)導(dǎo)致溢出嗎?
數(shù)字大小超過了當(dāng)前數(shù)據(jù)類型的限度;會(huì),但是一般情況不用考慮
溢出會(huì)導(dǎo)致一個(gè)運(yùn)行時(shí)錯(cuò)誤嗎?
會(huì)
什么是取整錯(cuò)誤? 整數(shù)操作會(huì)導(dǎo)致取整錯(cuò)誤嗎? 浮點(diǎn)數(shù)操作會(huì)導(dǎo)致取整錯(cuò)誤嗎?
編程練習(xí)題計(jì)算得到的數(shù)字精確值與算數(shù)值的差異;整數(shù)運(yùn)算不會(huì)導(dǎo)致錯(cuò)誤;浮點(diǎn)數(shù)會(huì)
(將攝氏溫度轉(zhuǎn)換為華氏溫度)編寫程序,從控制臺(tái)讀人double型的攝氏溫度,然后將其轉(zhuǎn)換為華氏溫度 ,并且顯示結(jié)果。轉(zhuǎn)換公式如下所示:
華氏溫度 = (9/5) * 攝氏溫度 + 32
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input your Celsius temperature: ");
double celsius = input.nextDouble();
double fahrenheit = (9.0 / 5) * celsius + 32;
System.out.println(fahrenheit);
input.close();
}
}
Please input your Celsius temperature: 43
109.4
(計(jì)算圓柱體的體積)編寫程序,讀人圓柱體的半徑和高,并使用下列公式計(jì)算圓柱的體積:面積=半徑 x 半徑 x p;體積=面積 X 高
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input cylinder's radius, and then length: ");
double radius = input.nextDouble();
double length = input.nextDouble();
double area = radius * radius * Math.PI;
double volume = area * length;
System.out.println("The area is " + area + " and the volume is " + volume);
input.close();
}
}
Please input cylinder's radius, and then length: 5.5 12
The area is 95.03317777109125 and the volume is 1140.398133253095
(將英尺轉(zhuǎn)換為米)編寫程序,讀人英尺數(shù),將其轉(zhuǎn)換為米數(shù)并顯示結(jié)果。一英尺等于0.305米
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input feet: ");
double feet = input.nextDouble();
double meter = feet * 0.305;
System.out.println(feet + " feets equal to " + meter + " meters");
input.close();
}
}
Please input feet: 16.5
16.5 feets equal to 5.0325 meters
(將磅轉(zhuǎn)換為千克)編寫程序,將磅數(shù)轉(zhuǎn)換為千克數(shù)。程序提示用戶輸人磅數(shù),然后轉(zhuǎn)換成千克并顯示結(jié)果。一磅等于 0.454 千克。
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input pounds: ");
double pounds = input.nextDouble();
double kilograms = pounds * 0.454;
System.out.println(pounds + " pounds equal to " + kilograms + " kilograms");
input.close();
}
}
Please input pounds: 55.5
55.5 pounds equal to 25.197 kilograms
(財(cái)務(wù)應(yīng)用程序:計(jì)算小費(fèi))編寫一個(gè)程序,讀入一筆費(fèi)用與酬金率,計(jì)算酬金和總錢數(shù)。例如, 如果用戶輸人10 作為費(fèi)用,15%作為酬金率,計(jì)算結(jié)果顯示酬金為 $1.5, 總費(fèi)用為11.5。
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input subtotal, and then gratuity rate: ");
double subtotal = input.nextDouble();
double gratuityRate = input.nextDouble();
double gratuity = (subtotal * gratuityRate) / 100;
double total = gratuity + subtotal;
System.out.println("The gratuity is " + gratuity + " and the total is " + total);
input.close();
}
}
Please input subtotal, and then gratuity rate: 10 15
The gratuity is 1.5 and the total is 11.5
(求一個(gè)整數(shù)各位數(shù)的和)編寫程序,讀取一個(gè)在0和1000之間的整數(shù),并將該整數(shù)的各位數(shù)字相加。例如:整數(shù)是932, 各位數(shù)字之和為14
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input a number: ");
int number = input.nextInt();
int divide1 = number / 10;
int divide2 = number % 10;
int divide3 = divide1 / 10;
int divide4 = divide1 % 10;
int total = divide2 + divide3 + divide4;
System.out.println(total);
input.close();
}
}
Please input a number: 589
22
(求出年數(shù))編寫程序,提示用戶輸入分鐘數(shù)(例如十億)然后顯示這些分鐘代表多少年和多少天。為了簡化問題,假設(shè)一年有 365 天
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input minutes: ");
int minutes = input.nextInt();
int years = minutes / 525600;
int remainingMinutes = minutes % 525600;
int days = remainingMinutes / 1440;
System.out.println(minutes + " minutes equal to " + years + " years and " + days + " days.");
input.close();
}
}
Please input minutes: 1000000000
1000000000 minutes equal to 1902 years and 214 days.
(當(dāng)前時(shí)間)程序清單2-7給出了顯示當(dāng)前格林威治時(shí)間的程序。修改這個(gè)程序,提示用戶輸入相對(duì)于 GMT 的時(shí)區(qū)偏移量,然后顯示在這個(gè)特定時(shí)區(qū)的時(shí)間。
import java.util.Scanner;
public class Main{public static void main(String[] args){//讀取當(dāng)前毫秒時(shí)間
long totalMilliseconds = System.currentTimeMillis();
//計(jì)算總秒數(shù)
long totalSeconds = totalMilliseconds / 1000;
//計(jì)算當(dāng)前秒數(shù)
long currentSeconds = totalSeconds % 60;
//計(jì)算總分鐘數(shù)
long totalMinutes = totalSeconds / 60;
//計(jì)算當(dāng)前分鐘數(shù)
long currentMinutes = totalMinutes % 60;
//計(jì)算總小時(shí)數(shù)
long totalHours = totalMinutes / 60;
//計(jì)算當(dāng)前小時(shí)數(shù)
long currentHours = totalHours % 24;
Scanner input = new Scanner(System.in);
System.out.println("Please input your local timeZone compared to GMT with +/- integers: ");
int timeZone = input.nextInt();
long newHours = currentHours + timeZone;
System.out.print("The current time is " + newHours + ":" + currentMinutes + ":" + currentSeconds);
}
}
Please input your local timeZone compared to GMT with +/- integers:
8
The current time is 20:53:44
(物理:加速度)平均加速度定義為速度的變化量除以這個(gè)變化所用的時(shí)間,如下式所示: a = v 1 ? v 0 t a = \frac{v_1 - v_0}{t} a=tv1??v0?? 編寫程序,提示用戶輸人以米/秒為單位的起始速度 v 0 v_0 v0?, 以米/秒為單位的終止速度 v 1 v_1 v1?,以及以秒為單位的時(shí)間段t, 最后顯示平均加速度
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input v_0, v_1, and t: ");
double v_0 = input.nextDouble();
double v_1 = input.nextDouble();
double t = input.nextDouble();
System.out.print("The acceleration is " + (v_1 - v_0) / t);
}
}
Please input v_0, v_1, and t: 5.5 50.9 4.5
The acceleration is 10.088888888888889
(科學(xué):計(jì)算能量)編寫程序,計(jì)算將水從初始溫度加熱到最終溫度所需的能量。程序應(yīng)該提示用戶輸人水的重量(以千克為單位 ),以及水的初始溫度和最終溫度。計(jì)算能量的公式是: Q = M x (最終溫度 - 初始溫度) x 4184,這里的M是以千克為單位的水的重量,溫度以攝氏度為單位,而能置Q以焦耳為單位。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input waterQuantity: ");
double waterquantity = input.nextDouble();
System.out.print("Please input initial temp: ");
double tem0 = input.nextDouble();
System.out.print("Please input final temp: ");
double tem1 = input.nextDouble();
System.out.print("The energy needed is " + waterquantity * (tem1 - tem0) * 4184);
}
}
Please input waterQuantity: 55.5
Please input initial temp: 3.5
Please input final temp: 10.5
The energy needed is 1625484.0
(人口統(tǒng)計(jì))重寫編程練習(xí)題1.11, 提示用戶輸人年數(shù),然后顯示這個(gè)年數(shù)之后的人口值。將編程練習(xí)題 1.11中的提示用于這個(gè)程序。人口數(shù)應(yīng)該類型轉(zhuǎn)換為一個(gè)整數(shù)
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input the years: ");
int years = input.nextInt();
System.out.println("第" + years + "年:" + 365 * years * (86400/7 - 86400/13 + 86400/45));
}
}
Please input the years: 5
第5年:13899200
(物理:求出跑道長度)假設(shè)一個(gè)飛機(jī)的加速度是a,而起飛速度是v,那么可以使用下面的公式計(jì)算出飛機(jī)起飛所需的最短跑道長度: 跑道長度 = v 2 2 a \frac{v^2}{2a} 2av2?。編寫程序,提示用戶輸人以米/秒(m/s) 為單位的速度v和以米/秒的平方(m/s2 ) 為單位的加速度a, 然后顯示最短跑道長度。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input speed and acceleration: ");
double speed = input.nextDouble();
double acceleration = input.nextDouble();
System.out.println("The minumum length for taking off is: " + speed * speed / (2 * acceleration) );
}
}
Please input speed and acceleration: 60 3.5
The minumum length for taking off is: 514.2857142857143
(財(cái)務(wù)應(yīng)用程序:復(fù)利值)假設(shè)你每月向銀行賬戶存100美元,年利率為5%,那么每月利率是0.05/12=0.00417。第一個(gè)月之后,賬戶上的值就變成:100 * (1 + 0.00417) = 100.417。第二個(gè)月之后,賬戶上的值就變成:(100 + 100.417) * (1 + 0.00417) = 201.252。第三個(gè)月之后,賬戶上的值就變成:(100 + 201.252) * (1 + 0.00417) = 302.507,依此類推。編寫程序顯示六個(gè)月后賬戶上的錢數(shù)。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input monthly saving: ");
double saving = input.nextDouble();
double total1 = saving * 1.00417;
double total2 = (saving + total1) * 1.00417;
double total3 = (saving + total2) * 1.00417;
double total4 = (saving + total3) * 1.00417;
double total5 = (saving + total4) * 1.00417;
double total6 = (saving + total5) * 1.00417;
System.out.println("The 6th saving is: " + total6);
}
}
Please input monthly saving: 100
The 6th saving is: 608.8181155768638
(醫(yī)療應(yīng)用程序:計(jì)算 BMI)身體質(zhì)量指數(shù)(BMI)是對(duì)體重的健康測量。它的值可以通過將體重(以公斤為單位)除以身高(以米為單位)的平方值得到。編寫程序,提示用戶輸人體重(磅為單位)以及身髙(英寸為單位),然后顯示BMI。注意:一磅是0.45359237公斤,一英寸是 0.0254 米
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);
System.out.print("Input height in inches: ");
double height = scanner.nextDouble();
System.out.print("Input weight in pounds: ");
double weight = scanner.nextDouble();
double BMI = (weight * 0.45359237)/Math.pow(height * 0.0254, 2);
System.out.printf("Your BMI is " + BMI);
}
}
Input height in inches: 50
Input weight in pounds: 95.5
Your BMI is 26.857257942215885
(幾何:兩點(diǎn)間距離)編寫程序,提示用戶輸入兩個(gè)點(diǎn)(x1,y1)和(x2,y2),然后顯示兩點(diǎn)間的距離。計(jì)算兩點(diǎn)間距離的公式是 ( x 2 ? x 1 ) 2 ? ( y 2 ? y 1 ) 2 \sqrt{(x_2 - x_1)^2-(y_2 - y_1)^2} (x2??x1?)2?(y2??y1?)2 ?。注意,可以使用Math.pow(a, 0.5)來計(jì)算$\sqrt{a}
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);
System.out.print("Input x1, x2, y1, y2 accordingly: ");
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double y2 = scanner.nextDouble();
System.out.print(Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5));
}
}
Input x1, x2, y1, y2 accordingly: 1.5 4 -3.4 5
8.764131445842194
(幾何:六邊形面積)編寫程序,提示用戶輸入六邊形的邊長,然后顯示它的面積。計(jì)算六邊形面積的公式是: 面積 = 3 3 2 s 2 \frac{3\sqrt3}{2}s^2 233 ??s2 , s 就是邊長。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Input side: ");
double side = input.nextDouble();
System.out.print("The area is " + ((3 * Math.pow(3, 0.5))/2) * side * side);
input.close();
}
}
Input side: 5.5
The area is 78.59180539343781
(科學(xué):風(fēng)寒溫度)外面到底有多冷?只有溫度是不足以提供答案的,包括風(fēng)速、相對(duì)濕度以及陽光等其他的因素在確定室外是否寒冷方面都起了很重要的作用。2001年,國家氣象服務(wù)(NWS)利用溫度和風(fēng)速計(jì)算新的風(fēng)寒溫度,來衡量寒冷程度,計(jì)算公式如下所示:
t w c = 35.74 + 0.6215 t a ? 35.75 v 0 . 16 + 0.4275 t a v 0 . 16 t_{wc} = 35.74 + 0.6215t_a - 35.75v^0.16 + 0.4275t_av^0.16 twc?=35.74+0.6215ta??35.75v0.16+0.4275ta?v0.16
這里的 t a t_a ta?是室外的溫度,以華氏攝氏度為單位,而v是速度,以每小時(shí)英里數(shù)為單位。 t w c t_{wc} twc?是風(fēng)寒溫度。該公式不適用于風(fēng)速低于2mph,或溫度在-58以下或41以上的情況。
編寫程序,提示用戶輸入在-58和41之間的度數(shù),同時(shí)大于或等于2的風(fēng)速,然后顯示風(fēng)寒溫度。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter temperature(Fahrenheit, between -58 and 41), and wind spped(>= 2): ");
double temp = input.nextDouble();
double speed = input.nextDouble();
System.out.print("The wind chill index is " + (35.74 + 0.6215 * temp - 35.75 * Math.pow(speed, 0.16) + 0.4275 * temp * Math.pow(speed, 0.16)));
input.close();
}
}
Enter temperature(Fahrenheit, between -58 and 41), and wind spped(>= 2): 5.3 6
The wind chill index is -5.567068455881625
(打印表格)編寫程序,顯示下面的表格。將浮點(diǎn)數(shù)值類型轉(zhuǎn)換為整數(shù)。
public class Main{public static void main(String[] args){System.out.print("a");
System.out.print(" " + "b");
System.out.println(" " + "pow(a,b)");
int a = (int)(Math.pow(1, 2));
System.out.print(1);
System.out.print(" " + 2);
System.out.println(" " + a);
int b = (int)(Math.pow(2, 3));
System.out.print(2);
System.out.print(" " + 3);
System.out.println(" " + b);
int c = (int)(Math.pow(3, 4));
System.out.print(3);
System.out.print(" " + 4);
System.out.println(" " + c);
int d = (int)(Math.pow(4, 5));
System.out.print(4);
System.out.print(" " + 5);
System.out.println(" " + d);
int e = (int)(Math.pow(5, 6));
System.out.print(5);
System.out.print(" " + 6);
System.out.println(" " + e);
}
}
a b pow(a,b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625
**(幾何:三角形的面積)編寫程序,提示用戶輸入三角形的三個(gè)點(diǎn)(x1,y1), (x2,y2)和(x3.y3), 然后顯示它的面積。計(jì)算三角形面積的公式是: **
s= (邊1+邊2+邊3)/2
面積 = s ( s ? 邊 1 ) ( s ? 邊 2 ) ( s ? 邊 3 ) \sqrt{s(s-邊1)(s-邊2)(s-邊3)} s(s?邊1)(s?邊2)(s?邊3) ?
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter 3 points of an triangle: ");
//下面的6個(gè)數(shù)據(jù)輸入是分先后順序的
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double line12 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double line13 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
double line23 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double s = (line12 + line13 + line23) / 2;
double area = Math.pow(s * (s - line12) * (s - line23) * (s - line13), 0.5);
System.out.print("The area of this triangle is " + area);
input.close();
}
}
Enter 3 points of an triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of this triangle is 33.600000000000016
(財(cái)務(wù)應(yīng)用程序:計(jì)算利息)如果知道收支余額和年利率的百分比,就可以使用下面的公式計(jì)算下個(gè)月要支付的利息額: 編寫程序,讀取收支余額和年百分利率,顯示兩個(gè)版本的下月利息。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interest rate: ");
double balance = input.nextDouble();
double intRate = input.nextDouble();
System.out.print(balance * (intRate/1200));
input.close();
}
}
Enter balance and interest rate: 1000 3.5
2.916666666666667
(財(cái)務(wù)應(yīng)用:計(jì)算未來投資值)編寫程序,讀取投資總額、年利率和年數(shù),然后使用下面的公式顯示未來投資金額
未來投資金額 = 投遞總額 x ( 1 + 月利率 年數(shù) ? 12 {1 + 月利率}^{年數(shù) * 12} 1+月利率年數(shù)?12)
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interest rate: ");
double investment = input.nextDouble();
double annualrate = input.nextDouble();
double years = input.nextDouble();
//利率要接著/100
System.out.print(investment * Math.pow(1 + annualrate / 12 / 100, years * 12));
input.close();
}
}
Enter balance and interest rate: 1000.56
4.25
1
1043.9219854307503
(財(cái)務(wù)應(yīng)用:貨幣單位)改寫程序淸單2-10, 解決將double型值轉(zhuǎn)換為int型值時(shí)可能會(huì)造成精度損失的問題。輸人的輸人值是一個(gè)整數(shù),其最后兩位代表的是美分幣值。例如:1156 就表示的是11美元56美分
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter total money as penny: ");
int penny = input.nextInt();
int dollar = penny / 100;
int remainingPenny = penny % 100;
System.out.print("You have " + dollar + " dollars and " + remainingPenny + " pennies");
input.close();
}
}
Enter total money as penny: 1156
You have 11 dollars and 56 pennies
(駕駛費(fèi)用)編寫一個(gè)程序,提示用戶輸人駕駛的距離、以每加侖多少英里的汽車燃油性能,以及每加侖的價(jià)格 ,然后顯示旅程的費(fèi)用
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter distance: ");
double distance = input.nextDouble();
System.out.print("Enter gallons: ");
double gallons = input.nextDouble();
System.out.print("Enter price: ");
double price = input.nextDouble();
System.out.print("Your cost will be " + distance / gallons * price);
input.close();
}
}
Enter distance: 900.5
Enter gallons: 25.5
Enter price: 3.55
Your cost will be 125.36372549019607
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧