编写一个Java程序来显示三位数的百位、十位和一位的值
我是java的noob,我需要一些建议来修复这段代码,我需要编写一个java程序来显示百位、十位和一位三位数的值。这就是我到目前为止所知道的,有什么办法让它发挥作用吗?我没有收到任何错误,但我的号码甚至没有正确地输出到控制台。就像我输入123作为我的3位数,所有的东西都是空白的。例如: 输入一个3位数的数字:123 百位数字: 十位数字: 一位数字: 错误!数字超过3位。 错误!数字小于3位 例如,它没有检测到我输入的“123”或我输入的任何东西编写一个Java程序来显示三位数的百位、十位和一位的值,java,Java,我是java的noob,我需要一些建议来修复这段代码,我需要编写一个java程序来显示百位、十位和一位三位数的值。这就是我到目前为止所知道的,有什么办法让它发挥作用吗?我没有收到任何错误,但我的号码甚至没有正确地输出到控制台。就像我输入123作为我的3位数,所有的东西都是空白的。例如: 输入一个3位数的数字:123 百位数字: 十位数字: 一位数字: 错误!数字超过3位。 错误!数字小于3位 例如,它没有检测到我输入的“123”或我输入的任何东西 import java.util.Sca
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.");
}
import java.util.Scanner;
数字的公共阶级价值{
公共静态void main(字符串[]args){
//创建新扫描仪
扫描仪输入=新扫描仪(System.in);
//每个数字的值
整数=0;
整数十位数=0;
整数=0;
//提示用户输入3位数字
系统输出打印(“输入3位数字:”);
int number=input.nextInt();
//显示百位数字
百分之一百=个数/100;
System.out.printf(“百位数字:”,百位);
//显示十位数
十位数=(百位数)/10;
System.out.printf(“\n位数字:”,十);
//显示一位数
一=(数字-十-百);
System.out.printf(“\nOnes位数字:”,个);
//数字小于或大于三位数时出错
若(电话号码>999);
System.out.println(“\n错误!数字超过3位。”);
如果(数量<100);
System.out.println(“错误!数字小于3位”);
}
}替换
System.out.printf(“百位数字:”,百位)代码>与System.out.println(“百位数字:+数百”)
并删除在if
-stations之后添加code>
如果您想使用printf
请查看以下内容:您在计算第十位和一位数字时几乎没有错误
//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);
在if条件后删除分号
同样,3位数字检查应在读取数字后立即完成。否则,将对无效数字进行无意义的计算。完全固定的代码如下:
您可以与代码进行比较以查找代码中的错误
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.");
}
}
}
import java.util.Scanner;
数字的类别价值{
公共静态void main(字符串[]args)
{
//创建新扫描仪
扫描仪输入=新扫描仪(System.in);
//每个数字的值
整数=0;
整数十位数=0;
整数=0;
//提示用户输入3位数字
系统输出打印(“输入3位数字:”);
int number=input.nextInt();
if(编号99)//三位数字的检查条件
{
//显示百位数字
百分之一百=个数/100;
System.out.printf(“百位数字:+100”);
//显示十位数
tens=(number-(数百*100))/10;//与代码进行比较
System.out.printf(“\n位数字:“+tens”);
//显示一位数
ones=(数字-(十位数*10)-(百位数*100));//与代码进行比较
System.out.printf(“\nOnes位数字:“+1”);
}
//数字小于或大于三位数时出错
其他的
{
如果(数字>999)
System.out.println(“\n错误!数字超过3位。”);
如果(数量<100)
System.out.println(“错误!数字小于3位”);
}
}
}
我们可以使用如下所示的简单代码在Java中获得1、100或任何其他位置值:
int n=356;
int one=(n/1)%10;
int tens= (n/10)%10;
int hundred = (n/100)%10;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int a=sc.nextInt();
如果(a<10){
系统输出打印项次(0);
}否则{
系统输出打印项次((a-(a/100)*100)/10);
}
}
}'
类源代码{
公共静态void main(字符串[]args){
创建新扫描仪
扫描仪输入=新扫描仪(System.in);
//每个数字的值
int=0;
整数=0;
整数=0;
整数十位数=0;
整数=0;
//提示用户输入5位数字
int n=input.nextInt();
如果(n>99999){
System.out.println(“\n错误!数字超过5位。”);
}
否则如果(n<10000){
System.out.println(“错误!数字小于5位”);
}
否则如果(100000什么不起作用?是否有任何错误,或者输出错误?请回答您的问题,以包含更多必要的信息。假设用户输入了256号。在这种情况下,数百
的值是多少?当我输入任何3位数字时,number-数百
的结果会是什么?一切都会显示为bl然后扔下我的“如果”错误。这就像它没有检测到我在控制台中输入的数字一样。非常感谢!我知道我做错了什么。我的错误和成百上千的位置现在正常工作,但是我的十和一没有显示正确的数字。非常感谢你们的帮助!嗨@Shakhobbek,请在确定您理解之前发布一个答案问题完全正确。问题要显示三位数的三个值。您可以先使用pos测试代码
'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);
}
}
}'
class Source {
public static void main(String[] args) {
Create new scanner
Scanner input = new Scanner(System.in);
//Values of each digit
int tenThousands=0;
int thousands=0;
int hundreds=0;
int tens=0;
int ones=0;
//Prompt user to input 5 digit number
int n = input.nextInt();
if (n > 99999) {
System.out.println("\nError! Number more than 5 digits.");
}
else if (n < 10000) {
System.out.println("Error! Number less than 5 digits.");
}
else if (10000<=n&&n<=99999){
tenThousands = n/10000;
System.out.println(tenThousands);
thousands = (n/1000)%10;
System.out.println(thousands);
hundreds = (n%1000)/100;
System.out.println(hundreds);
tens = (n%100)/10;
System.out.println(tens);
ones = n%10;
System.out.println(ones);
}
}
}