Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Learning the Java Language
Lesson: Numbers and Strings

Questions and Exercises: Numbers问题和练习:数字

Questions问题

  1. Use the API documentation to find the answers to the following questions:使用API文档查找以下问题的答案:

    1. What Integer method can you use to convert an int into a string that expresses the number in hexadecimal?您可以使用什么Integer方法将int转换为以十六进制表示数字的字符串?For example, what method converts the integer 65 into the string "41"?例如,什么方法将整数65转换为字符串“41”?

    2. What Integer method would you use to convert a string expressed in base 5 into the equivalent int?您将使用什么Integer方法将以基数5表示的字符串转换为等效的intFor example, how would you convert the string "230" into the integer value 65?例如,如何将字符串“230”转换为整数值65?Show the code you would use to accomplish this task.显示用于完成此任务的代码。

    3. What Double method can you use to detect whether a floating-point number has the special value Not a Number (NaN)?您可以使用什么Double方法来检测浮点数是否具有特殊值非数字(NaN)?

  2. What is the value of the following expression, and why?以下表达式的值是什么,为什么?

    Integer.valueOf(1).equals(Long.valueOf(1))

Exercises练习

  1. Change MaxVariablesDemo to show minimum values instead of maximum values.更改MaxVariablesDemo以显示最小值而不是最大值。You can delete all code related to the variables aChar and aBoolean.您可以删除与变量aCharaBoolean相关的所有代码。What is the output?输出是什么?

  2. Create a program that reads an unspecified number of integer arguments from the command line and adds them together.创建一个程序,从命令行读取未指定数量的整数参数,并将它们相加。For example, suppose that you enter the following:例如,假设您输入以下内容:

    java Adder 1 3 2 10

    The program should display 16 and then exit.程序应显示16,然后退出。The program should display an error message if the user enters only one argument.如果用户只输入一个参数,程序应显示错误消息。You can base your program on ValueOfDemo.您可以基于ValueOfDemo编写程序。

  3. Create a program that is similar to the previous one but has the following differences:创建一个与上一个类似但有以下区别的程序:

    • Instead of reading integer arguments, it reads floating-point arguments.它不读取整数参数,而是读取浮点参数。
    • It displays the sum of the arguments, using exactly two digits to the right of the decimal point.它显示参数的总和,精确地使用小数点右侧的两位数字。

    For example, suppose that you enter the following:例如,假设您输入以下内容:

    java FPAdder 1 1e2 3.0 4.754

    The program would display 108.75.程序将显示108.75Depending on your locale, the decimal point might be a comma (,) instead of a period (.).根据您的区域设置,小数点可能是逗号(,)而不是句点(.)。

Check your answers.检查你的答案。


Previous page: Summary of Numbers
Next page: Characters