




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第六章 方法的深入剖析6.7結(jié)果如下:a)7.5b)7.0c)0.0d)0.0e)6.4f)6.0g)-14.06.8程序代碼為:import java.util.Scanner;public class Exercise1_2 public static void main( String args) Exercise1_2 e = new Exercise1_2(); Scanner input = new Scanner( System.in ); int time; double account = 0.0; System.out.println("Enter the par
2、k time or end with -1:"); time = input.nextInt(); while ( time != -1 ) System.out.printf("Charge with $%.3fn", e.calculateCharge( time ); account += e.calculateCharge( time ); System.out.println("Enter the park time or end with -1:"); time = input.nextInt(); System.out.print
3、f("The total park money is $%.3f.", account); public double calculateCharge( int time )double charge;if ( time <= 3 )charge = 2.00;else if ( time <= 24 )charge = 2.00+(time-3)*0.50;elsecharge = 10.00;return charge;6.9程序?yàn)椋篿mport java.util.Scanner;public class Exercise1 /* * param args
4、 */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);double number;int result;System.out.println("Enter a number :");number = input.nextDouble();result = (int)Math.floor(number+0.5);System.out.printf("Before %.3fnAfter %dn&quo
5、t;, number, result);6.10程序代碼為:import java.util.Scanner;public class Exercise1 /* * param args */public static void main(String args) / TODO Auto-generated method stubExercise1 e = new Exercise1();Scanner input = new Scanner(System.in);double number;System.out.println("Enter a number :");nu
6、mber = input.nextDouble();System.out.printf("The original number is %.3fn", number); System.out.printf("Rounding to the integer is %dn",e.roundToInteger(number);System.out.printf("Rounding to the tenths is %.1fn", e.roundToTenths(number);System.out.printf("Rounding
7、 to the hundredths is %.2fn", e.roundToHundredths(number);System.out.printf("Rounding to the thousands is %.3fn",e.roundToThousandsths(number);public int roundToInteger( double number )return (int)Math.floor(number+0.5);public double roundToTenths( double number )return Math.floor(num
8、ber*10+0.5)/10;public double roundToHundredths( double number )return Math.floor(number*100+0.5)/100;public double roundToThousandsths( double number )return Math.floor(number*1000+0.5)/1000;6.11 a)隨機(jī)意味著公平的原則,即概率相等b)屬于計(jì)算機(jī)隨機(jī)取值,也就保證了游戲的公平性c)為了符合實(shí)際情況,故需要縮放或變換random的值d)由于計(jì)算機(jī)計(jì)算的隨機(jī)性和公平性對(duì)于解決一些實(shí)際問(wèn)題更方便6.12 a
9、) n = 1 + randmNumbers.nextInt(2);b)n = 1 + randmNumbers.nextInt(100);c)n = 1 + randmNumbers.nextInt(9);d)n = 1000 + randmNumbers.nextInt(112);e)n = -1 + randmNumbers.nextInt(2);f)n = -3 + randmNumbers.nextInt(14);6.13語(yǔ)句如下:a)n = 2*(randmNumbers.nextInt(5)+1);b)n = 2*(randmNumbers.nextInt(5)+1)+1;c)n
10、 = 4*(randmNumbers.nextInt(5)+1)+2;6.14程序?yàn)椋篿mport java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int base;int exponent;int result;System.out.println("Enter base and exponen
11、t number :");base = input.nextInt();exponent = input.nextInt();result = integerPower( base, exponent );System.out.println("The integer power is :" +result);public static int integerPower( int base , int exponent )int i;int result = 1;for ( i=1; i<=exponent; i+ )result *= base;retur
12、n result;6.15程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );double side1;double side2;double hyp;System.out.println("Enter side1 and side2:");side1 = inpu
13、t.nextDouble();side2 = input.nextDouble();hyp = hypotenuse( side1, side2 );System.out.printf("The hypotenuse is :%.2f", hyp);public static double hypotenuse( double side1, double side2 )double hyp;hyp = Math.pow(side1*side1+side2*side2), 0.5);return hyp;6.16程序代碼為:import java.util.Scanner;p
14、ublic class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num1;int num2;int result;System.out.println("Enter number1 and number2:");num1 = input.nextInt();num2 = input.nextInt();result = mutipl
15、e( num1, num2 );if ( result = 1 )System.out.printf("%d is %d mutiple.", num1, num2 );elseSystem.out.printf("%d is not %d mutiple.", num1, num2 );public static int mutiple( int number1, int number2 )if ( number1 % number2 = 0 )return 1;else return 0;6.17程序代碼為:public class Exercise
16、5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num;int result;System.out.println("Enter number:");num = input.nextInt();while ( num != -1 ) result = isEven( num );if ( result = 1 )System.out.printf(&quo
17、t;%d is an even number.", num );elseSystem.out.printf("%d is an odd number.", num );System.out.println("Enter number:");num = input.nextInt();public static int isEven( int number )if ( number % 2 = 0 )return 1;else return 0;6.18程序代碼為:import java.util.Scanner;public class Exe
18、rcise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num;System.out.println("Enter number:");num = input.nextInt();System.out.println("The result is :");squareOfAsterisks( num );public static v
19、oid squareOfAsterisks( int number )int i;int j;for ( i=1; i<=number; i+ )for ( j=1; j<=number; j+ )System.out.print('*');System.out.println();6.19程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method st
20、ubScanner input = new Scanner( System.in );int num;System.out.println("Enter number:");num = input.nextInt();System.out.println("The result is :");fillCharacter( num );public static void fillCharacter( int number )int i;int j;for ( i=1; i<=number; i+ )for ( j=1; j<=number;
21、j+ )System.out.print('#');System.out.println();6.20程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );double radius;System.out.println("Enter radius:"
22、);radius = input.nextDouble();System.out.printf("The area is :%.2f", circleArea( radius );public static double circleArea( double radius )return Math.PI*radius*radius;6.21程序代碼為:public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubSc
23、anner input = new Scanner( System.in );int number;System.out.println("Enter a number:");number = input.nextInt();System.out.println("The result is :");displayDigitals( number );public static void displayDigitals( int number )int a;int b;int c;int d;int e;a = number/10000;b = (num
24、ber/1000)%10;c = (number/100)%10;d = (number/10)%10;e = number%10;if ( a > 0 )System.out.printf("%-2d", a);System.out.printf("%-2d", b);System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( b > 0 )Syst
25、em.out.printf("%-2d", b);System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( c > 0 )System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( d
26、 > 0 )System.out.printf("%-2d", d);System.out.printf("%-2d", e);elseSystem.out.printf("%-2d", e);6.22程序代碼為:import java.util.Scanner;public class Exercise1_3 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Sc
27、anner( System.in );double temperature;int temp;System.out.println("Enter 1 chage fahrenheit to Celsius");System.out.println("Enter 2 chage Celsius to fahrenheit");temp = input.nextInt();System.out.println("Enter the temperature:");temperature = input.nextDouble();if ( t
28、emp = 1 )celsius( temperature );elsefahrenheit( temperature );public static void celsius( double temperature )double degree;degree = 5.0/9.0*(temperature-32);System.out.printf("After changing to celsius,temperature is %.2f", degree);public static void fahrenheit( double temperature )double
29、 degree;degree = 9.0/5.0*temperature+32;System.out.printf("After changing to fahrenheit,temperature is %.2f", degree);6.23程序代碼為:import java.util.Scanner;public class Exercise1_2 public static void main( String args) Exercise1_2 e = new Exercise1_2();Scanner input = new Scanner( System.in ); double num1;double num2;double num3;double min;System.out.println("Enter three numbers:");num1 = input.nextDouble();num2 = input.nextDouble();num3 = input.nextDouble();min = e.minimum3( num1, num2, num3 );System.out.printf("The minimum number is :%.3f"
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 上海建設(shè)管理職業(yè)技術(shù)學(xué)院《外匯交易模擬》2023-2024學(xué)年第二學(xué)期期末試卷
- 福州英華職業(yè)學(xué)院《園林建筑設(shè)計(jì)Ⅱ》2023-2024學(xué)年第二學(xué)期期末試卷
- 寧夏幼兒師范高等??茖W(xué)?!陡苯虒W(xué)訓(xùn)練理論與實(shí)踐(1)》2023-2024學(xué)年第二學(xué)期期末試卷
- 北京理工大學(xué)《建筑消防設(shè)備工程》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安航空職業(yè)技術(shù)學(xué)院《自動(dòng)控制原理B實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 濰坊食品科技職業(yè)學(xué)院《德育原理》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣州涉外經(jīng)濟(jì)職業(yè)技術(shù)學(xué)院《意識(shí)形態(tài)前沿問(wèn)題研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 民辦合肥財(cái)經(jīng)職業(yè)學(xué)院《精神病護(hù)理學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 贛州師范高等專科學(xué)?!睹嫦?qū)ο蟪绦蛟O(shè)計(jì)-JAVA語(yǔ)言》2023-2024學(xué)年第二學(xué)期期末試卷
- 天津外國(guó)語(yǔ)大學(xué)《原理與應(yīng)用實(shí)訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 危險(xiǎn)源辨識(shí)、風(fēng)險(xiǎn)評(píng)價(jià)、風(fēng)險(xiǎn)控制措施清單-05變電站工程5
- 水泵采購(gòu)?fù)稑?biāo)方案(技術(shù)標(biāo) )
- 高三數(shù)學(xué)《最后一課》(課件)
- 2023學(xué)年完整公開(kāi)課版周培源
- 遼寧省2017定額費(fèi)用標(biāo)準(zhǔn)
- 五月天《干杯》歌詞
- 肺結(jié)核診療規(guī)范內(nèi)科學(xué)診療規(guī)范診療指南2023版
- 四川省高等教育自學(xué)考試畢業(yè)生登記表【模板】
- 全建筑工程綠色施工技術(shù)指導(dǎo)手冊(cè)
- 請(qǐng)對(duì)自己的心理做一個(gè)簡(jiǎn)要分析-2
評(píng)論
0/150
提交評(píng)論