简体   繁体   English

Java 数组使用用户输入和双打

[英]Java Array using User Input and Doubles

Our prompt for the problem is as follows:我们的问题提示如下:

This function will fill an array of doubles.这个 function 将填充一个双精度数组。 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.它返回一个 integer,它等于放置在数组中的值的数量。 The function should start by declaring any variables that are needed. function 应首先声明所需的任何变量。 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.至少应该有两个变量:一个用作下标的 integer 和一个用于保存用户输入的值的双精度变量。 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.只要双数不是 -99.99 就会执行的循环内部,双数应放入数组的下标 position 处,下标应递增 1,应提示用户输入另一个双数。 Once the loop is finished executing, the number of values that were placed in the array should be returned.一旦循环完成执行,应该返回放置在数组中的值的数量。

So far I have tried:到目前为止,我已经尝试过:

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));
    }
}

But am not able to compare the input to -99.99.但我无法将输入与 -99.99 进行比较。

I would really appreciate if you guys took a look at my problem.如果你们看看我的问题,我将不胜感激。 Thanks!谢谢!

You are comparing scanner ith -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));
  }
}

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

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