我是java的菜鸟,我需要建议来修复这段代码,我需要编写一个Java程序来显示数百位,数十位和三位数的位置的值。这是我到目前为止,有任何关于如何使它工作的想法?我没有收到任何错误,但我的号码甚至没有正确输出到控制台。就像我输入123为我的3位数字一样,所有内容都打印为空白。例如:
输入一个3位数字:123 数百位数: 十位数字: 一个数字: 错误!数字超过3位数。 错误!数字少于3位数。
它没有检测到我输入的{'例如,或者我放的任何其他东西。
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.");
}
}
答案 0 :(得分:2)
计算第十位和第一位数时几乎没有错误。
//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);
在条件之后删除分号。
同样,应在读取数字后立即进行3位数检查。否则将对无效数字进行无意义的计算。
答案 1 :(得分:1)
我们可以使用如下所示的简单代码在Java中获得一个,一百或任何其他位置值:
int n=356;
int one=(n/1)%10;
int tens= (n/10)%10;
int hundred = (n/100)%10;
答案 2 :(得分:0)
将System.out.printf("Hundreds place digit: " , hundreds);
替换为System.out.println("Hundreds place digit: " + hundreds);
,然后移除;
后的if
-
如果您想使用printf
,请查看以下内容:https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)
答案 3 :(得分:0)
完全固定的代码在这里: 您可以将其与代码进行比较,以查找代码中的错误
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.");
}
}
}
答案 4 :(得分:-1)
'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);
}
}
}'
答案 5 :(得分:-1)
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);
}
}
}