Java how to program(第六版)第五章_第1页
Java how to program(第六版)第五章_第2页
Java how to program(第六版)第五章_第3页
Java how to program(第六版)第五章_第4页
Java how to program(第六版)第五章_第5页
已阅读5页,还剩12页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第五章 控制语句(第部分)5.5 计数控制器的4个所需要素为:1)一个控制器2)控制器的初始值3)用于修改控制变量的增量或减量4)循环继续条件5.6 while是先判断后执行,如果条件不满足,则一次循环体语句也不执行。for(表达式1;表达式2;表达式3) 第一步,计算表达式1的值。第二步,计算表达式2的值。若值为真(非0)则执行循环体一次,否则跳出循环。第三步,计算表达式3的值,转回第二步重复执行5.7 dowhile和while的区别就是前者至少执行一次,也就是对于至少执行一次的程序使用前者很好。两种循环基本没有区别。5.8 break是结束循环 continue是结束本次循环。就是这么简

2、单。具体的相见书本。5.9 a)逗号应该为分号,修改后为:for ( i=100; i=1; i+ )System.out.println(i);b)如果为偶数是会出现:Even integer Odd integer如果奇数会:Odd integer修改后为:switch ( value % 2 )case 0:System.out.print(Even integer);break;case 1:System.out.println(Odd integer);break;c)自增条件错误,修改后的程序为:for ( i=19; i=1; i-=2 )System.out.println(i

3、);d)判断的条件错误,修改后的程序为:counter = 2;doSystem.out.println(counter);counter += 2;while ( counter = 100 );5.10程序输出结果如下,即可看出:5.11程序代码为:import java.util.Scanner;public class Exercise1 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in

4、);int min;int number;int n;int i = 1;System.out.println(Enter the number you want to input:);n = input.nextInt();System.out.printf(Enter %d numbers:n, n);number = input.nextInt();min = number;while ( i n )number = input.nextInt();if ( number min )min = number;i+;System.out.printf(The minimum number

5、is %d!, min);5.12程序代码如下:public class Exercise2 /* * param args */public static void main(String args) / TODO Auto-generated method stubint n = 1;int i;for ( i=1; i=15; i+=2 )n *= i;System.out.printf(The mutiplication is :%d, n);5.13程序代码为:public class Exercise2 /* * param args */public static void ma

6、in(String args) / TODO Auto-generated method stubint factorial = 1;int n;System.out.printf(%-5s%10sn,N,Factorial);for ( n=1; n=5; n+ )factorial *= n;System.out.printf(%-5d%-10dn, n, factorial);不能求20的阶乘是因为int型的存储有限,20的阶乘超过了存储的最大值。5.14修改后的程序为:public class Interest public static void main( String args

7、) double amount; / amount on deposit at end of each year double principal = 1000.0; / initial amount before interest double rate; / interest rate / display headers System.out.printf( %s%20sn, Year, Amount on deposit ); for ( rate=5; rate=9; rate+ ) rate /= 100; / calculate amount on deposit for each

8、 of ten years for ( int year = 1; year = 10; year+ ) / calculate new amount for specified year amount = principal * Math.pow( 1.0 + rate, year ); / display the year and the amount System.out.printf( %4d%,20.2fn, year, amount ); / end for / end main / end class Interest5.15a)程序代码为:public class Exerci

9、se2 /* * param args */public static void main(String args) / TODO Auto-generated method stubint i;int j;int n = 10;for ( i=1; i=n; i+ )for ( j=1; j=1; i- )for ( j=1; j=1; i- )for ( j=n-i; j=1; j- )System.out.print( );for ( j=1; j=i; j+ )System.out.print(*);System.out.println();d)程序代码为:public class E

10、xercise2 /* * param args */public static void main(String args) / TODO Auto-generated method stubint i;int j;int n = 10;for ( i=1; i=1; j- )System.out.print( );for ( j=1; j=i; j+ )System.out.print(*);System.out.println();5.16程序代码为:import java.util.Scanner;public class Exercise2 /* * param args */pub

11、lic static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);int i = 1;int j;int n = 5;int number;System.out.println(Enter 5 numbers between 1 and 30!);System.out.printf(Enter the %dth number :, i);number = input.nextInt();for ( j=1; j=number; j+ )System.

12、out.print(*);System.out.println();while ( i = n-1 )System.out.printf(Enter the %dth number :, i+1);number = input.nextInt();for ( j=1; j=number; j+ )System.out.print(*);System.out.println();i+;5.17程序代码为:import java.util.Scanner;public class Market private double price;private int serialNumber;privat

13、e int productAmount;private double amountMoney = 0.0;public double productPrice( int serialNumber )switch ( serialNumber )case 1:price = 2.98;break;case 2:price = 4.50;break;case 3:price = 9.98;break;case 4:price = 4.49;break;case 5:price = 6.87;break;return price;public void displayMessage()Scanner

14、 input = new Scanner( System.in );int number = 1;System.out.printf(Enter the serial number and sale of the product:);serialNumber = input.nextInt();productAmount = input.nextInt();amountMoney += productAmount*productPrice(serialNumber);while ( number 5 )System.out.printf(Enter the serial number and

15、sale of the product:);serialNumber = input.nextInt();productAmount = input.nextInt();amountMoney += productAmount*productPrice(serialNumber);number+;System.out.printf(The total sale money is %.3f, amountMoney);测试如下:public class MarketTest /* * param args */public static void main(String args) / TODO

16、 Auto-generated method stubMarket market = new Market();market.displayMessage();5.18程序与原程序基本无差别,在这儿就不写出了。5.19 a)显示:true b)显示:false c)显示:true d)显示:false e)显示:true f)显示:false g)显示:false5.20程序如下:public class Exercise1_1 /* * param args */public static void main(String args) / TODO Auto-generated method

17、 stubint n = 3;int i;int sign = 1;double pi = 0.0;System.out.printf(%s%10sn,Item Number,PI);for ( i=1; i=(2*n-1); i+=2 )pi += 1.0*sign/i;sign = -sign;System.out.printf(%d%10.3fn, (i+1)/2, pi*4);对于估算,可以试着来,也可以用程序设计用。这儿就不在赘述。5.21程序代码为:public class Exercise1_1 /* * param args */public static void main(

18、String args) / TODO Auto-generated method stubint side1;int side2;int hypotenuse; System.out.printf(%s%10s%10sn,side1,side2,hypotenuse); for ( hypotenuse=1; hypotenuse=500; hypotenuse+ ) for ( side1=1; side1hypotenuse; side1+ ) for ( side2=1; side2side1; side2+ ) if ( hypotenuse*hypotenuse = side1*s

19、ide1+side2*side2) System.out.printf(%d%10d%10dn, side1, side2, hypotenuse); 5.22程序代码为:public class Exercise1_1 /* * param args */public static void main(String args) / TODO Auto-generated method stubint i;int j;int n = 10;for ( i=1; i=n; i+ )for ( j=1; j=i; j- )System.out.print( );for ( j=n-i+1; j=1

20、; j- )System.out.print(*);for ( j=1; j=1; j- )System.out.print(*);for ( j=n+5; j=i; j- )System.out.print( );for ( j=1; j=i; j+ )System.out.print(*);System.out.println();5.23程序按照要求容易写,故不再赘述。5.24程序代码为:public class Exercise1_2 /* * param args */public static void main(String args) / TODO Auto-generated

21、 method stubint i;int j;int n =5;for ( i=1; i=n; i+ )for ( j=1; j=n-i; j+ )System.out.print( );for ( j=1; j=1; i- )for ( j=1; j=n-i; j+ )System.out.print( );for ( j=1; j=(2*i-1); j+ )System.out.print(*);System.out.println();5.25程序代码为:import java.util.Scanner;public class Exercise1_2 /* * param args

22、*/public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int i;int j;int n;System.out.println(Enter a odd number between 1 and 19:);n =input.nextInt();System.out.println(The graph is :);for ( i=1; i=n; i+ )for ( j=1; j=n-i; j+ )System.out.print

23、( );for ( j=1; j=1; i- )for ( j=1; j=n-i; j+ )System.out.print( );for ( j=1; j=(2*i-1); j+ )System.out.print(*);System.out.println();5.26程序为:public class BreakTest public static void main( String args ) int count; / control variable also used after loop terminates for ( count = 1; count = 10; count+

24、 ) / loop 10 times if ( count 5 ) / if count is 5, System.out.printf( %d , count ); / terminate loop / end for System.out.printf( nBroke out of loop at count = %dn, (count+1)/3 ); / end main / end class BreakTest5.27程序结果为:*5.28修改后的程序为:public class ContinueTest public static void main( String args )

25、for ( int count = 1; count 2) display(-x); public static void main(String args) Christmas c=new Christmas(); for(int i=1;i=12;i+) switch(i) case 1: System.out.println(On the first day of Christmasnmy true love sent to me:); c.display(i); break; case 2: System.out.println(On the second day of Christmasnmy true love sent to me:); c.display(i); break; case 3: System.out.println(On the third day of Christmasnmy true love sent to me:); c.display(i); break; case 4: System.out.println(On the foruth day of Christmasnmy true love sent to me:); c.display(i); break; case 5: System.out.println(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论