簡體   English   中英

integer 的范圍基於它的最大值和最小值

[英]Range of an integer based on the max and min value of it

在我的 class 中,我被要求:

創建一個參數為單個 integer 的方法,並返回 integer 參數的范圍。

(負 integer 參數可以視為正 integer。)

澄清示例:

2634982 將產生 7 的范圍(因為 9 減去 2)

這是代碼:

import java.util.Scanner;

public class Main {
    static int Range(int one)
    {
        int max = 0;
        int min = Integer.MAX_VALUE;
        int two = 0;
        int three = 0;

        while (one != 0) {
            two = one % 10;
            three = one / 10;
            System.out.print(two+" "+three);
            if (two > max){
                max = two;
            }
            if (two < min){
                min = two;
            }

            //System.out.print("\n"+min+" "+max);
            one = max-min;
        }
        return one;
    }

    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int one = key.nextInt();
        System.out.println("\n"+one+" will produce a range of "+Range(one));
    }
}

我在 while 循環多次運行時遇到了麻煩,我覺得解決這個問題會使這項工作(至少比以前更好)

你為什么使用變量“三”? 我認為不需要。 我建議您使用以下代碼:

import java.util.Scanner;
public class Main {

static int Range(int one)
{

    int max = 0;
    int min = Integer.MAX_VALUE;
    int two = 0;
    
    while (one != 0) {
        two = one % 10;
        one = one / 10;
        
        //System.out.print(two+" "+three);
    
        if (two > max){
            max = two;    
        }

        if (two < min){
            min = two;    
        }

        //System.out.print("\n"+min+" "+max);
    }
    one = max-min;    

    return one;
}

    public static void main(String[] args) {
    
        Scanner key = new Scanner(System.in);
    
        System.out.print("Enter an integer: ");
        int one = key.nextInt();
    
        System.out.println("\n"+one+" will produce a range of "+Range(one));
    
    }
}

另一種解決方案

  1. int轉換為String
  2. String轉換為char數組。
  3. char數組進行排序(按升序)。
  4. 從最后一個數組元素中減去第一個數組元素。
import java.util.Scanner;

public class Main {
    private static int range(int one) {
        String str = String.valueOf(one);
        str = str.replaceFirst("\\+|\\-", "");
        char[] digits = str.toCharArray();
        Arrays.sort(digits);
        return (digits[digits.length - 1] - '0') - (digits[0] - '0');
    }

    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int one = key.nextInt();
        System.out.println("\n"+one+" will produce a range of "+ range(one));
    }
}
  • 請注意,class java.util.Scanner的方法nextInt將接受帶有前導+-的整數。 因此,我在將int轉換為String后刪除了該字符。
  • char實際上是一種數字類型(如int ),因此從char中減去字符0即可得到其數值。 換句話說,這是一種將char (數字)轉換為int的方法。
  • 如果您輸入單個數字int ,例如7 ,則范圍將為 0(零)。

這是來自示例運行的 output:

Enter an integer: 1959

1959 will produce a range of 8

† 假設您被允許使用 class java.util.Arrays

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM