2016-11-04 78 views
-2
int value = 1234; 
char[] chars = String.valueOf(value).toCharArray(); 

如何將這些值顯示爲整數?以整數形式顯示字符數字

+0

如果你想獲得的單個數字數組,使用一個循環,師和mod運算符 – TheLostMind

+0

我只想獲得數字Sequencewhich用戶輸入的最小值和最大值。 就像: –

+0

@OliverQueen您的意思是輸入的最小和最大位數。例如,對於值= 1234,它將是1和4?對? – Nurjan

回答

0
public static void main(String[] args) { 
    int value = 1234; 
    List<Integer> output = new ArrayList<Integer>(); 
    while (value > 0) { 
     output.add(value % 10); 
     value /= 10; 
    } 
    Collections.sort(output); 
    System.out.println("Min:" + output.get(0)); 
    System.out.println("Max:" + output.get(output.size() - 1)); 
} 
0

幾乎在那裏。你錯過了一件重要的事情。你需要調用sort()Arrays

public static void main(String[] args) { 
    int value = 1234; 
    char[] arr = String.valueOf(value).toCharArray(); 
    Arrays.sort(arr); 
    System.out.println(arr[0] + " " + arr[arr.length - 1]); 
} 

O/P:

1 4 // arr[0] is min and arr[arr.length-1] is max 
0

爲此,您可以使用for循環,只有當你想顯示這些數字seperately.I建議要顯示所有四個數字分開四行,由波紋管代碼完成。

int value = 1234; 
char [] chars = String.valueOf(value).toCharArray();  
for(int i=0; i < chars.length ; i++) 
System.out.println(chars[i]); 
0

您將需要一個循環拉每一個人數位,並將其與當前的最小/最大:

int n = 36348; 
    int min = Integer.MAX_VALUE; 
    int max = Integer.MIN_VALUE; 

    if (n > 0) { 


     while (n > 0) { 
      int digit = n % 10; 

      max = Math.max(max, digit); 
      min = Math.min(min, digit); 

      n /= 10; 
     } 


    } 

    System.out.println(min); 
    System.out.println(max); 
相關問題