2016-10-13 75 views
-1

我很困惑要在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 
+0

做一些研究如何在java中使用方法 –

回答

-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 
    } 
} 
0

根據你的方法原型,該oneDigit方法接受四個整數並已調用只有一個整數的方法。 您可以使用一個參數編寫該方法。在該方法內部使用帶參數的開關盒來打印數字。

例如: 此代碼應oneDigit方法

public static void oneDigit(int digit) { 
    switch(digit) { 
     case 1 : System.out.print("One"); 
       break; 
     //Other cases followed 
} 

你可以與所有數字遵循這裏面寫。

或者您可以編寫一個if-else if塊來檢查將哪個整數傳遞給該方法並在該塊中打印出單詞中的數字。

編輯:代碼塊

相關問題