java 运行程序并观察输出为:55 4 50 19

6tqwzwtp  于 2023-01-24  发布在  Java
关注(0)|答案(5)|浏览(110)

请帮助我学习程序

我需要修改addPlayer函数以按排序顺序插入数组中的每个数字。程序应输出:-100 -45 4 19 50 55等提示:---〉重载add()方法对于维护排序顺序的列表特别有用〈------

前4步输出正常,然后不工作(((

import java.util.ArrayList;
import java.util.Scanner;

 public class Array_sort_by_add {
   // Adds playerNum to end of ArrayList
   public static void addPlayer (ArrayList<Integer> players, int playerNum) {
  int i = 1;
  boolean foundHigher = false;

  // Look for first item greater than playerNum
  foundHigher = false;
  i = 1;

  while ( (!foundHigher) && (i < players.size()) ) {
     if (players.get(i) > playerNum) {
        
         // FIXME: insert playerNum at element i
        players.add(i, playerNum); // is it connect?
        foundHigher = true;
     }
     ++i;
  }

  // FIXME: change so executes if higher number NOT found
  if (true) { // No higher number found, add at end
    players.add(new Integer(playerNum));
  
    // It should be something here  - don't know what (((
    
  }

  return;
 }

   // Prints player numbers currently in ArrayList
   public static void printPlayers(ArrayList<Integer> players) {
 // int i = 1;

  for (int i = 1; i < players.size(); ++i) {
     System.out.println(" " + i + ". " + players.get(i));
  }

  return;
    }

  // Maintains ArrayList of player numbers 
  public static void main (String [] args) {
  ArrayList<Integer> players = new ArrayList<Integer>();
  
  // adding 10 values 
  addPlayer(players, 55);
  addPlayer(players, 4);
  addPlayer(players, 33);
  addPlayer(players, 30);
  addPlayer(players, 40);
  addPlayer(players, 80);
  addPlayer(players, 70);
  addPlayer(players, 19);
  addPlayer(players, -45);
  addPlayer(players, -100);
  
  printPlayers(players);

  return;
}
 }
mm5n2pyu

mm5n2pyu1#

我读了你的代码,我建议你使用compareTo()方法来比较每个整数。
Java文档

    • 示例**
public static void addPlayer (ArrayList<Integer> players, int playerNum) {
    int index = 0;

    for (int i = 0; i< players.size(); i++) {
        if (players.get(i).compareTo(playerNum) >= 0) {
            index = i;
            break;
        } else {
            index = i +1;
        }
    }
    players.add(index, playerNum);
}

而且你需要改变这个循环的开始索引,因为否则你会跳过第一项。

    • 示例**
// Prints player numbers currently in ArrayList
public static void printPlayers(ArrayList<Integer> players) {
    for (int i = 0; i < players.size(); i++) {
        System.out.println(" " + (i+1) + ". " + players.get(i));
    }
}
    • 系统输出**
1. -100
 2. -45
 3. 4
 4. 19
 5. 30
 6. 33
 7. 40
 8. 55
 9. 70
 10. 80

Process finished with exit code 0
ykejflvf

ykejflvf2#

这就是你们班要找的东西:

import java.util.ArrayList;
import java.util.Scanner;

public class PlayerManager {
   // Adds playerNum to end of ArrayList
   public static void addPlayer (ArrayList<Integer> players, int playerNum) {
      int i;
      boolean foundHigher;

      // Look for first item greater than playerNum
      foundHigher = false;
      i = 0;

      while ( (!foundHigher) && (i < players.size()) ) {
         if (players.get(i) > playerNum) {
            players.add(i, playerNum); // fixed: insert playerNum at element i
            foundHigher = true;
         }

         ++i;
      }

      // fixed: changed so executes if higher number NOT found
      if (i == players.size()) { // No higher number found, add at end
         players.add(new Integer(playerNum));
      }
   }

   // Prints player numbers currently in ArrayList
   public static void printPlayers(ArrayList<Integer> players) {
      int i;

      for (i = 0; i < players.size(); ++i) {
         System.out.println(" " + players.get(i));
      }
   }

   // Maintains ArrayList of player numbers 
   public static void main (String [] args) {
      ArrayList<Integer> players = new ArrayList<Integer>();

      addPlayer(players, 55);
      addPlayer(players, 4);
      addPlayer(players, 50);
      addPlayer(players, 19);

      printPlayers(players);
   }
}
lymgl2op

lymgl2op3#

我发现了一个更简单的方法然后什么答案给...

public static void addPlayer (ArrayList<Integer> players, int playerNum) {
      int i;
      boolean foundHigher;

      // Look for first item greater than playerNum
      foundHigher = false;
      i = 0;

      while ( (!foundHigher) && (i < players.size()) ) {
         if (players.get(i) > playerNum) {
            // FIXME: insert playerNum at element i
            playerNum = players.set(i, playerNum);
            foundHigher = true;
         }

         ++i;
      }

      // FIXME: change so executes if higher number NOT found
      if (true) { // No higher number found, add at end
         players.add(i, playerNum);
      }
   }
gev0vcfq

gev0vcfq4#

或者只是:

public static void addPlayer (ArrayList<Integer> players, int playerNum) {
    int i = 0;
    while (i < players.size() && players.get(i) < playerNum) {
      i++;
    }
    players.add(i, playerNum);
  }
bjp0bcyl

bjp0bcyl5#

第一个 FIXME 很简单,您只需将“i”元素添加到playerNum中,但第二个 FIXME 可以通过两种方式应用。因为它符合逻辑,您可以使用不等式运算符:

while ( (!foundHigher) && (i < players.size()) ) {
     if (players.get(i) > playerNum) {
        players.add(i, playerNum); // FIXME: insert playerNum at element i
        foundHigher = true;
     }

     ++i;
   }

   // FIXME: change so executes if higher number NOT found
      if (foundHigher != true) { // No higher number found, add at end
        players.add(playerNum);
     }
   }

    // or you can use:
       if (!foundHigher == true) { 
         players.add(playerNum);
     }
   }

相关问题