gpt4 book ai didi

java - 如何使用数组/数组列表将值更改为星号

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:15:34 24 4
gpt4 key购买 nike

我是 Java 的新手,我正在尝试使用数组和数组列表编写一个程序,您可以在其中输入任意数量的值,然后使用星号输出两个参数之间的值数量。例如:

[5,14,23,43,54,15]
1-10: *
11-20: **
21-30:*
31-40:
41-50:*
51-60: *

等等。到目前为止,这是我所拥有的,但我遇到了错误和越界异常。谁能说我是否在正确的轨道上?感谢您的帮助!

package arraylists;

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

public class numberslists {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
ArrayList numbers = new ArrayList();
int [] number = new int[10];
int x, count = 0;
System.out.println("how many numbers would you like?");
count = reader.nextInt();
System.out.println("enter in those numbers please");
for (x=0; x < count; x++){
number[x] = reader.nextInt();
numbers.add(number[x]);
}
System.out.println(numbers);
int x10 = numbers.indexOf(number[x] < 10);
numbers.remove(x10);
System.out.println(numbers);
}
}

最佳答案

简而言之,正如拉希鲁所说,您需要更改行:int x10 = numbers.indexOf(number[x] < 10);

您的代码的主要问题是表达式 number[x] < 10它返回一个 boolean 值(真或假)。因此 numbers.indexOf(number[x] < 10)将返回 1 或 -1。

最后,当代码到达 numbers.remove(x10); 时如果是 -1(假)那么你会得到 java.lang.ArrayIndexOutOfBoundsException因为没有办法做 numbers.remove(-1); .查看documentation .

您的代码还有改进的余地。以下是您可以做什么的建议。但是,请在尝试修复自己的代码后查看此建议(这样您可以获得更好的学习体验)。

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

public class CountOcurrancesInArray {

private static Scanner reader = new Scanner(System.in);
private static List<Integer> numbers = new ArrayList<Integer>(); // Use generics when possible: <Integer>
public static void main(String[] args) {
int x, count = 0;
System.out.println("how many numbers would you like?");
count = reader.nextInt();
System.out.println("enter in those numbers please");
for (x=0; x < count; x++){
// I don't see a need for this line. number[x] = reader.nextInt();
numbers.add(reader.nextInt());
}
System.out.println(numbers);
int[] comparingNumbers = requestComparingNubers();
System.out.println("You entered these numbers: " + numbers);
String matchingNumbers = checkForNumbersInTheList(comparingNumbers);
System.out.println("Numbers between " + comparingNumbers[0] + "-" + comparingNumbers[1] + ":" + matchingNumbers);
}

/**
* Counts how many entries are in the list between 'comparingNumbersInput'
* @param comparingNumbersInput
* @return number of entries as asterisks "*"
*/
private static String checkForNumbersInTheList(int[] comparingNumbersInput) {
String result = "";
for(Integer i : numbers) {
if (i >= comparingNumbersInput[0] && i <= comparingNumbersInput[1]) {
result += "*";
}
}
return result;
}

/**
* Asks the user to enter 2 numbers to be compared against the all the numbers in the list.
* @return returns a int[2] sorted ascendingly
*/
private static int[] requestComparingNubers() {
int [] result = new int[2];
System.out.println("Counting how many numbers there are in between x and y.");
System.out.println("What is the first number?");
result[0]=reader.nextInt();
System.out.println("What is the second number?");
result[1]=reader.nextInt();
// Sort comparingList
if (result[0] > result[1]) {
int temp = result[1];
result[1] = result[0];
result[0] = temp;
}
return result;
}
}

关于java - 如何使用数组/数组列表将值更改为星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com