繁体   English   中英

在Java中对整数数组进行排序和展平

[英]Sorting and flattening an integer array in Java

我有一个介绍Java类的项目,我们必须创建一个游戏,用户选择要使用的玩家,回合和骰子数。 掷骰子时,应该将它们从最大到最小排序。 我遇到的问题是将排序后的数组转换为int值(即将4、3、2转换为432)。 有没有一种方法可以组合数组,还是必须创建一个循环? 感谢您的帮助,我希望这很清楚,因为我的大脑被枪击了。

import java.util.Scanner;

/*
 * @param args
 */

class beatThat {


   public static void main(String[] args) {  
       int players = 0, rounds, dice, size, dValue, roundScore, a, b, t = 0;

       Scanner playerInput;
       playerInput = new Scanner(System.in);

       //   Thanks the user for playing the game.
       //   Asks for number of players.
       System.out.println("Thank you for playing Beat That!");

       //   Picks number of players.            
       //   If number of players not correct, requests selection again.
       do {
           System.out.println("Please pick your number of players (2-4):");
           players = playerInput.nextInt();

       } while (players < 2 | players > 4);

       //   Picks number of rounds.         
       //   If number of rounds not correct, requests selection again.
       do {
           System.out.println("Please pick your number of rounds " + 
                "(max 15):");
           rounds = playerInput.nextInt();

       } while (rounds < 1 | rounds > 15);

       //   Picks number of dice.           
       //   If number of dice not correct, requests selection again.            
       do {
           System.out.println("Please pick your number of dice (max 7):");
           dice = playerInput.nextInt();
       } while (dice < 1 | dice > 7);

       // Creates one dimensional array to hold dice values
       int score [] = new int [dice];

       // Generates random value between 1 & 6 for the amount of dice
       // chosen and assigns them to the array.
       for (int d = 0; d < dice; d++) {
           dValue = (int)(Math.random() * 6) + 1;
           System.out.print(dValue + ", ");
           score [d] = dValue;
       }

       // Rearranges array from largest number to smallest number.  
       for (a=1; a < dice; a++) {
           for (b = dice-1; b >= a; b--) {
               if(score[b-1] < score[b]){
                   t = score[b-1];
                   score[b-1] = score[b];
                   score[b] = t;

               }
           }
       }

       //////       // Can Delete. Prints out sorted array
       System.out.println();    
       System.out.println("Sorted array is: ");
       for (int i = 0; i < dice; i++)
           System.out.print(score[i] + " ");
       System.out.println("\n");

       // Makes sorted array into one score.
       int arrayLength = score.length;
       int arrayIndex; 
       for (int i = 0; i < dice; i++) {
           arrayIndex = (arrayLength - i - 1);
           roundScore = score[i];
           System.out.print("Your roundScore: ");
           System.out.println(roundScore);  
       }

       ///////      End of Dice method              


   }

}

只需将骰子乘以每​​个回合的底数即可:

int roundScore = 0;
for (int i = 0; i < dice; i++) {
       roundScore = roundScore * 10 + score[i];
}
System.out.println(roundScore);

在您的示例中, {4, 3, 2} 432 {4, 3, 2}的得分为432 ,中间值为: 0, 4, 43, 432

编辑

根据@PeterLawrey的建议改写

首先将其存储在字符串中,然后将其转换回数字

int[] nums = {1,2,3,4,5,6,7,8,9};

String number = "";
for(int i=0; i<nums.length;i++)
   number+=nums[i];

int finalNum = Integer.parseInt(number);

现场演示

循环可能是一个好方法。 查看StringBuilder

public static int intArrayToInt(int[] arr){
    StringBuilder ret = new StringBuilder();
    for(int i=0; i<arr.length; i++){
        ret.append(arr[i]);
    }
    return Integer.parseInt(ret.toString());
}

使用循环,这是最好的方法。 如果您真的很注重性能,请使用StringBuffer进行连接,然后转换为整数。

但是,如果您只想使用单行解决方案(而不用担心性能),则要将整数数组转换为单个整数,请使用以下命令:

Integer.parseInt(Arrays.toString(arr).replaceAll(",|\\s|\\[|\\]", ""))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM