簡體   English   中英

創建一個在用戶輸入“ 0”時停止的數組

[英]creating array that stops when the user enters “0”

==============

我在分配給我的作業中存在一些問題。 這是用Java編寫的。

目標/要求

  • 使用WHILE循環存儲用戶輸入的整數。

  • 如果輸入的值小於0,則循環應該結束(如果輸入負值,我不知道如何讀取)

  • 程序應該輸出這些東西(到目前為止,我還沒有走,但是如果有人也可以解釋這些,那將是驚人的)

    • 偶數輸入
    • 奇數輸入數
    • 累計總數。 例如,如果輸入為1 7 2 9,則程序應打印1 8 10 19

到目前為止,這是我所擁有的,但由於尚未完成,而且很愚蠢,所以無法正常工作。

import java.util.Scanner;

public class IntegerArrayTester
{

    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);

        int total = 0;
        int[] array = new int[total];

        // Asks for user input
        System.out.println("Please enter integers. To stop, type a negative number.");
        int input = console.nextInt();
        int num = input;

        while(num >= 0) {
            for(int i = 0; i < total; i++)
                array[i] = console.nextInt();
        }

        // displays the array
        for(int k = 0; k < array.length; k++) {
            System.out.println(array[k] + " ");
        }

        console.close();
    }
}

您永遠不會更新num。 將其添加到獲取用戶輸入的循環中。

    while(num >= 0) {
        for(int i = 0; i < total; i++) {
            array[i] = num;
            num = console.nextInt();
        }
    }

如果要填充數組,還需要將total設置為大於0的值。

數組在Java中不可調整大小,您需要使用List。

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

public class IntegerArrayTester {

    public static void main(String[] args) {
        try (Scanner console = new Scanner(System.in)) {

            int nEven = 0;
            int nOdd = 0;

            List<Integer> cummulativeTotal = new ArrayList<Integer>();

            // Asks for user input
            System.out.println("Please enter integers. To stop, type a negative number.");

            int num;
            int total = 0;

            while (true) {
                try {
                    num = console.nextInt();
                } catch (InputMismatchException ex) {
                    System.err.println("The entered value is not an integer");
                    return;
                }
                if (num < 0) {
                    break;
                }
                total += num;
                cummulativeTotal.add(total);
                if (num % 2 == 0) {
                    nEven++;
                } else {
                    nOdd++;
                }
            }

            System.out.println("Number of even inputs: " + nEven);
            System.out.println("Number of odd  inputs: " + nOdd);
            System.out.println("Cummulative totals: " + cummulativeTotal.toString());
        }
    }
}

首先,不要使用數組,因為您不知道用戶將提交的數字量。

在這里,您以固定大小0初始化數組

int total = 0;
int[] array = new int[total];

所以我想您的程序在提供第一個數字ist時崩潰了嗎?

array[i] = console.nextInt();

使用動態增長的Collection,例如ArrayList等。

另外,num應該被更新...

嘗試這個

    ArrayList<Integer> ins = new ArrayList<Integer>();
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter numbers. Enter negative value to stop.");

    int evens = 0, odds = 0;

    while (scan.hasNextLine()) {
        int num = Integer.parseInt(scan.next());

        if(num<0) break;

        ins.add(num);

        if (num % 2 == 0)
            evens++;

        else
            odds++;
    }

    System.out.println("Even numbers = "+evens+"\nOdd numbers = "+odds);
    int[] totals = new int[ins.size()];
    totals[0] = ins.get(0);
    System.out.print("Cumulative totals \n" + totals[0] + " ");
    for (int i = 1; i < ins.size(); i++) {
        totals[i] = totals[i - 1] + ins.get(i);
        System.out.print(totals[i] + " ");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM