该程序提取数字,我希望它一直循环直到用户键入键“Q”/“q”。例如,当用户按下“O”键时,程序应该打印他们输入的数字的个位数字,对于用户输入的任何 3 位数字,依此类推。我已经创建了提取个位、十位和百位数字的方法,并将它们存储到单独类中的变量中,但是当我尝试打印这些数字时,控制台中没有打印任何内容。
import java.util.Scanner;
class Methods {
public Methods (int value) {}
public int hundreds (int num) {
int hund = (num /100)%10;
return hund;
}
public int tens (int num) {
int ten = (num / 10)%10;
return ten;
}
public int ones (int num) {
int one = num % 10;
return one;
}
}
public class DigitExtractor {
public static void main(String[] args)
throws java.io.IOException {
char input = ' '; //initialize outside loop
Scanner s = new Scanner(System.in);
System.out.print("Input an integer");
int wholeNumber = s.nextInt();
Methods num = new Methods(wholeNumber);
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: ");
input = s.next().trim().charAt(0); //using scanner only
if (wholeNumber == 'W' || wholeNumber == 'w') {
System.out.println("The whole number is: " + wholeNumber);
} else if (wholeNumber == 'O' || wholeNumber == 'o') {
System.out.println("The ones place digit is: " + num.ones(wholeNumber));
} else if (wholeNumber == 'T' || wholeNumber == 't') {
System.out.println("The tens place digit is: " + num.tens(wholeNumber));
} else if (wholeNumber == 'H' || wholeNumber == 'H') {
System.out.println("The hundreds place digit is: " + num.hundreds(wholeNumber));
}
} while ((wholeNumber != 'q') && (wholeNumber != 'Q'));
}
}
弑天下
相关分类