我很困惑要在oneDigit
方法的參數中放什麼,以及爲了打印出四位數的整數4321而在調用oneDigit method
的主要方法中放什麼用文字(四三二一)。任何幫助將非常感激!調用Void方法來打印一個整數作爲文本
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
做一些研究如何在java中使用方法 –