我很担心放在oneDigit
方法的参数中的内容以及在我调用oneDigit method
的主方法中放入什么以打印出四位数的整数4321单词(四三二)。任何帮助将不胜感激!
import javax.swing.JOptionPane;
public class ToEnglish {
public static void oneDigit(int n1, int n2, int n3, int n4){
}//end oneDigit
public static void main(String[] args) {
int number;
int n1;
int n2;
int n3;
int n4;
String input;
JOptionPane.showMessageDialog(null,
"Welcome to the ToEnglish program by Josh Higgins!",
"ToEnglish",
JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog(null,
"Please enter a four-digit integer",
"ToEnglish",
JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(input);
//breaking integer up into 4 digits
n1 = number / 1000;
int n1Remainder = number % 1000;
n2 = n1Remainder / 100;
int n2Remainder = number % 100;
n3 = n2Remainder / 10;
int n3Remainder = number % 10;
n4 = number % 10;
if
(number < 0000 || number > 9999) {
System.out.println("Invalid input");
}
else {
n1 = number / 1000;
n1Remainder = number % 1000;
n2 = n1Remainder / 100;
n2Remainder = number % 100;
n3 = n2Remainder / 10;
n3Remainder = number % 10;
n4 = number % 10;
}
if ((number > 0000) && (number < 9999)) {
System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + " ");
}
else {
System.out.println("Invalid input");
}
//invoking oneDigit Method in order to print the four digit integer in text
oneDigit(n1);
oneDigit(n2);
oneDigit(n3);
oneDigit(n4);
System.out.println("All done for now!");
System.exit(0);
}//end of the main method
}//end of the class
@Vishal,这是我改进的代码,但是当我运行它时,我只得到4 3 2 1,而不是文本。有什么建议吗?
import javax.swing.JOptionPane;
public class ToEnglish {
public void oneDigit(int digit){
}//end oneDigit
public static void main(String[] args) {
int digit=0;
int number;
int n1;
int n2;
int n3;
int n4;
String input;
JOptionPane.showMessageDialog(null,
"Welcome to the ToEnglish program by Josh Higgins!",
"ToEnglish",
JOptionPane.PLAIN_MESSAGE);
input = JOptionPane.showInputDialog(null,
"Please enter a four-digit integer",
"ToEnglish",
JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(input);
//breaking integer up into 4 digits
n1 = number / 1000;
int n1Remainder = number % 1000;
n2 = n1Remainder / 100;
int n2Remainder = number % 100;
n3 = n2Remainder / 10;
int n3Remainder = number % 10;
n4 = number % 10;
if
(number < 0000 || number > 9999) {
System.out.println("Invalid input");
}
else {
n1 = number / 1000;
n1Remainder = number % 1000;
n2 = n1Remainder / 100;
n2Remainder = number % 100;
n3 = n2Remainder / 10;
n3Remainder = number % 10;
n4 = number % 10;
}
if ((number > 0000) && (number < 9999)) {
System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + " ");
}
else {
System.out.println("Invalid input");
}
switch (digit) {
case 1 : System.out.print("One");
break;
case 2 : System.out.print("Two");
break;
case 3 : System.out.print("Three");
break;
case 4 : System.out.print("Four");
break;
case 5 : System.out.print("Five");
break;
case 6 : System.out.print("Six");
break;
case 7 : System.out.print("Seven");
break;
case 8 : System.out.print("Eight");
break;
case 9 : System.out.print("Nine");
break;
}
System.out.println("All done for now!");
System.exit(0);
}//end of the main method
}//end of the class
答案 0 :(得分:0)
根据您的方法原型,oneDigit方法接受四个整数,并且您只使用一个整数调用该方法。 您可以使用一个参数编写方法。在方法内部使用带有参数的开关盒来打印单词中的数字。
例如: 此代码应写在oneDigit方法
中{{1}}
你可以跟随所有数字。
或者您可以编写一个if-else if块来检查传递给方法的整数,并在该块中的单词中打印数字。
编辑:代码块
答案 1 :(得分:-1)
试试这个:
public static String oneDigit(int digit){
switch(digit){
case 0:
return "zero";
case 1:
return "one";
//add missing cases
default:
return "not in range"; //you can throw an exception here if you prefer
}
}