由于篇幅问题,8次上机仅公开展示第1次上机题目。有文件需要请根据链接自取https://pan.baidu.com/s/1yV2xh5aj_lnVZzLs9hYoyA?pwd=45g6
提取码:45g6
1. 编译器的试验
本题需要用到HelloWorld程序(代码参考课件),需要在HelloWorld程序中引入一些变动(错误)并重新编译。尝试一下的改动,每次改一项(进行下一项时,前一项改动需要恢复),修改、重新编译并观察编译结果。如有编译错误,则需要理解编译提示并与之前的改动关联起来。通过这个操作,能够促进对编译信息的理解。以下为需要修改的内容列表:
- delete the “}” at the end of the file
- misspell “main” as “man”
- misspell “HelloWorld” as “helloworld”
- delete the “;”
- delete the closing “"”
- misspell “args” as “double”
理解并熟悉错误信息在编程过程中是很重要的能力。当然需要注意的是,有些字面含义不一定会有直接的帮助——提示信息中的错误位置很可能不是真正错误的地方。因此需要思考并进行尝试。
|
1.public class HelloWorld{ public static void main(String argv[]){ System.out.println("Hello World!"); } 2.public class HelloWorld{ public static void man(String argv[]){ System.out.println("Hello World!"); } } 3.public class helloworld{ public static void main(String argv[]){ System.out.println("Hello World!"); } 4.public class HelloWorld{ public static void main(String argv[]){ System.out.println("Hello World!") } } 5.public class HelloWorld{ public static void main(String argv[]){ System.out.println(Hello World!); } } 6.public class HelloWorld{ public static void main(String double[]){ System.out.println("Hello World!"); } } |
public class helloworld{ 4.需要';' 5.HelloWorld.java:3: 错误: 需要')' System.out.println(Hello World!); ^ HelloWorld.java:3: 错误: 非法的表达式开始 System.out.println(Hello World!); ^ HelloWorld.java:3: 错误: 不是语句 System.out.println(Hello World!); ^ HelloWorld.java:3: 错误: 非法字符: '\uff1b' System.out.println(Hello World!); ^ HelloWorld.java:3: 错误: 需要';' System.out.println(Hello World!); ^ HelloWorld.java:5: 错误: 解析时已到达文件结尾 6.HelloWorld.java:2: 错误: 需要<标识符> public static void main(String double[]){ ^ HelloWorld.java:2: 错误: 需要';' public static void main(String double[]){ ^ HelloWorld.java:2: 错误: 非法的类型开始 public static void main(String double[]){ ^ HelloWorld.java:2: 错误: 需要<标识符> public static void main(String double[]){ ^ HelloWorld.java:2: 错误: 需要';' public static void main(String double[]){ ^ HelloWorld.java:3: 错误: 需要<标识符> System.out.println("Hello World!"); ^ HelloWorld.java:3: 错误: 非法的类型开始 System.out.println("Hello World!"); ^ HelloWorld.java:3: 错误: 非法字符: '\uff1b' System.out.println("Hello World!"); ^ HelloWorld.java:5: 错误: 需要class, interface或enum |
不同的错误会导致不同的影响 对于语言的学习,要重视语法 有些错误即使存在,不影响程序的编译以及结果 |
2. 写一个程序叫CheckMark,该程序会对一个变量mark进行判断,如果大于等于50,则在控制台输出PASS,否则输出FAIL。提示:mark的值在main方法中进行设定。
输入时,将字符串转换成数字类型并进行判断 |
public class CheckMark { |
|
输入要紧跟在运行后面 如应该输入 java CheckMark 89 |
3. 写一个程序叫CozaLozaWoza,该程序输出从1到110的所有整数,输出时每行显示11个数字,并且若数字是3的倍数则输出“Coza”代替该数字的位置,若是5的倍数则输出“Loza”,7的倍数则为“Woza”,同时是3和5的倍数则为“CozaLoza”……以此类推。输出样例如下:
对每个数分别做3/5/7判断,倘若能被3整除,则将该数除以三之后进行是否被5整除的判断 |
public class CozaLozaWoza { |
|
要区分println和print方法的区别,println使用后会默认切换到下一行 |
4. 以下是一个货币转换的算法,主要是从人民币(CNY)转换为俄罗斯卢布(RUB),兑率是18.58。
兑换的算法如下:
• declare a final double variable for the RUB exchange rate and initialise it to 18.58
• declare a double variable for the CNY amount to be provided
• declare a double variable for the RUB amount to be calculated
• declare and create a Scanner object and initialise it
• ask the user how much money in CNY they wish to exchange
• obtain this amount from them using sc.nextDouble() and store the result in the relevant variable
• calculate how many RUB this is worth by multiplying by the exchange rate and store the result in the relevant variable and
• tell the user how many RUB they would receive for the amount of CNY they indicated.
请编写一个程序CurrencyConverter,实现上述算法:提示用户输入一个数,代表CNY的量,将其转换为RUB并输出转换后的金额。
导入scan模块实现用户输入,用final定义常量 |
import java.util.Scanner; |
|
Java中用户输入相较于其他语言略微繁琐,需要反复练习 |
5. 打印出100-1000范围内所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数",因为153 = 1的三次方+5的三次方+3的三次方。
需要将每个三位数的个十百位分别取到 |
public class exercise01 { |
|
使用了pow函数之后在前面需要加强制类型转换 |