gpt4 book ai didi

使用用户输入和 double 的 Java 数组

转载 作者:行者123 更新时间:2023-12-01 17:00:31 25 4
gpt4 key购买 nike

我们的问题提示如下:

This function will fill an array of doubles. It takes as its argument an array of doubles: the array that will hold the numeric information. It returns an integer that is equal to the number of values that were placed in the array. The function should start by declaring any variables that are needed. At a minimum, there should be two variables: an integer that will be used as a subscript and a double that will be used to hold a value that is entered by the user. Initialize the subscript variable to the beginning of the array. Prompt the user for a double number. This value should be saved in the double variable that was declared earlier. Inside of a loop that will execute as long as the the double number is not -99.99, the double number should be put into the array at the subscript position, the subscript should be incremented by 1, and the user should should be prompted for another double number. Once the loop is finished executing, the number of values that were placed in the array should be returned.

到目前为止我已经尝试过:

import java.util.*;

public class ArrayTest {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

Scanner input = new Scanner(System.in);

int x = 0;

do {
System.out.println("Current list is " + list);

System.out.println("Enter : ");
list.add(input.next());

if (input = -99.99)

} while (x == 0);

input.close();
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
}

但我无法将输入与 -99.99 进行比较。

如果你们能看一下我的问题,我将非常感激。谢谢!

最佳答案

您正在比较扫描仪与 -99.99

public static void main(String[] args) {

List<String> list = new ArrayList<>();

Scanner input = new Scanner(System.in);

int x = 0;

do {
System.out.println("Current list is " + list);
System.out.println("Enter : ");
String next = input.next();
list.add(next);

if (Integer.parseInt(next) == -99.99) {
// do logic
}

} while (x == 0);

input.close();
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
}

关于使用用户输入和 double 的 Java 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61512197/

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