考试酷java 第二章

合集下载

Java入门经典第2章动手写Hello World知识点练习答案及课后习题答案

Java入门经典第2章动手写Hello World知识点练习答案及课后习题答案

第2章 动手写“Hello World”并不是每台计算机都可以运行Java程序,要运行Java程序,计算机必须搭建Java开发环境,而编写Java程序则可以使用任何的文本编辑工具,如最简单的文本编辑工具之一——记事本。

搭建Java开发环境是每一位Java程序员必须掌握并熟练使用的一项技能。

本章为大家介绍接触Java的第一步——搭建Java开发环境。

知识点:1、并不是每台计算机都可以运行Java程序,要运行Java程序,计算机必须搭建Java开发环境,而编写Java程序则可以使用任何的文本编辑工具,如最简单的文本编辑工具之一——记事本。

练习:1、并不是每台计算机都可以运行Java程序,要运行Java程序,计算机必须搭建 ,而编写Java程序则可以使用任何的 ,如最简单的文本编辑工具之一—— 。

通过本章的学习,希望读者可以达到以下的学习目的:□ 学会下载JDK。

□ 掌握如何安装JDK。

□ 掌握如何在Windows系统下配置和测试JDK。

□ 学会使用记事本编写Java程序。

□ 学会编译和运行Java程序。

2.1 搭建Java开发环境所谓“工欲善其事,必先利其器”,在学习一门语言之前,首先需要把整个开发环境搭建好。

要编译和执行Java程序,JDK是必备的。

下面将具体介绍下载并安装JDK及配置环境变量的方法。

2.1.2 下载JDK由于Sun公司已经被Oracle收购,因此JDK可以在Oracle公司的官方网站(/index.html)下载。

下面以目前最新版本JDK 7 Update 3为例介绍下载JDK的方法,具体下载步骤如下。

Step(1)打开IE浏览器,在地址栏中输入Oracle公司网站地址“/index.html”,并按下<Enter>键,进入到图2.1所示的Oracle官方网站主页面。

单击Oracle主页中的“Downloads”选项卡,然后在“Popular Downloads”栏目中单击“Java for Developers”超链接,这样就进入到Java SE相关资源下载页面。

java语言程序设计第二章编程练习题(1-11)

java语言程序设计第二章编程练习题(1-11)

2.1import java.util.Scanner;public class eserciseII1{public static void main(String[] args){double F;//华氏温度值double C;//摄氏温度值//创建输入Scanner input=new Scanner(System.in);System.out.println("请输入摄氏温度值:\n");//控制台打印API//引入控制台输入C=input.nextDouble();System.out.println("华氏温度值为:\n");//控制台打印APISystem.out.println(F=((double)9/5)*C+32);//控制台打印API}}--------------------------------------------------------------------------------------------------------------------------------- 2.2import java.util.Scanner;/** 读入圆柱半径和高,并计算其体积;* 圆柱体积= 底面积* 高* 底面积= 半径*半径*PI(约为3.14)*/public class exerciseII2{public static void main(String[] args){final double PI=3.14;double R , H , V;//创建R输入Scanner inputR=new Scanner(System.in);System.out.println("输入半径:");//控制台打印APIR=inputR.nextDouble();///创建H输入Scanner inputH=new Scanner(System.in);System.out.println("输入圆柱高:");//控制台打印APIH=inputH.nextDouble();V=H*R*R*PI;System.out.println("圆柱体积为:"+V);//控制台打印API}}---------------------------------------------------------------------------------------------------------------------------------2.3import java.util.Scanner;/** 英尺与米的换算* 1英尺=0.305米*/public class exerciseII3{public static void main(String[] args){//创建输入Scanner input=new Scanner(System.in);System.out.println("输入英尺数");//控制台打印APIdouble 英尺=input.nextDouble();double 米=英尺*0.305;System.out.println(米);//控制台打印API}}---------------------------------------------------------------------------------------------------------------------------------2.4import java.util.Scanner;/** 将磅转换为千克* 1磅=0.454千克*/public class exerciseII4{public static void main(String[] args){//创建输入;Scanner input=new Scanner(System.in);System.out.println("输入磅数:\n");//控制台打印APIdouble 磅数=input.nextDouble();double 千克=磅数*0.454;System.out.println(千克);//控制台打印API}}---------------------------------------------------------------------------------------------------------------------------------2.5import java.util.Scanner;/** 总费用=费用*(1+酬金率)*/public class exerciseII5{public static void main(String[] args){//创建费用输入Scanner input费用=new Scanner(System.in);System.out.println("输入费用:\n");//控制台打印APIdouble 费用=input费用.nextDouble();//创建酬金率输入Scanner input酬金率=new Scanner(System.in);System.out.println("输入酬金率,例如15%输入15:\n");//控制台打印APIdouble 酬金率=input酬金率.nextDouble();double 总费用=费用*(1+酬金率/100);System.out.println(总费用);//控制台打印API}}--------------------------------------------------------------------------------------------------------------------------------- 2.6import java.util.Scanner;/** 一个数(0,100),并计算各个位置和*/public class exerciseII6{public static void main(String[] args){//创建输入Scanner input=new Scanner(System.in);System.out.println("输入一个数(0,1000):\n");//控制台打印APIint 数=input.nextInt();int 个位,百位,千位,余,加和;千位=数/100;余=数%100;百位=余/10;余=余%10;个位=余;加和=个位+百位+千位;System.out.println(加和);//控制台打印API}}--------------------------------------------------------------------------------------------------------------------------------- 2.7import java.util.Scanner;public class exerciseII7{public static void main(String[] args){//创建输入Scanner input=new Scanner(System.in);System.out.println("输入分钟数:\n");//控制台打印APIdouble M=input.nextDouble();double tian=M/(60*24);System.out.println(tian);//控制台打印APIdouble nian=tian/365;System.out.println(nian);//控制台打印API}}--------------------------------------------------------------------------------------------------------------------------------- 2.8import java.util.Scanner;public class exerciseII8{public static void main(String[] args){//创建输入Scanner input=new Scanner(System.in);System.out.println("输入ASCII值:\n");//控制台打印APIint num=input.nextInt();char ch=(char)num;System.out.println(ch);//控制台打印API}}--------------------------------------------------------------------------------------------------------------------------------- 2.9import java.util.Scanner;public class exerciseII9{public static void main(String[] args){//创建输入Scanner input = new Scanner(System.in);System.out.println("输入数值,例如:$11.56:\n");//控制台打印APIdouble shu=input.nextDouble();int num=(int)(shu*100);int yuan=num/100;int yu=num%100;System.out.println("你输入的是"+yuan+"元"+yu+"分");//控制台打印API }}--------------------------------------------------------------------------------------------------------------------------------- 2.10import javax.swing.JOptionPane;public class exerciseII10{public static void main(String[] args){//创建对话框输入;String input = JOptionPane.showInputDialog("Enter an number of double");//String向其它类型转换;double 双= Double.parseDouble(input);//创建输出字符串;String output = "输出" + 双;//创建对话框输出;JOptionPane.showMessageDialog(null, output);}}--------------------------------------------------------------------------------------------------------------------------------- 2.11(对话框输入输出)import javax.swing.JOptionPane;public class exerciseII11控制台{public static void main(String[] args){//创建对话框输入String input = JOptionPane.showInputDialog("请输入姓名");String input1= JOptionPane.showInputDialog("请输入工作时间,例如:30");String input2= JOptionPane.showInputDialog("请输入工资,例如:2000000.1");String input3= JOptionPane.showInputDialog("请输入联邦所得税率,例如:1.2%输入12");String input4= JOptionPane.showInputDialog("请输入州所得税率,例如:1.2%输入12");//String向其它类型转换,如果本身就是字符串型的变量,直接用String input = JOptionPane.showInputDialog("请输入姓名\n");中的input即可;int 工作时间= Integer.parseInt(input1);double 工资= Double.parseDouble(input2);double 联邦所得税率= Double.parseDouble(input3);double 州所得税率=Double.parseDouble(input4);//创建输出字符串;String output ="姓名:\n"+input+"\n工作时间:\n"+工作时间+"\n工资:\n"+工资+"\n联邦所得税率:\n"+联邦所得税率+"%"+"\n州所得税率:\n"+州所得税率+"%";//创建对话框输出;JOptionPane.showMessageDialog(null, output);}}--------------------------------------------------------------------------------------------------------------------------------- 2.11(控制台输入,输出)import java.util.Scanner;public class exerciseII11对话框{public static void main(String[] args){//创建姓名输入Scanner input= new Scanner(System.in);System.out.println("请输入姓名");//控制台打印APIString 姓名=input.next();//创建工作时间输入1Scanner input1= new Scanner(System.in);System.out.println("请输入工作时间,例如:30");//控制台打印APIString 工作时间=input1.next();//创建工资输入Scanner input2= new Scanner(System.in);System.out.println("请输入工资,例如:2000000.1");//控制台打印APIString 工资=input2.next();//创建联邦所得税率输入Scanner input3= new Scanner(System.in);System.out.println("请输入联邦所得税率,例如:1.2%输入12");//控制台打印APIString 联邦所得税率=input3.next();//创建输入Scanner input4= new Scanner(System.in);System.out.println("请输入州所得税率,例如:1.2%输入12");//控制台打印APIString 州所得税率=input4.next();System.out.println("姓名:\n"+姓名+"\n工作时间:\n"+工作时间+"\n工资:\n"+工资+"\n联邦所得税率:\n"+联邦所得税率+"%"+"\n州所得税率:\n"+州所得税率+"%");//控制台打印API}}。

2011java计算机等级考试二级第二章

2011java计算机等级考试二级第二章

Java二级真题第二章1、下列变量定义中,不合法的是()A int xB int _123C int Summer_20010_gross_salD int #dim2、下列变量名的定义中,符合java命名约定的是()A filednameB superC IntnumD $ number3、下列符合java命名约定的是()A package com.Bi.hrB public class xyzC int ID void setCustomerName()4、下列标识符(名字)命名规则中,正确的是()A 类名的首字母小写B 变量和方法名的首字母大写C 接口名的首字母小写D常量名完全大写5、java中常量的保留字是()A constB finalC finallyD native6、下列关键字中可以表示常量的是()A finalB defaultC privateD transient7、下列数中为八进制的是()A 27B 0×25C 026D 0288、java中的基本数据类型int在不同的操作系统平台上的字长是()A不同的 B 32位 C 64位 D 16位9、给一个short类型变量赋值的范围是()A -128 ~ +127B -2 147 483 648 ~ +2 147 483 647C -32 768 ~+ 32 767D -1000 ~ +100010、下列语句中错误的是()A String[] = {"how","are"}B byte b=255C Strings="one"+"two"D int i =2+200011、下列语句中正确的是()A System.out.pritln(1+"1");B int I = 2+'2';C strings="on"+'one';D byte b=257;12、下列选项中为单精度数的是()A 2B 5.2C 0.2fD 02313、下列有关java布尔类型的描述中,正确的是()A 一种基本的数据类型,它的类型名称为booleanB 用ini表示类型C 其值可以赋给int类型的变量D 有两个值,1代表真,0代表假14、下列布尔型变量的定义中,正确且规范的是()A BOOLEAN canceled = false;B boolean canceled=false;C boolean CANCELED=false;D boolean canceled=FALSE;15、下列关于boolean类型的叙述中,正确的是()A 可以将boolean类型的数值转换为int类型的数值B 可以将boolean类型的数值转换为字符串集C 可以将boolean 类型的数值转换为char类型的数值D 不能将boolean类型的数值转换为其他基本数据类型16、在下列表达式中,类型可以作为int型的是()A "abc"+"efg"B "abc"+'efg'C 'a'+'b'D 3+"4"17、public class TypeTransition {public static void main(String[] args) {char a = 'a';int i = 100;long y = 456;int aa = a + i;long yy = y - aa;System.out.println("a=" + aa);System.out.println("yy=" + yy);}}运行结果是()A aa=197 yy=259B aa=177 yy=259C aa=543 yy=288D aa=197 yy=33318、下列属于java合法标识符的是()A "ABC"B &5678C +rriwoD saler19、下列代表十六进制整数的是()A 0123B 1 900C fa00D 0xa220、下列叙述中,正确的是()A java 语言的标识符是区分大小写的B 源文件名与public类名可以不相同C 源文件扩展名jarD 源文件中public 类的数目不限21、下列属于合法标识符的是()A _catB 5booksC +staticD -3.1415922、在java中,表示换行的转义符是()A \nB \fC 'n'D \dd23、char类型的取值范围是()A -27 ~ 27-1B 0 ~ 216-1C -215 ~ 215-1D 0~ 28-124、十进制数16的十六进制表示格式是()A 0x10B 0x16C 0xa D1625、int型的public成员变量MAX_LENGTH ,该值保持为常数100,则定义这个变量的语句是()A public int MAX_LENGTH =100B final int MAX_LENGTH =100C public const int MAX_LENGTH =100D public final int MAX_LENGTH =10026、按照java的标识符命名规范,下列表示一个类的标识符正确的是A HellowordB HelloWordC hellowordD helloWord填空题1、按照Java中的命名约定,方法名的起始字母一般都是小写,但___方法例外.2、在java中,所有数据类型的长度都是固定的,因此没有保留字___3、表达式(10*49.3)的类型是____4、java语言中的浮点数默认类型是____5、在java中,3.14159D 所表示____6、Java中的字符变量在内存中占_____位(bit)7、能打印出一个双引号的语句是System.out.println("___")8、在Java中,字符以16位的_____表示9、java中,转义符\n表示_____10、java对简单数据类型进行了类包装,int对应的包装类是____11、。

JAVA第二章 课后习题答案

JAVA第二章 课后习题答案

9.
编写程序,将十进制整数转换为二进制。 public class Tentotwo { public static void main(String[] args) { int a = 123; int remainder; int sum = 0; int k = 1; while(a != 0){ remainder = a %2; a /= 2; sum = sum + remainder * k; k *= 10; } //对目标数字求余 //对目标数字求商 //求和 //改变位数 //定义一个变量并赋给他一个十进制的值 //定义一个变量用于存储余数 //定义一个变量用于存放和 //定义一个变量控制位数
4
Hale Waihona Puke } }7.编写程序,求 100~999 之间所有的三位水仙花数。 (水仙花数是指一个 n 位数 ( n≥3 ),它的 每个位上的数字的 n 次幂之和等于它本身,例如:1^3 + 5^3+ 3^3 = 153) public class Shuixian { public static void main(String[] args) { int b1, b2, b3; for(int m=101; m<1000; m++) { b3=m/100; b2=m%100/10; b1=m%10; if ((b3*b3*b3+b2*b2*b2+b1*b1*b1)==m) System.out.println(m+"是一个水仙花数"); } } }
6
System.out.println("10 进制的 123 转换为 2 进制结果为:" + sum ); } }

java2实用教程试题

java2实用教程试题

第1章Java 语言入门1-1开发与运行Java程序需要经过哪些主要步骤和过程?1-2怎样区分应用程序和小应用程序?应用程序的主类或小应用程序的主类必须用public修饰吗?1-3Java程序是有什么组成的?一个程序中必须要有public类吗?Java源文件的命名规则是怎样的?1-4在运行小应用程序的html文件中可以使用codebase属性制定小应用程序的字节码所驻留的目录。

如果不使用codebase属性,小应用程序的字节码文件必须和运行它的html在同一目录中,编写一个小应用程序并将它的字节码存放在某个目录中,比如c:\Boy. 把运行该小应用程序的html文件(注意其中的codebase属性):< applet code=你的小程序的字节码wdith=20 height=30codebase=c:\boy> </applet>存放在另一个目录中(查阅有关编写网页方面的书籍,会有更详细的关于怎样在一个网页中嵌入一个小应用程序的讲解)。

第2章标识符、关键字和数据类型2-1上机运行下列程序,注意观察输出的结果。

public class E{ public static void main ( string args [ ] ){ for (int i=20302; i<=20302; i++){ System.out. println (( char ) i );}}2-2System.out. println (“你好”);可输出字符串值,也可以使用System.out. println()输出变量或表达式的值,只需使用并置符号:“+”将变量、表达式或一个常数值与一个字符串并置即可,如System.out. println(“”+x);System.out. println(“:”+123+“大于”+122)等。

上机调试下列程序,注意观察结果,特别注意System.out. print()和System.out. println()的区别。

java程序设计课后第二章习题程序答案

java程序设计课后第二章习题程序答案

public class xiti9{public static void main(String args[]) {int x=4;int y;if(x<1){y=x;}else if(x>=10){y=4*x;}else{y=3*x-2;}System.out.println("y="+y);}}习题12public class xiti12{public static void main(String args[]) {int sum=0;for(int k=1;k<=10;k++){sum=sum+k*k;}System.out.println("sum="+sum); }}习题13public class xiti13{public static void main(String args[]) {intt,a=3,b=5,c=8;if(a<b){t=a;a=b;b=t;}if(a<c){t=a;a=c;c=t;}{t=b;b=c;c=t;}System.out.println("大小顺序输出为:"+a+" "+b+" "+c); }}习题14public class xiti14{public static void main(String args[]){int n=1;System.out.print("\n1-100之间的所有素数为:\n"+" 3"); for(int i=1;i<=100;i++){for(int j=2;j<=i/2;j++){if(i%j==0){break;}if(j==i/2){System.out.print(" "+i);n++;}}}System.out.println("\n共有"+n+"个素数。

java第二章练习题答案

java第二章练习题答案

java第二章练习题答案Java第二章练习题答案Java作为一种广泛应用的编程语言,具有着强大的功能和丰富的库。

学习Java 的过程中,练习题是巩固知识和提高编程能力的重要一环。

在第二章的练习题中,我们将探索一些基本的Java概念和语法。

下面是第二章练习题的答案,希望对大家的学习有所帮助。

1. 问题:什么是Java的标识符?标识符的命名规则是什么?答案:Java的标识符是用来标识变量、方法、类等的名称。

标识符的命名规则如下:- 必须以字母、下划线或美元符号开头;- 可以包含字母、数字、下划线或美元符号;- 大小写敏感;- 不能使用Java的关键字。

2. 问题:Java的八种基本数据类型是什么?答案:Java的八种基本数据类型分别是:- byte:字节型;- short:短整型;- int:整型;- long:长整型;- float:单精度浮点型;- double:双精度浮点型;- char:字符型;- boolean:布尔型。

3. 问题:如何声明一个整型变量并赋初值为10?答案:可以使用以下语句声明一个整型变量并赋初值为10:int num = 10;4. 问题:如何将一个字符串转换为整型?答案:可以使用Integer类的parseInt()方法将一个字符串转换为整型。

示例代码如下:String str = "123";int num = Integer.parseInt(str);5. 问题:如何声明一个常量?答案:可以使用关键字final来声明一个常量。

示例代码如下:final int MAX_NUM = 100;6. 问题:如何使用if-else语句实现条件判断?答案:可以使用if-else语句实现条件判断。

示例代码如下:int num = 10;if (num > 0) {System.out.println("num是正数");} else {System.out.println("num是负数或零");}7. 问题:如何使用for循环实现重复执行某段代码?答案:可以使用for循环实现重复执行某段代码。

java第七版课后答案——第二章

java第七版课后答案——第二章

//******************************************************************** // AverageOfThree.java Author: Lewis/Loftus//// Solution to Programming Project 2.2//******************************************************************** import java.util.Scanner;public class AverageOfThree{//----------------------------------------------------------------- // Reads three integers from the user and prints their average.//----------------------------------------------------------------- public static void main <String[] args>{int num1, num2, num3;double average;Scanner scan = new Scanner <System.in>;System.out.print <"Enter the first number: ">;num1 = scan.nextInt<>;System.out.print <"Enter the second number: ">;num2 = scan.nextInt<>;System.out.print <"Enter the third number: ">;num3 = scan.nextInt<>;average = <double> <num1+num2+num3> / 3;System.out.println <"The average is: " + average>;}}//******************************************************************** // AverageOfThree2.java Author: Lewis/Loftus//// Alternate solution to Programming Project 2.2//******************************************************************** import java.util.Scanner;public class AverageOfThree2{//----------------------------------------------------------------- // Reads three integers from the user and prints their average.//----------------------------------------------------------------- public static void main <String[] args>{int num1, num2, num3;double average;Scanner scan = new Scanner <System.in>;System.out.print <"Enter three numbers: ">;num1 = scan.nextInt<>;num2 = scan.nextInt<>;num3 = scan.nextInt<>;average = <double> <num1+num2+num3> / 3;System.out.println <"The average is: " + average>;}}//******************************************************************** // Balloons.java Author: Lewis/Loftus//// Solution to Programming Project 2.17//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class Balloons extends JApplet{//----------------------------------------------------------------- // Draws a set of balloons on strings.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.white>;// draw the stringspage.setColor <Color.black>;page.drawLine <45, 95, 100, 300>;page.drawLine <90, 100, 100, 300>;page.drawLine <60, 100, 100, 300>;page.drawLine <122, 85, 100, 300>;page.drawLine <145, 115, 100, 300>;// draw the balloonspage.setColor <Color.blue>;page.fillOval <20, 30, 50, 65>;page.setColor <Color.yellow>;page.fillOval <70, 40, 40, 60>;page.setColor <Color.red>;page.fillOval <40, 50, 40, 55>;page.setColor <Color.green>;page.fillOval <100, 30, 45, 55>;page.setColor <Color.cyan>;page.fillOval <120, 55, 50, 60>;}}//******************************************************************** // BigDipper.java Author: Lewis/Loftus//// Solution to Programming Project 2.16//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class BigDipper extends JApplet{//----------------------------------------------------------------- // Draws the Big Dipper constellation and some other stars.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.black>;page.setColor <Color.white>;page.drawLine <100, 80, 110, 120>;page.drawLine <110, 120, 165, 115>;page.drawLine <165, 115, 175, 70>;page.drawLine <100, 80, 175, 70>;page.drawLine <175, 70, 245, 20>;page.drawLine <245, 20, 280, 30>;// Draw some extra starspage.fillOval <50, 50, 4, 4>;page.fillOval <70, 150, 3, 3>;page.fillOval <90, 30, 3, 3>;page.fillOval <220, 140, 4, 4>;page.fillOval <280, 170, 3, 3>;page.fillOval <310, 100, 4, 4>;page.fillOval <360, 20, 3, 3>;}}//******************************************************************** // BusinessCard.java Author: Lewis/Loftus//// Solution to Programming Project 2.20//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class BusinessCard extends JApplet{//----------------------------------------------------------------- // Draws a business card.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.yellow>;page.setColor <Color.red>;page.fillOval <10, 20, 60, 60>;page.setColor <Color.cyan>;page.fillRect <25, 35, 130, 25>;page.setColor <Color.black>;page.drawString <"James C. Kerplunk", 35, 50>;page.drawString <"President and CEO", 85, 80>;page.drawString <"Origin Software, Inc.", 85, 100>;page.drawLine <225, 20, 225, 80>;page.drawLine <195, 50, 255, 50>;page.setColor <Color.blue>;page.drawString <"where it all begins...", 115, 135>;}}//******************************************************************** // ChangeCounter.java Author: Lewis/Loftus//// Solution to Programming Project 2.10//******************************************************************** import java.util.Scanner;public class ChangeCounter{//----------------------------------------------------------------- // Computes the total value of a collection of coins.//----------------------------------------------------------------- public static void main <String[] args>{int quarters, dimes, nickels, pennies;int total, dollars, cents;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the number of quarters: ">;quarters = scan.nextInt<>;System.out.print <"Enter the number of dimes: ">;dimes = scan.nextInt<>;System.out.print <"Enter the number of nickels: ">;nickels = scan.nextInt<>;System.out.print <"Enter the number of pennies: ">;pennies = scan.nextInt<>;total = quarters * 25 + dimes * 10 + nickels * 5 + pennies;dollars = total / 100;cents = total % 100;System.out.println <"Total value: " + dollars + " dollars and " + cents + " cents.">;}//******************************************************************** // DrawName.java Author: Lewis/Loftus//// Solution to Programming Project 2.15//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class DrawName extends JApplet{//----------------------------------------------------------------- // Draws a name.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.cyan>;page.setColor <Color.black>;page.drawString <"John A. Lewis", 50, 50>;}}//******************************************************************** // FloatCalculations.java Author: Lewis/Loftus//// Solution to Programming Project 2.4//******************************************************************** import java.util.Scanner;public class FloatCalculations{//----------------------------------------------------------------- // Reads two floating point numbers and prints their sum,// difference, and product.//----------------------------------------------------------------- public static void main <String[] args>{float num1, num2;Scanner scan = new Scanner <System.in>;System.out.print <"Enter the first number: ">;num1 = scan.nextFloat<>;System.out.print <"Enter the second number: ">;num2 = scan.nextFloat<>;System.out.println <"Their sum is: " + <num1+num2>>;System.out.println <"Their difference is: " + <num1-num2>>;System.out.println <"Their product is: " + <num1*num2>>;}//******************************************************************** // Fraction.java Author: Lewis/Loftus//// Solution to Programming Project 2.13//******************************************************************** import java.util.Scanner;public class Fraction{//----------------------------------------------------------------- // Computes the floating point equivalent of a fraction.//----------------------------------------------------------------- public static void main <String[] args>{int numerator, denominator;float value;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the numerator: ">;numerator = scan.nextInt<>;System.out.print <"Enter the denominator: ">;denominator = scan.nextInt<>;value = <float> numerator / denominator;System.out.println <"Floating point equivalent: " + value>;}}//******************************************************************** // HousePicture.java Author: Lewis/Loftus//// Solution to Programming Project 2.19//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class HousePicture extends JApplet{//----------------------------------------------------------------- // Draws a house scene.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.cyan>;page.setColor <Color.gray>;page.fillRect <0, 200, 400, 50>; // groundpage.setColor <Color.blue>;page.fillRect <50, 125, 300, 100>; // housepage.setColor <Color.green>;page.fillRect <180, 175, 40, 50>; // doorpage.setColor <Color.yellow>;page.fillRect <100, 155, 40, 25>; // windowpage.fillRect <260, 155, 40, 25>; // windowpage.setColor <Color.black>;page.fillRect <40, 100, 320, 40>; // roofpage.fillOval <210, 200, 6, 6>; // doorknobpage.setColor <Color.red>;page.fillRect <80, 80, 20, 40>; // chimneypage.setColor <Color.darkGray>;page.fillOval <80, 60, 20, 20>; // smokepage.fillOval <85, 50, 15, 25>; // smokepage.fillOval <90, 45, 15, 20>; // smokepage.setColor <Color.white>;page.fillOval <200, 30, 80, 40>; // cloudpage.fillOval <230, 40, 80, 40>; // cloud}}//******************************************************************** // InfoParagraph.java Author: Lewis/Loftus//// Solution to Programming Project 2.3//******************************************************************** import java.util.Scanner;public class InfoParagraph{//----------------------------------------------------------------- // Reads information about a person and incorporates it into an// output paragraph.//----------------------------------------------------------------- public static void main <String[] args>{String name, age, college, pet;Scanner scan = new Scanner <System.in>;System.out.print <"What is your name? ">;name = scan.nextLine<>;System.out.print <"How old are you? ">;age = scan.nextLine<>;System.out.print <"What college do you attend? ">;college = scan.nextLine<>;System.out.print <"What is your pet's name? ">;pet = scan.nextLine<>;System.out.println <>;System.out.print <"Hello, my name is " + name + " and I am ">;System.out.print <age + " years\nold. I'm enjoying my time at ">; System.out.print <college + ", though\nI miss my pet " + pet>;System.out.println <" very much!">;System.out.println <>;}}//******************************************************************** // Lincoln4.java Author: Lewis/Loftus//// Solution to Programming Project 2.1//******************************************************************** public class Lincoln4{//----------------------------------------------------------------- // Prints a quote from Abraham Lincoln, including quotation// marks.//----------------------------------------------------------------- public static void main <String[] args>{System.out.println <"A quote by Abraham Lincoln:">;System.out.println <"\"Whatever you are, be a good one.\"">;}}//******************************************************************** // MilesToKilometers.java Author: Lewis/Loftus//// Solution to Programming Project 2.6//******************************************************************** import java.util.Scanner;public class MilesToKilometers{//----------------------------------------------------------------- // Converts miles into kilometers. The value for miles is read// from the user.//----------------------------------------------------------------- public static void main <String[] args>{final double MILES_PER_KILOMETER = 1.60935;double miles, kilometers;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the distance in miles: ">;miles = scan.nextDouble<>;kilometers = MILES_PER_KILOMETER * miles;System.out.println <"That distance in kilometers is: " +kilometers>;}}//******************************************************************** // MoneyConversion.java Author: Lewis/Loftus//// Solution to Programming Project 2.11//******************************************************************** import java.util.Scanner;public class MoneyConversion{//----------------------------------------------------------------- // Reads a monetary amount and computes the equivalent in bills// and coins.//----------------------------------------------------------------- public static void main <String[] args>{double total;int tens, fives, ones, quarters, dimes, nickels;int remainingCents;Scanner scan = new Scanner<System.in>;System.out.print <"Enter monetary amount: ">;total = scan.nextDouble<>;remainingCents = <int> <total * 100>;tens = remainingCents / 1000;remainingCents %= 1000;fives = remainingCents / 500;remainingCents %= 500;ones = remainingCents / 100;remainingCents %= 100;quarters = remainingCents / 25;remainingCents %= 25;dimes = remainingCents / 10;remainingCents %= 10;nickels = remainingCents / 5;remainingCents %= 5;System.out.println <"That's equivalent to:">;System.out.println <tens + " ten dollar bills">;System.out.println <fives + " five dollar bills">;System.out.println <ones + " one dollar bills">;System.out.println <quarters + " quarters">;System.out.println <dimes + " dimes">;System.out.println <nickels + " nickels">;System.out.println <remainingCents + " pennies">;}}//******************************************************************** // OlympicRings.java Author: Lewis/Loftus//// Solution to Programming Project 2.18//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class OlympicRings extends JApplet{//----------------------------------------------------------------- // Draws the olympic logo.//----------------------------------------------------------------- public void paint <Graphics page>{final int DIAMETER = 50;setBackground <Color.lightGray>;page.setColor <Color.blue>;page.drawOval <30, 40, DIAMETER, DIAMETER>;page.setColor <Color.yellow>;page.drawOval <60, 70, DIAMETER, DIAMETER>;page.setColor <Color.black>;page.drawOval <90, 40, DIAMETER, DIAMETER>;page.setColor <Color.green>;page.drawOval <120, 70, DIAMETER, DIAMETER>;page.setColor <Color.red>;page.drawOval <150, 40, DIAMETER, DIAMETER>;}}//******************************************************************** // PieChart.java Author: Lewis/Loftus//// Solution to Programming Project 2.22//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class PieChart extends JApplet{//----------------------------------------------------------------- // Draws the pie chart with equal sections.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.lightGray>;page.setColor <Color.blue>;page.fillArc <70, 25, 100, 100, 0, 45>;page.setColor <Color.yellow>;page.fillArc <70, 25, 100, 100, 45, 45>;page.setColor <Color.black>;page.fillArc <70, 25, 100, 100, 90, 45>;page.setColor <Color.green>;page.fillArc <70, 25, 100, 100, 135, 45>;page.setColor <Color.red>;page.fillArc <70, 25, 100, 100, 180, 45>;page.setColor <Color.magenta>;page.fillArc <70, 25, 100, 100, 225, 45>;page.setColor <Color.cyan>;page.fillArc <70, 25, 100, 100, 270, 45>;page.setColor <Color.orange>;page.fillArc <70, 25, 100, 100, 315, 45>;}}//******************************************************************** // Seconds.java Author: Lewis/Loftus//// Solution to Programming Project 2.8//******************************************************************** import java.util.Scanner;public class Seconds{//----------------------------------------------------------------- // Computes the total number of seconds for a given number of// hours, minutes, and seconds.//----------------------------------------------------------------- public static void main <String[] args>{final int SECONDS_PER_HOUR = 3600;final int SECONDS_PER_MINUTE = 60;int seconds, minutes, hours, totalSeconds;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the number of hours: ">;hours = scan.nextInt<>;System.out.print <"Enter the number of minutes: ">;minutes = scan.nextInt<>;System.out.print <"Enter the number of seconds: ">;seconds = scan.nextInt<>;totalSeconds = hours * SECONDS_PER_HOUR +minutes * SECONDS_PER_MINUTE +seconds;System.out.println <>;System.out.println <"Total seconds: " + totalSeconds>;}}//******************************************************************** // Seconds2.java Author: Lewis/Loftus//// Solution to Programming Project 2.9//******************************************************************** import java.util.Scanner;public class Seconds2{//----------------------------------------------------------------- // Computes the number of hours, minutes, and seconds that are// equivalent to the number of seconds entered by the user.//----------------------------------------------------------------- public static void main <String[] args>{final int SECONDS_PER_HOUR = 3600;final int SECONDS_PER_MINUTE = 60;int seconds, minutes, hours;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the number of seconds: ">;seconds = scan.nextInt<>;hours = seconds / SECONDS_PER_HOUR;seconds = seconds % SECONDS_PER_HOUR; // remaining secondsminutes = seconds / SECONDS_PER_MINUTE;seconds = seconds % SECONDS_PER_MINUTE; // remaining secondsSystem.out.println <>;System.out.println <"Hours: " + hours>;System.out.println <"Minutes: " + minutes>;System.out.println <"Seconds: " + seconds>;}}//******************************************************************** // ShadowName.java Author: Lewis/Loftus//// Solution to Programming Project 2.21//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class ShadowName extends JApplet{//----------------------------------------------------------------- // Draws a name with a slightly offset shadow.//----------------------------------------------------------------- public void paint <Graphics page>{setBackground <Color.yellow>;page.setColor <Color.black>;page.drawString <"John A. Lewis", 50, 50>;page.setColor <Color.red>;page.drawString <"John A. Lewis", 49, 49>;}}//******************************************************************** // Snowman2.java Author: Lewis/Loftus//// Solution to Programming Project 2.14//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class Snowman2 extends JApplet{//----------------------------------------------------------------- // Draws a snowman with added features.//----------------------------------------------------------------- public void paint <Graphics page>{final int MID = 130;final int TOP = 50;setBackground <Color.cyan>;page.setColor <Color.blue>;page.fillRect <0, 175, 300, 50>; // groundpage.setColor <Color.yellow>;page.fillOval <260, -40, 80, 80>; // sunpage.setColor <Color.white>;page.fillOval <MID-20, TOP, 40, 40>; // headpage.fillOval <MID-35, TOP+35, 70, 50>; // upper torsopage.fillOval <MID-50, TOP+80, 100, 60>; // lower torsopage.setColor <Color.red>;page.fillOval <MID-3, TOP+50, 6, 6>; // buttonpage.fillOval <MID-3, TOP+60, 6, 6>; // buttonpage.setColor <Color.black>;page.fillOval <MID-10, TOP+10, 5, 5>; // left eyepage.fillOval <MID+5, TOP+10, 5, 5>; // right eyepage.drawArc <MID-10, TOP+20, 20, 10, 10, 160>; // frownpage.drawLine <MID-25, TOP+60, MID-50, TOP+40>; // left armpage.drawLine <MID+25, TOP+60, MID+55, TOP+60>; // right armpage.drawLine <MID-20, TOP+5, MID+20, TOP+5>; // brim of hatpage.fillRect <MID-15, TOP-20, 30, 25>; // top of hatpage.drawString <"John Lewis", 20, 20>; // artist}}//******************************************************************** // SquareCalculations.java Author: Lewis/Loftus//// Solution to Programming Project 2.12//******************************************************************** import java.util.Scanner;public class SquareCalculations{//----------------------------------------------------------------- // Computes a square's perimeter and area given the length of// one side.//----------------------------------------------------------------- public static void main <String[] args>{int side, perimeter, area;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the length of a square's side: ">;side = scan.nextInt<>;perimeter = side * 4;area = side * side;System.out.println <"Perimeter: " + perimeter>;System.out.println <"Area: " + area>;}}//******************************************************************** // TempConverter2.java Author: Lewis/Loftus//// Solution to Programming Project 2.5//******************************************************************** import java.util.Scanner;public class TempConverter2{//----------------------------------------------------------------- // Computes the Celsius equivalent of a Fahrenheit value read// from the user. Uses the formula C = <5/9><F - 32>.//----------------------------------------------------------------- public static void main <String[] args>{final int BASE = 32;final double CONVERSION_FACTOR = 5.0 / 9.0;double celsiusTemp, fahrenheitTemp;Scanner scan = new Scanner<System.in>;System.out.print <"Enter a Fahrenheit temperature: ">;fahrenheitTemp = scan.nextDouble<>;celsiusTemp = CONVERSION_FACTOR * <fahrenheitTemp - BASE>;System.out.println <"Celsius Equivalent: " + celsiusTemp>;}}//******************************************************************** // TravelTime.java Author: Lewis/Loftus//// Solution to Programming Project 2.7//******************************************************************** import java.util.Scanner;public class TravelTime{//----------------------------------------------------------------- // Converts miles into kilometers. The value for miles is read// from the user.//----------------------------------------------------------------- public static void main <String[] args>{int speed, distance;double time;Scanner scan = new Scanner<System.in>;System.out.print <"Enter the speed: ">;speed = scan.nextInt<>;System.out.print <"Enter the distance traveled: ">;distance = scan.nextInt<>;time = <double> distance / speed;System.out.println <"Time elapsed during trip: " + time>;}}。

java习题及答案第2章 习题参考答案

java习题及答案第2章 习题参考答案

第2章习题解答1. 安装JDK开发运行环境:从官方网站下载最新版的JDK安装软件,安装JDK软件。

答:JDK开发运行环境的安装大致有三步,一是下载JDK安装软件,二是安装JDK软件,三是确认JDK是否正确安装。

建议从官方网站下载JDK软件。

详细安装过程略。

2. 安装集成开发环境Eclipse:从官方网站下载最新版的Eclipse软件,将其解压缩到适当的文件夹中,运行Eclipse并配置其所需要的Java运行环境,创建workspace文件夹,创建一个Java Application项目并编写简单的Java程序,执行该程序验证安装过程的正确性。

答:安装集成开发环境Eclipse 大致有三步,一是下载Eclipse软件(有多种版本可供选择,根据实际情况或需要下载必要的版本),二是安装Eclipse软件(通常只需要将压缩软件解压后放在适当的文件夹下即可),三是创建workspace文件夹用于存放Java程序。

建议从官方网站下载Eclipse软件。

详细安装过程略。

3. 从网络上查找一个感兴趣的Java应用程序,在Eclipse开发环境中编辑调试运行该程序。

答:有关过程略。

4. 简述建立Java有关软件开发运行环境的主要步骤。

答:建立Java有关软件开发运行的主要步骤有:一是下载安装JDK;二是下载安装Eclipse 或netbeansws等这样的集成开发环境;三是运行集成开发环境,建立Java项目,编写Java 程序,调试运行。

最后,还需要考虑开发好的程序的部署和运行。

5. 从官方网站下载《The Java™ Language Specification》(Java SE 7 Edition),并打开阅读该文档。

答:在线问答的网址是://docs.oracle/javase/specs/jls/se7/html/ 。

下载PDF文档的网址是ht3tp://docs.oracle/javase/specs/。

java第二章代码练习.doc

java第二章代码练习.doc

运行结果截图:
//:NumberalExpression.java package com.chenlong; public class NumberalExpression { public static void main(String[] args) { int decimal = 65; int octal = 0101; int hexadecimal = 0x41; System.out.println("SA进 " + decimal); System.out.println("Qk进 0101对应 进 " + octal); System.out.println("SAQm进 0x41对应 进 " + hexadecimal); } }
运行结果截图:
//:primitiveWidening.java package com.chenlong; public class primitiveWidening { public static void main(String[] args) { char c = '\u0041'; System.out.println("char ->long:c=\'\\u0041\''->"); long l =c; System.out.println("l=0x"+Long.toHexString (l)); float f = 1.23f; double d =f; System.out.println("float->double:f = 1.23f->d=" + d); int big =1234567890; float approx =big; System.out.println("int->float:(1234567890-(int)1234567890f)v„结果:"+ (big -(int)approx)); long large =1234567890123456789L; double pricisionLosing =large; System.out.println("long->double:(1234567890123456789L-(long)1234567890123456789D) v„结果:" +(large-(long)pricisionLosing)); } } 第 7 页,共 19 页

jAVA2程序设计基础第二章精品PPT课件

jAVA2程序设计基础第二章精品PPT课件
Java中整型常量可以分为十进制、十六进制和八进制。而整型变量的 定义可分为四种:byte型、short型、int型和long型。 4. 浮点类型
浮点类型可分为 float(32位)和double(64位)两种类型。float 型叫做单 精度浮点数。
返回
2.1.4 Java中的命名规则
一般来说,变量命名只要按照标识符的命名规则来命名即可,但为了 程序更加规范和易读,在Java语言规范中有以下命名规则:
返回
2.1.3 Java语言中的基本数据类型
1. 逻辑类型 逻辑类型又称布尔类型,其值只有两种:真(true)和假(false)。它使用
关键字boolean 来定义逻辑变量。 2. 字符类型
Java中每一个字符占两个字节,它使用的是unicode字符集,因此可 使用的常量共有65535个。它使用关键字char来定义字符变量。 3. 整数类型
第2章 Java编程基础
教学提示:在上一章中,我们对Java语言的历史、原理、开发环 境的搭建和系统环境变量的设置进行了介绍,使读者对其有一个基本 的了解。但仅仅掌握这些还不够。本章将进一步介绍Java语言程序设计 的方法与技巧,任何技巧和能力的形成都是以基础知识作为铺垫的, 因此本章把注意力集中在Java语言最基础的知识方面,这涉及到数据类 型以及定义在其上的运算、表达式、基本语法格式、变量及变量的作 用域和运算符等。学习这些内容后,读者对Java语言会有一个更深的了 解。
返回
2.1.5 Java中的一些注意事项
(1) Java是要区分大小写的。 (2) 功能执行语句的后面必须以分号(;)结束,这是提示该执行语句 结束的标志,而且这个分号是英文的(;)而不能是中文的(;)。
返回
2.2 变量及变量的作用域

java第七版课后答案——第二章

java第七版课后答案——第二章

//******************************************************************** // AverageOfThree.java Author: Lewis/Loftus//// Solution to Programming Project 2.2//******************************************************************** import java.util.Scanner;public class AverageOfThree{//----------------------------------------------------------------- // Reads three integers from the user and prints their average.//----------------------------------------------------------------- public static void main (String[] args){int num1, num2, num3;double average;Scanner scan = new Scanner (System.in);System.out.print ("Enter the first number: ");num1 = scan.nextInt();System.out.print ("Enter the second number: ");num2 = scan.nextInt();System.out.print ("Enter the third number: ");num3 = scan.nextInt();average = (double) (num1+num2+num3) / 3;System.out.println ("The average is: " + average);}}//******************************************************************** // AverageOfThree2.java Author: Lewis/Loftus//// Alternate solution to Programming Project 2.2//******************************************************************** import java.util.Scanner;public class AverageOfThree2{//----------------------------------------------------------------- // Reads three integers from the user and prints their average.//----------------------------------------------------------------- public static void main (String[] args){int num1, num2, num3;double average;Scanner scan = new Scanner (System.in);System.out.print ("Enter three numbers: ");num1 = scan.nextInt();num2 = scan.nextInt();num3 = scan.nextInt();average = (double) (num1+num2+num3) / 3;System.out.println ("The average is: " + average);}}//******************************************************************** // Balloons.java Author: Lewis/Loftus//// Solution to Programming Project 2.17//********************************************************************import javax.swing.JApplet;import java.awt.*;public class Balloons extends JApplet{//----------------------------------------------------------------- // Draws a set of balloons on strings.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.white);// draw the stringspage.setColor (Color.black);page.drawLine (45, 95, 100, 300);page.drawLine (90, 100, 100, 300);page.drawLine (60, 100, 100, 300);page.drawLine (122, 85, 100, 300);page.drawLine (145, 115, 100, 300);// draw the balloonspage.setColor (Color.blue);page.fillOval (20, 30, 50, 65);page.setColor (Color.yellow);page.fillOval (70, 40, 40, 60);page.setColor (Color.red);page.fillOval (40, 50, 40, 55);page.setColor (Color.green);page.fillOval (100, 30, 45, 55);page.setColor (Color.cyan);page.fillOval (120, 55, 50, 60);}}//******************************************************************** // BigDipper.java Author: Lewis/Loftus//// Solution to Programming Project 2.16//********************************************************************import javax.swing.JApplet;import java.awt.*;public class BigDipper extends JApplet{//----------------------------------------------------------------- // Draws the Big Dipper constellation and some other stars.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.black);page.setColor (Color.white);page.drawLine (100, 80, 110, 120);page.drawLine (110, 120, 165, 115);page.drawLine (165, 115, 175, 70);page.drawLine (100, 80, 175, 70);page.drawLine (175, 70, 245, 20);page.drawLine (245, 20, 280, 30);// Draw some extra starspage.fillOval (50, 50, 4, 4);page.fillOval (70, 150, 3, 3);page.fillOval (90, 30, 3, 3);page.fillOval (220, 140, 4, 4);page.fillOval (280, 170, 3, 3);page.fillOval (310, 100, 4, 4);page.fillOval (360, 20, 3, 3);}}//******************************************************************** // BusinessCard.java Author: Lewis/Loftus//// Solution to Programming Project 2.20//********************************************************************import javax.swing.JApplet;import java.awt.*;public class BusinessCard extends JApplet{//----------------------------------------------------------------- // Draws a business card.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.yellow);page.setColor (Color.red);page.fillOval (10, 20, 60, 60);page.setColor (Color.cyan);page.fillRect (25, 35, 130, 25);page.setColor (Color.black);page.drawString ("James C. Kerplunk", 35, 50);page.drawString ("President and CEO", 85, 80);page.drawString ("Origin Software, Inc.", 85, 100);page.drawLine (225, 20, 225, 80);page.drawLine (195, 50, 255, 50);page.setColor (Color.blue);page.drawString ("where it all begins...", 115, 135);}}//******************************************************************** // ChangeCounter.java Author: Lewis/Loftus//// Solution to Programming Project 2.10//******************************************************************** import java.util.Scanner;public class ChangeCounter{//----------------------------------------------------------------- // Computes the total value of a collection of coins.//----------------------------------------------------------------- public static void main (String[] args){int quarters, dimes, nickels, pennies;int total, dollars, cents;Scanner scan = new Scanner(System.in);System.out.print ("Enter the number of quarters: ");quarters = scan.nextInt();System.out.print ("Enter the number of dimes: ");dimes = scan.nextInt();System.out.print ("Enter the number of nickels: ");nickels = scan.nextInt();System.out.print ("Enter the number of pennies: ");pennies = scan.nextInt();total = quarters * 25 + dimes * 10 + nickels * 5 + pennies;dollars = total / 100;cents = total % 100;System.out.println ("Total value: " + dollars + " dollars and " + cents + " cents.");}}//******************************************************************** // DrawName.java Author: Lewis/Loftus//// Solution to Programming Project 2.15//********************************************************************import javax.swing.JApplet;import java.awt.*;public class DrawName extends JApplet{//----------------------------------------------------------------- // Draws a name.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.cyan);page.setColor (Color.black);page.drawString ("John A. Lewis", 50, 50);}}//******************************************************************** // FloatCalculations.java Author: Lewis/Loftus//// Solution to Programming Project 2.4//******************************************************************** import java.util.Scanner;public class FloatCalculations{//----------------------------------------------------------------- // Reads two floating point numbers and prints their sum,// difference, and product.//----------------------------------------------------------------- public static void main (String[] args){float num1, num2;Scanner scan = new Scanner (System.in);System.out.print ("Enter the first number: ");num1 = scan.nextFloat();System.out.print ("Enter the second number: ");num2 = scan.nextFloat();System.out.println ("Their sum is: " + (num1+num2));System.out.println ("Their difference is: " + (num1-num2));System.out.println ("Their product is: " + (num1*num2));}}//******************************************************************** // Fraction.java Author: Lewis/Loftus//// Solution to Programming Project 2.13//******************************************************************** import java.util.Scanner;public class Fraction{//----------------------------------------------------------------- // Computes the floating point equivalent of a fraction.//----------------------------------------------------------------- public static void main (String[] args){int numerator, denominator;float value;Scanner scan = new Scanner(System.in);System.out.print ("Enter the numerator: ");numerator = scan.nextInt();System.out.print ("Enter the denominator: ");denominator = scan.nextInt();value = (float) numerator / denominator;System.out.println ("Floating point equivalent: " + value);}}//******************************************************************** // HousePicture.java Author: Lewis/Loftus//// Solution to Programming Project 2.19//********************************************************************import javax.swing.JApplet;import java.awt.*;public class HousePicture extends JApplet{//----------------------------------------------------------------- // Draws a house scene.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.cyan);page.setColor (Color.gray);page.fillRect (0, 200, 400, 50); // groundpage.setColor (Color.blue);page.fillRect (50, 125, 300, 100); // housepage.setColor (Color.green);page.fillRect (180, 175, 40, 50); // doorpage.setColor (Color.yellow);page.fillRect (100, 155, 40, 25); // windowpage.fillRect (260, 155, 40, 25); // windowpage.setColor (Color.black);page.fillRect (40, 100, 320, 40); // roofpage.fillOval (210, 200, 6, 6); // doorknobpage.setColor (Color.red);page.fillRect (80, 80, 20, 40); // chimneypage.setColor (Color.darkGray);page.fillOval (80, 60, 20, 20); // smokepage.fillOval (85, 50, 15, 25); // smokepage.fillOval (90, 45, 15, 20); // smokepage.setColor (Color.white);page.fillOval (200, 30, 80, 40); // cloudpage.fillOval (230, 40, 80, 40); // cloud}}//******************************************************************** // InfoParagraph.java Author: Lewis/Loftus//// Solution to Programming Project 2.3//********************************************************************import java.util.Scanner;public class InfoParagraph{//----------------------------------------------------------------- // Reads information about a person and incorporates it into an// output paragraph.//----------------------------------------------------------------- public static void main (String[] args){String name, age, college, pet;Scanner scan = new Scanner (System.in);System.out.print ("What is your name? ");name = scan.nextLine();System.out.print ("How old are you? ");age = scan.nextLine();System.out.print ("What college do you attend? ");college = scan.nextLine();System.out.print ("What is your pet's name? ");pet = scan.nextLine();System.out.println ();System.out.print ("Hello, my name is " + name + " and I am ");System.out.print (age + " years\nold. I'm enjoying my time at "); System.out.print (college + ", though\nI miss my pet " + pet);System.out.println (" very much!");System.out.println ();}}//******************************************************************** // Lincoln4.java Author: Lewis/Loftus//// Solution to Programming Project 2.1//********************************************************************public class Lincoln4{//----------------------------------------------------------------- // Prints a quote from Abraham Lincoln, including quotation// marks.//----------------------------------------------------------------- public static void main (String[] args){System.out.println ("A quote by Abraham Lincoln:");System.out.println ("\"Whatever you are, be a good one.\"");}}//******************************************************************** // MilesToKilometers.java Author: Lewis/Loftus//// Solution to Programming Project 2.6//******************************************************************** import java.util.Scanner;public class MilesToKilometers{//----------------------------------------------------------------- // Converts miles into kilometers. The value for miles is read// from the user.//----------------------------------------------------------------- public static void main (String[] args){final double MILES_PER_KILOMETER = 1.60935;double miles, kilometers;Scanner scan = new Scanner(System.in);System.out.print ("Enter the distance in miles: ");miles = scan.nextDouble();kilometers = MILES_PER_KILOMETER * miles;System.out.println ("That distance in kilometers is: " +kilometers);}}//******************************************************************** // MoneyConversion.java Author: Lewis/Loftus//// Solution to Programming Project 2.11//******************************************************************** import java.util.Scanner;public class MoneyConversion{//----------------------------------------------------------------- // Reads a monetary amount and computes the equivalent in bills// and coins.//----------------------------------------------------------------- public static void main (String[] args){double total;int tens, fives, ones, quarters, dimes, nickels;int remainingCents;Scanner scan = new Scanner(System.in);System.out.print ("Enter monetary amount: ");total = scan.nextDouble();remainingCents = (int) (total * 100);tens = remainingCents / 1000;remainingCents %= 1000;fives = remainingCents / 500;remainingCents %= 500;ones = remainingCents / 100;remainingCents %= 100;quarters = remainingCents / 25;remainingCents %= 25;dimes = remainingCents / 10;remainingCents %= 10;nickels = remainingCents / 5;remainingCents %= 5;System.out.println ("That's equivalent to:");System.out.println (tens + " ten dollar bills");System.out.println (fives + " five dollar bills");System.out.println (ones + " one dollar bills");System.out.println (quarters + " quarters");System.out.println (dimes + " dimes");System.out.println (nickels + " nickels");System.out.println (remainingCents + " pennies");}}//******************************************************************** // OlympicRings.java Author: Lewis/Loftus//// Solution to Programming Project 2.18//********************************************************************import javax.swing.JApplet;import java.awt.*;public class OlympicRings extends JApplet{//----------------------------------------------------------------- // Draws the olympic logo.//----------------------------------------------------------------- public void paint (Graphics page){final int DIAMETER = 50;setBackground (Color.lightGray);page.setColor (Color.blue);page.drawOval (30, 40, DIAMETER, DIAMETER);page.setColor (Color.yellow);page.drawOval (60, 70, DIAMETER, DIAMETER);page.setColor (Color.black);page.drawOval (90, 40, DIAMETER, DIAMETER);page.setColor (Color.green);page.drawOval (120, 70, DIAMETER, DIAMETER);page.setColor (Color.red);page.drawOval (150, 40, DIAMETER, DIAMETER);}}//******************************************************************** // PieChart.java Author: Lewis/Loftus//// Solution to Programming Project 2.22//******************************************************************** import javax.swing.JApplet;import java.awt.*;public class PieChart extends JApplet{//----------------------------------------------------------------- // Draws the pie chart with equal sections.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.lightGray);page.setColor (Color.blue);page.fillArc (70, 25, 100, 100, 0, 45);page.setColor (Color.yellow);page.fillArc (70, 25, 100, 100, 45, 45);page.setColor (Color.black);page.fillArc (70, 25, 100, 100, 90, 45);page.setColor (Color.green);page.fillArc (70, 25, 100, 100, 135, 45);page.setColor (Color.red);page.fillArc (70, 25, 100, 100, 180, 45);page.setColor (Color.magenta);page.fillArc (70, 25, 100, 100, 225, 45);page.setColor (Color.cyan);page.fillArc (70, 25, 100, 100, 270, 45);page.setColor (Color.orange);page.fillArc (70, 25, 100, 100, 315, 45);}}//******************************************************************** // Seconds.java Author: Lewis/Loftus//// Solution to Programming Project 2.8//******************************************************************** import java.util.Scanner;public class Seconds{//----------------------------------------------------------------- // Computes the total number of seconds for a given number of// hours, minutes, and seconds.//-----------------------------------------------------------------public static void main (String[] args){final int SECONDS_PER_HOUR = 3600;final int SECONDS_PER_MINUTE = 60;int seconds, minutes, hours, totalSeconds;Scanner scan = new Scanner(System.in);System.out.print ("Enter the number of hours: ");hours = scan.nextInt();System.out.print ("Enter the number of minutes: ");minutes = scan.nextInt();System.out.print ("Enter the number of seconds: ");seconds = scan.nextInt();totalSeconds = hours * SECONDS_PER_HOUR +minutes * SECONDS_PER_MINUTE +seconds;System.out.println ();System.out.println ("Total seconds: " + totalSeconds);}}//******************************************************************** // Seconds2.java Author: Lewis/Loftus//// Solution to Programming Project 2.9//******************************************************************** import java.util.Scanner;public class Seconds2{//----------------------------------------------------------------- // Computes the number of hours, minutes, and seconds that are// equivalent to the number of seconds entered by the user.//----------------------------------------------------------------- public static void main (String[] args){final int SECONDS_PER_HOUR = 3600;final int SECONDS_PER_MINUTE = 60;int seconds, minutes, hours;Scanner scan = new Scanner(System.in);System.out.print ("Enter the number of seconds: ");seconds = scan.nextInt();hours = seconds / SECONDS_PER_HOUR;seconds = seconds % SECONDS_PER_HOUR; // remaining secondsminutes = seconds / SECONDS_PER_MINUTE;seconds = seconds % SECONDS_PER_MINUTE; // remaining secondsSystem.out.println ();System.out.println ("Hours: " + hours);System.out.println ("Minutes: " + minutes);System.out.println ("Seconds: " + seconds);}}//******************************************************************** // ShadowName.java Author: Lewis/Loftus//// Solution to Programming Project 2.21//********************************************************************import javax.swing.JApplet;import java.awt.*;public class ShadowName extends JApplet{//----------------------------------------------------------------- // Draws a name with a slightly offset shadow.//----------------------------------------------------------------- public void paint (Graphics page){setBackground (Color.yellow);page.setColor (Color.black);page.drawString ("John A. Lewis", 50, 50);page.setColor (Color.red);page.drawString ("John A. Lewis", 49, 49);}}//******************************************************************** // Snowman2.java Author: Lewis/Loftus//// Solution to Programming Project 2.14//********************************************************************import javax.swing.JApplet;import java.awt.*;public class Snowman2 extends JApplet{//----------------------------------------------------------------- // Draws a snowman with added features.//----------------------------------------------------------------- public void paint (Graphics page){final int MID = 130;final int TOP = 50;setBackground (Color.cyan);page.setColor (Color.blue);page.fillRect (0, 175, 300, 50); // groundpage.setColor (Color.yellow);page.fillOval (260, -40, 80, 80); // sunpage.setColor (Color.white);page.fillOval (MID-20, TOP, 40, 40); // headpage.fillOval (MID-35, TOP+35, 70, 50); // upper torsopage.fillOval (MID-50, TOP+80, 100, 60); // lower torsopage.setColor (Color.red);page.fillOval (MID-3, TOP+50, 6, 6); // buttonpage.fillOval (MID-3, TOP+60, 6, 6); // buttonpage.setColor (Color.black);page.fillOval (MID-10, TOP+10, 5, 5); // left eyepage.fillOval (MID+5, TOP+10, 5, 5); // right eyepage.drawArc (MID-10, TOP+20, 20, 10, 10, 160); // frownpage.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left armpage.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right armpage.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hatpage.fillRect (MID-15, TOP-20, 30, 25); // top of hatpage.drawString ("John Lewis", 20, 20); // artist}}//******************************************************************** // SquareCalculations.java Author: Lewis/Loftus//// Solution to Programming Project 2.12//******************************************************************** import java.util.Scanner;public class SquareCalculations{//----------------------------------------------------------------- // Computes a square's perimeter and area given the length of// one side.//----------------------------------------------------------------- public static void main (String[] args){int side, perimeter, area;Scanner scan = new Scanner(System.in);System.out.print ("Enter the length of a square's side: ");side = scan.nextInt();perimeter = side * 4;area = side * side;System.out.println ("Perimeter: " + perimeter);System.out.println ("Area: " + area);}}//********************************************************************// TempConverter2.java Author: Lewis/Loftus//// Solution to Programming Project 2.5//******************************************************************** import java.util.Scanner;public class TempConverter2{//----------------------------------------------------------------- // Computes the Celsius equivalent of a Fahrenheit value read// from the user. Uses the formula C = (5/9)(F - 32).//----------------------------------------------------------------- public static void main (String[] args){final int BASE = 32;final double CONVERSION_FACTOR = 5.0 / 9.0;double celsiusTemp, fahrenheitTemp;Scanner scan = new Scanner(System.in);System.out.print ("Enter a Fahrenheit temperature: ");fahrenheitTemp = scan.nextDouble();celsiusTemp = CONVERSION_FACTOR * (fahrenheitTemp - BASE);System.out.println ("Celsius Equivalent: " + celsiusTemp);}}//******************************************************************** // TravelTime.java Author: Lewis/Loftus//// Solution to Programming Project 2.7//******************************************************************** import java.util.Scanner;public class TravelTime{//----------------------------------------------------------------- // Converts miles into kilometers. The value for miles is read// from the user.//-----------------------------------------------------------------public static void main (String[] args){int speed, distance;double time;Scanner scan = new Scanner(System.in);System.out.print ("Enter the speed: ");speed = scan.nextInt();System.out.print ("Enter the distance traveled: ");distance = scan.nextInt();time = (double) distance / speed;System.out.println ("Time elapsed during trip: " + time); }}。

JAVA编程思想第二章习题答案

JAVA编程思想第二章习题答案

JAVA编程思想第⼆章习题答案// access/AccessTest.java// TIJ4 Chapter Access, Exercise 5, page 227/* Create a class with public, private, protected and package-access fields and * method members. Create an object of this class and see what kind of compiler * messages you get when you try to access all the class members. Be aware that * classes in the same directory are part of the "default" package.*//* in same directory:* package access;** public class FourWays {* int a = 0;* public int b = 1;* protected int c = 2;* private int d = 3;* FourWays() { System.out.println("FourWays() constructor"); }* void showa() { System.out.println(a); }* public void showb() { System.out.println(b); }* protected void showc() { System.out.println(c); }* private void showd() { System.out.println(d); }* }*/package access; // run command java access.AccessTestpublic class AccessTest {public static void main(String[] args) {FourWays fw = new FourWays();fw.showa();fw.showb();fw.showc();fw.a = 10;fw.b = 20;fw.c = 30;fw.showa();fw.showb();fw.showc();//! fw.showd(); // private access, compiler can't touch}}// access/Collision.java// TIJ4 Chapter Access, Exercise 2, page 217/* Take the code fragments in this section and turn them into a program and* verify that collisions do occur.*/import net.mindview.simple.*;import java.util.*;public class Collision {public static void main(String[] args) {// Vector v = new Vector(); // ambiquous collisionnet.mindview.simple.Vector v1 = new net.mindview.simple.Vector();java.util.Vector v2 = new java.util.Vector();}}// access/ConnectionManager.java// TIJ4 Chapter Access, Exercise 8, page 233/* Following the form of the example Lunch.java, create a class called* ConnectionManager that manages a fixed array of Connection objects. The client* programmer must not be able to explicitly create Connection objects, but can* only get them via a static method in ConnectionManager. When ConnectionManager * runs out of objects, it returns a null reference. Test the classes in main(). */ package access;class Connection {private static int count = 0;private int i = 0;private Connection() { System.out.println("Connection()");}// Allow creation via static method:static Connection makeConnection() {count++;return new Connection();}public static int howMany() { return count; }public String toString() {return ("Connection " + count);}}public class ConnectionManager {static int howManyLeft = 3;static Connection[] ca = new Connection[3];{for(Connection x : ca)x = Connection.makeConnection();}public static Connection getConnection() {if(howManyLeft > 0)return ca[--howManyLeft];else {System.out.println("No more connections");return null;}}public static void main(String[] args) {ConnectionManager cm = new ConnectionManager();System.out.println(cm.howManyLeft);cm.getConnection();System.out.println(howManyLeft);cm.getConnection();System.out.println(howManyLeft);cm.getConnection();System.out.println(cm.getConnection());System.out.println(howManyLeft);}}// access/cookie2/CookieMonster.java// TIJ4 Chapter Access, Exercise 4, page 227// Show that protected methods have package access but are not public. /* In directory Cookie2:* //access/cookie2/Cookie.java* //Creates a library* package access.cookie2;** public class Cookie {* public Cookie() {* System.out.println("Cookie contstructor");* }* protected void bite() { System.out.println("bite"); }* }*/package access.cookie2;public class CookieMonster {public static void main(String[] args) {Cookie x = new Cookie();x.bite(); // package access to protected method}}// access/CookieThief.java// TIJ4 Chapter Access, Exercise 4, page 227// Show that protected methods have package access but are not public. /* In directory Cookie2:* //access/cookie2/Cookie.java* //Creates a library* package access.cookie2;** public class Cookie {* public Cookie() {* System.out.println("Cookie contstructor");* }* protected void bite() { System.out.println("bite"); }* }*/import access.cookie2.*;public class CookieThief {public static void main(String[] args) {Cookie x = new Cookie();//! x.bite(); // access protected}}// access/debug/Debug.java// TIJ4 Chapter Access, Exercise 3, page 220/* Create two packages: debug and debugoff, containing an identical class with a * debug() method. The first version displays its String argument to the console, * the second does nothing. Use a static import line to import the class into a test * program, and demonstrate the conditional compilation effect.*//* In directory access/debugoff:* // access/debugoff/Debug.java* package access.debugoff;** public class Debug {* public static void debug(String s) { }* }*/package access.debug;public class Debug {public static void debug(String s) {System.out.println(s);}// TIJ4 chapter Access, Exercise 9, page 233/* Create in access/local directory in your CLASSPATH:* // access/local/PackagedClass.java* package access.local;** class PackagedClass {* public PackagedClass() {* System.out.println("Creating a packaged class");* }* }* // Then, in another directrory create the file below and explain why compiler* generates error. Would making Foreign class part of access.local change anything? */// access/foreign/Foreign.javapackage access.foreign;import access.local.*;public class Foreign {public static void main(String[] args) {PackagedClass pc = new PackagedClass();}}/* Compiler error because: PackagedClass in not public, so no access outside of* package. Moving Foreign to local would allow package access to PackagedClass. */// MakeWidget.java// TIJ4 Chapter Access, Exercise 7, page 230/* Create the library according to the code fragments describing access and Widget. * Create a Widget in a class that is not part of the access package.*//* in access package:* // access/Widget.java* package access;** public class Widget {* public Widget() { System.out.println("Widget()"); }* }*/import access.*;public class MakeWidget {public static void main(String[] args) {Widget w = new Widget();}}// access/ProtectedData.java// TIJ4 Chapter Access, Exercise 6, page 228/* Create a class with protected data. Create a second class in the same file with* a method that manipulates the protected data in the first class.*/class SomeData {protected int a = 13;}class DataChanger {static void change(SomeData sd, int i) { sd.a = i; }}public class ProtectedData {public static void main(String[] args) {SomeData x = new SomeData();System.out.println(x.a);DataChanger.change(x, 99);System.out.println(x.a);}}// access/UnpackagedMyClass.java// TIJ4 Chapter Access, Exercise 1, page 217// Create a class in a package. Create an instance of your class outside of that package. /* In another directory:* // access/mypackage/MyPackagedClass.java** package access.mypackage;** public class MyPackagedClass {* public MyPackagedClass() {System.out.println("MyPackagedClass()");}* }*/public class UnpackagedMyClass {public static void main(String[] args) {access.mypackage.MyPackagedClass m = new access.mypackage.MyPackagedClass(); }。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第二章
1. 下列()是合法的标识符?
_blank
2. 下列代码的输出结果是
class Test{
public static void main(String args [ ]) {
int ctr = 3;
int result = 1;
while(ctr > 1) {
result = result * ctr;
ctr--;
}
System.out.println(result);
}
}
6
3. 下列表达式1+2+ "aa"+3的值是()。

"3aa3 "
4. 已知y=2, z=3, n=4,则经过n=n+ -y*z/n运算后n的值为()。

3
5. 以下的变量定义语句中,合法的是()
double d = Double.MAX_VALUE;
6. 下列()不是Java中的保留字?
sizeof
7. 下列说法中,正确的一项是()。

False是合法的Java标识符
8. 已知x=2, y=3, z=4,则经过z- = --y – x--运算后,z的值为()。

4
9. 下列关于Java语言简单数据类型的说法中,正确的一项是()。

以0开头的整数代表8进制整型常量
10. 下列语句中不正确的一个是()。

byte b = 128;
11. 以下( )是合法的标识符
Te1_num
12. 下列关于基本数据类型的取值范围的描述中,正确的一个是()。

boolean类型的取值范围是真或假
13. 下列关于运算符优先级的说法中,不正确的一个是()
同一优先级的运算符在表达式中都是按照从右到左的顺序进行运算的
14. 下列()不是合法的标识符?
2$_million
15. 下列代码的输出结果是(选择一项)
class you{
public static void main (String [ ] args) {
int sales =3500;
int profit =800;
System.out.println(((sales + profit) /10 )*5);
}
}
2150
16. 在编写Java程序时,如果不为类的成员变量定义初始值,Java会给出它们的默认值,下列说法中不正确的一个是()。

long类型的默认值是0.0L
17. 在Java中,byte数据类型的范围是(选择一项)
-128~127
18. 下列关于基本数据类型的说法中,不正确的一项是()。

char是8位Unicode字符
19. 以下关键字(选择一项)用于终止循环语句
break
20. 下列选项中,()不属于Java语言的基本数据类型?
数组
21. 设有类型定义short i=32; long j=64; 下面赋值语句中不正确的一个是()
i=j;
22. 下列Java语句中,不正确的一项是()。

float e = 0.0d;
23. 现有1个char类型的变量c1=66和1个整型变量i=2,当执行c1=c1+(char)i;语句后,c1的值为()。

语句在编译时出错
24. 已知a=2, b=3,则表达式a%b*4%b的值为()。

2
25. public class Q {
public static void main( String argv [ ]){
int anar[ ]=new int [ ]{ 1,2,3 };
System.out.println(anar[1]);
}
2
26. 表达式(12==0) && (1/0 < 1)的值为()。

false
27. 不论测试条件是什么,下列(选择一项)循环将至少执行一次.
do-while
1. Java语言的整数类型变量和常量一样,各自都包括4种类型的数据,它们分
别是byte和long。

int short
2. 变量是Java程序的基本存储单元之一,变量的主要类型包括2大类:
字符型数值型
3. 在Java语言的基本数据类型中,占存储空间最少的类型是
,该类型占用的存储空间为
Boolean 1
4. 表达式42<<4。

(十进制表示)
672
5. 数据类型中存储空间均为64
Long double
6. Java具有特殊意义和作用,不能作为普通标识符使用。

保留字
7. 表达式5&2。

(十进制表示)
8. 定义初始值为10的8次方的常整型变量iLong的语句是
final iLong = 100000000L
9. 表达式10^2的值为。

(十进制表示)
8
10. Java
自动转换强制转换
类型数据不可以做类型转换。

boolean
12. Java语言中的逻辑与(&&)和逻辑或(||)
算。

短路
13. char
65536
14. 表达式9*4/ -5%5(十进制表示)
-2
15. 假设i=10, j=20, k=30,则表达式 !(i<j+k) || !(i+10<=j) 的值为
false
16. 表达式7|3。

(十进制表示)
7
17. 表达式11010011>>>3(二进制表示)
11010
18. 若a、b为int型变量,并且已分别赋值为5和10,则表达式(a++)+(++b)+a*b。

82
19. 在Java
单精度双精度
20. Java中的字符采用的是16位的编码。

Unicode
1. (科学方面:计算能量)
2. 编写一个Java Applet类型的程序,计算输出表达式12+5>3||12-5<7的值。

3. (账务应用:计算利息)如果你知道收支余额和年利率的百分比,你就可以使用下面的公式计算下个月要支付的利息额:
利息额=收支余额*(年利率/1200)
编写程序,读取收支余额和年百分利率,显示两个版本下的月利息:(1)使用对象框获取输入并显示输出;(2)使用控制台进行进行输入和输出。

4. 编写一个Java Application类型的程序,从键盘上输入三角形的三条边的长度,计算三角形的面积和周长并输出。

根据三角形边长求面积公式如
下:,其中a、b、c为三角形的三条边,s=(a+b+c)/2。

5. 已知圆球的体积公式为
4/3 r3,编一程序,输入圆球半径,计算并输出球的体积。

6. 课后习题2.12
7. 课后习题2.24
8. 编写一个Java Application类型的程序,从键盘上输入三角形的三条边的长度,计算三角形的面积和周长并输出。

根据三角形边长求面积公式如
下:,其中a、b、c为三角形的三条边,s=(a+b+c)/2。

9.编写一个Java Application类型的程序,定义一个byte类型的变量b,并从键盘上给它赋值为-100和100时,输出该变量的值
氏温度F的值并输出。

其转换公式如下:
F = (9 / 5) * C + 32
10.编写一个Java Application类型的程序,从键盘上输入摄氏温度C,计算华
F = (9 / 5) * C + 32
11.课后习题2.16。

相关文档
最新文档