编写一个Java程序来显示一个三位数的百位、十位和个位的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39429666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number
提问by Wilyum
I am a noob to java and I need advice to fix this code, I need to write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number. This is what I have so far, any ideas to how to get it to work? I'm not getting any errors, but my number isn't even outputting to the console correctly. Like if I typed in 123 for my 3 digit number everything prints blank. For example:
我是 Java 的菜鸟,我需要建议来修复此代码,我需要编写一个 Java 程序来显示三位数的百位、十位和个位的值。到目前为止,这就是我所拥有的,有关如何使其工作的任何想法?我没有收到任何错误,但我的号码甚至没有正确输出到控制台。就像我为我的 3 位数字输入 123 一样,所有内容都打印为空白。例如:
Enter a 3 digit number: 123 Hundreds place digit: Tens place digit: Ones place digit: Error! Number more then 3 digits. Error! Number less then 3 digits.
输入一个 3 位数字:123 百位数字:十位数字:个位数字:错误!数字超过 3 位。错误!数字少于 3 位。
It's not detecting my input of '123' for example, or anything else I put.
例如,它没有检测到我输入的“123”或我输入的任何其他内容。
import java.util.Scanner;
public class ValueOfDigits {
public static void main(String[] args) {
//Create new scanner
Scanner input = new Scanner(System.in);
//Values of each digit
int hundreds = 0;
int tens = 0;
int ones = 0;
//Prompt user to input 3 digit number
System.out.print("Enter a 3 digit number: ");
int number = input.nextInt();
//Displays hundreds place digit
hundreds = number / 100;
System.out.printf("Hundreds place digit: " , hundreds);
//Displays tens digit
tens = (number - hundreds) / 10;
System.out.printf("\nTens place digit: " , tens);
//Display ones digit
ones = (number - tens - hundreds);
System.out.printf("\nOnes place digit: " , ones);
//Error if number is less or more then three digits
if (number > 999);
System.out.println("\nError! Number more then 3 digits.");
if (number < 100);
System.out.println("Error! Number less then 3 digits.");
}
}
}
采纳答案by sinclair
Replace System.out.printf("Hundreds place digit: " , hundreds);
with System.out.println("Hundreds place digit: " + hundreds);
and remove the ;
after the if
-statments.
替换System.out.printf("Hundreds place digit: " , hundreds);
为System.out.println("Hundreds place digit: " + hundreds);
并删除-statments;
之后的if
。
If you want to use printf
take a look at this: https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)
如果你想使用printf
看看这个:https: //docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.目的...)
回答by Shahid
You have little error of calculating tenth and ones digit.
您在计算第十位和个位时几乎没有错误。
//Displays tens digit
tens = (number %100) / 10;
System.out.println("Tens place digit: " + tens);
//Display ones digit
ones = number %10;
System.out.println("Ones place digit: " ,+ ones);
Remove semicolons after if conditions.
删除 if 条件后的分号。
Again, the 3 digit checking should be done just after reading the number. Otherwise pointless calculation will be done for invalid numbers.
同样,应在读取数字后立即进行 3 位数检查。否则对无效数字将进行无意义的计算。
回答by Peter Rahul
Completely Fixed code is here : You can compare with your code to find error in your code
完全固定的代码在这里:您可以与您的代码进行比较以查找代码中的错误
import java.util.Scanner;
class ValueOfDigits {
public static void main(String[] args)
{
//Create new scanner
Scanner input = new Scanner(System.in);
//Values of each digit
int hundreds = 0;
int tens = 0;
int ones = 0;
//Prompt user to input 3 digit number
System.out.print("Enter a 3 digit number: ");
int number = input.nextInt();
if (number <= 999 && number > 99) // Checking condition for three digit number
{
//Displays hundreds place digit
hundreds = number / 100;
System.out.printf("Hundreds place digit: " + hundreds);
//Displays tens digit
tens = (number - (hundreds*100)) / 10; // compare with your code
System.out.printf("\nTens place digit: " + tens);
//Display ones digit
ones = (number - (tens*10) - (hundreds*100)); // compare with your code
System.out.printf("\nOnes place digit: " + ones);
}
//Error if number is less or more then three digits
else
{
if (number > 999)
System.out.println("\nError! Number more then 3 digits.");
if (number < 100)
System.out.println("Error! Number less then 3 digits.");
}
}
}
回答by Pooja
We can get ones, hundred or any other place value in Java using a simple code as shown below:
我们可以使用如下所示的简单代码在 Java 中获取 1、10 或任何其他位值:
int n=356;
int one=(n/1)%10;
int tens= (n/10)%10;
int hundred = (n/100)%10;
回答by Shakhobbek Juraev
'import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a < 10) {
System.out.println(0);
} else {
System.out.println((a- (a/100)*100)/10);
}
}
}'