簡體   English   中英

如何將布爾答案存儲在數組中並以Java輸出?

[英]How to store boolean answers in arrays and output it in Java?

我試圖在每個循環之后在5個不同的數組位置中存儲5個不同的布爾答案(對或錯),然后制作一種方法來顯示“ true”的問題。

例如,測試運行將如下所示:

問題1:Content1〜(是對還是錯?) 錯誤

問題2:內容2〜TRUE(真或假?)

問題3:Content3〜(是對還是錯?) 錯誤

(循環完成)


問題2:Content2

(出口)

到目前為止,這是我的代碼。

import javax.swing.*;

class booleanTest {

    public static void main(String [] params) {


            String[] data = {"Test1", "Test2", "Test3", "Test4", "Test5"};
            boolean[] user = new boolean[5];

            array_input(data, user);
            System.out.println(user); // to see if it works atm

            System.exit(0);
    }

    public static String array_input(String[] a, boolean[] b) {

        String x = "";

        for (int i=0; i<a.length; i++) {

            x = JOptionPane.showInputDialog("Data: " + a[i]);

            if(x.equals("yes")) {
                b[i] = true;
            }
            else {
                b[i] = false;
            }
        }

            return x;
    }

    //public static String array_print() {

    // print the boolean + question here

    //}
}

它不起作用,我知道b [i] = true部分一定是錯誤的,我應該做其他事情嗎?

如果布爾陣列的索引處的值是true ,打印出的值String該索引數組。

public static void printTrue(boolean[] answers, String[] questions) {
    // checking both indices to avoid ArrayIndexOutOfBoundsException
    for (int i = 0; i < answers.length &&  i < questions.length; i++) {
        // if the answer is true
        if (answers[i]) {
            System.out.println(questions[i] + ": " + true);
        }
    }
}

當您說System.out.println(user); 打印類似[Z@3b9187c7 ,這是因為ObjecttoString()實現返回class name + @ + hex hashCode()

Arrays#toString方法創建的結果更具可讀性:

[false, false, false, true, true]

當您沒有其他訪問數據的方法時,只需在方法中返回值。 如果您查看代碼,會發現您甚至沒有使用返回的值,並且x的最后一個值將永遠不會有用。 在這種情況下,可以將其void方法。 當您希望它執行某種操作但不需要它返回任何值時,可以使用空方法。 您的代碼有效,因為array是一個Object並且即使在方法外也可以看到對其所做的更改。

這或多或少是我將如何實施的方法。 注意,變量名更具描述性。

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        String[] questions = {"Test1", "Test2", "Test3", "Test4", "Test5"};

        boolean[] responses = getUserResponses(questions);
    }

    public static boolean[] getUserResponses(String[] questions) {
        boolean[] responses = new boolean[questions.length]; //use the length of the other array. Don't count on it always being 5

        for (int i = 0; i< questions.length; i++) {

            String x = JOptionPane.showInputDialog("Data: " + questions[i]);

            if(x.equals("yes")) {
                responses[i] = true;
            }
            else {
                responses[i] = false;
            }
        }

        return responses;
    }
}

通常,最好不要修改參數Objects ,而要返回新的參數。 有時這樣做更有用或更有必要,但對於您而言卻並非如此。

看來您在正確的道路上。

數組創建和元素分配:您的代碼看起來不錯。 您可以像使用任何對象一樣使用new運算符創建數組,但必須像以前那樣指定數組大小。 元素分配是使用索引(整數文字或整數變量)完成的(對於上面的代碼而言)。 數組索引的范圍是0到size-1。 看來您也正確了。

打印結果:在您的方案中,您只需要其中數組值為true某些結果。 一個簡單的if(user[i])循環就可以解決問題。

for (int i = 0; i < user.length; i++) {  
  if (user[i]) {  
    System.out.println(data[i] + " = " + true);
  }
}  

暫無
暫無

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

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