用整数分隔数字-Deitel的Java书中的练习 [英] Separating the Digits in an Integer - exercise from Deitel's Java book

查看:52
本文介绍了用整数分隔数字-Deitel的Java书中的练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Deitel的"Java How to Program"第10版中的练习:

Exercise from Deitel's "Java How To Program" 10th edition:

2.30(以整数分隔数字)编写一个应用程序,该应用程序从用户输入一个由五位数字组成的数字,将该数字分隔为各个数字,并打印彼此分隔开三个空格的数字.例如,如果用户键入数字42339,则程序应打印

2.30 (Separating the Digits in an Integer) Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

4   2   3   3   9

假设用户输入正确的数字.输入超过五位数的数字会怎样?输入少于五位数的数字会发生什么情况?[提示:可以使用本章中学习的技巧来进行此练习.您需要同时使用除法和提醒操作来提取"每个数字.]

Assume that the user enters the correct number of digits. What happens when you enter a number with more than five digits? What happens when you enter a number with fewer than five digits?[Hint: It's possible to do this exercise with the techniques you learned in this chapter. You'll need to use both division and reminder operations to "pick off" each digit.]

有人可以告诉我如何使用除法和提醒运算符挑选"单个整数吗?

Could someone explain to me how should I go about "picking off" individual integers using division and reminder operators?

尚不允许使用控制结构(如果/else等),将在以后的章节中进行探讨.仅限变量,算术运算符和比较运算符.

control structures (if / else and the like) are not allowed yet, those are explored in future chapters. Variables, arithmetic operators and comparison operators only.

推荐答案

您知道数字长5位.

number / 10 000 to retrieve the first digit.
number = reminder
number / 1000 to retrieve the second digit.
number = reminder
number / 100 to retrieve the third digit.
number = reminder
number / 10 to retrieve the fourth digit.
and reminder is the 5th one.

这篇关于用整数分隔数字-Deitel的Java书中的练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆