1 回答
TA贡献1818条经验 获得超8个赞
您的代码有一些问题需要解决才能使其正常工作:
班级
Method
必须在班级之外DigitExtractor
。您在循环中调用的方法属于该类
Methods
,因此您必须实例化的构造函数是Methods
.您必须从变量中捕获键盘输入
input
。额外:你需要用 try/catch 包围你的代码。
我对您的代码进行了最少的更正,并且它适用于此版本:
import java.util.Scanner;
public class DigitExtractor {
public static void main(String[] args) throws java.io.IOException {
try (Scanner input = new Scanner(System.in)){
System.out.print("Input an integer");
int integer = input.nextInt();
Methods num = new Methods(integer);
char keyboard;
do {
System.out.println("show (W)hole number.");
System.out.println("show (O)nes place number.");
System.out.println("show (T)ens place number.");
System.out.println("show (H)undreds place number.");
System.out.println("(Q)uit");
System.out.println("Enter your choice: ");
keyboard = input.next().charAt(0);
if (keyboard == 'W' || keyboard == 'w') {
System.out.println("The whole number is: " + integer);
} else if (keyboard == 'O' || keyboard == 'o') {
System.out.println("The ones place digit is: " + num.ones(integer));
} else if (keyboard == 'T' || keyboard == 't') {
System.out.println("The tens place digit is: " + num.tens(integer));
} else if (keyboard == 'H' || keyboard == 'H') {
System.out.println("The hundreds place digit is: " + num.hundreds(integer));
}
} while ((keyboard != 'q') && (keyboard != 'Q'));
}
}
}
class Methods {
public Methods (int value) {}
public int hundreds (int num) {
return (num /100)%10;
}
public int tens (int num) {
return (num / 10)%10;
}
public int ones (int num) {
return num % 10;
}
}
添加回答
举报