java—读取来自多个用户的输入,并在输出中显示输入

46scxncf  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(256)

问题的摘要如下:
编写一个应用程序,显示一系列4个调查问题,调查可以是你想要的社会或政治主题,每个问题应该有3个可能的选择。最后,使用对话框询问用户是否要输入(1)对同一组问题输入另一组回答,或(2)退出。继续接受一组回答,直到用户选择退出,然后显示调查结果-对于每个问题,显示有多少用户选择了第一个、第二个或第三个选项的结果
我理解这个问题,但在最后一部分中有问题,您必须显示响应,因此我想问,当使用joptionpane的方法showinputdialog从用户获取输入时,如何获取用户或多个用户的输入并在输出中显示它们。

import javax.swing.JOptionPane;
public class Survey {
    public static void main(String[] args) {
        String q1, q2, q3, q4;
        int surveyQuestion1,surveyQuestion2,surveyQuestion3,surveyQuestion4 , selection;
        do {
            q1 = JOptionPane.showInputDialog(null, "How you feel" + "\n1. Good" + "\n2. Alright" + "\n3. Bad");
            surveyQuestion1 = Integer.parseInt(q1);

            q2 = JOptionPane.showInputDialog(null, "How is family" + "\n1. Good" + "\n2. Alright" + "\n3. Bad");
            surveyQuestion2 = Integer.parseInt(q2);

            q3 = JOptionPane.showInputDialog(null, "How is home" + "\n1. Good" + "\n2. Alright" + "\n3. Bad");
            surveyQuestion3 = Integer.parseInt(q3);

            q4 = JOptionPane.showInputDialog(null, "How is your life" + "\n1. Good" + "\n2. Alright" + "\n3. Bad");
            surveyQuestion4 = Integer.parseInt(q4);

            selection = JOptionPane.showConfirmDialog(null, "Do you want to set another response ?");
        } while (selection == JOptionPane.YES_OPTION);
       if(selection == JOptionPane.NO_OPTION){
           JOptionPane.showMessageDialog(null,"The people choose " + surveyQuestion1 + " the most from the first question ");
           JOptionPane.showMessageDialog(null,"The people choose " + surveyQuestion2 + " the most from the second question ");
           JOptionPane.showMessageDialog(null,"The people choose " + surveyQuestion3 + " the most from the third question ");
           JOptionPane.showMessageDialog(null,"The people choose " + surveyQuestion4 + " the most from the fourth question ");
       }
    }
}

谢谢:)

bvk5enib

bvk5enib1#

我正在尽我所能利用您提供的信息,但是您应该能够将输入记录在堆栈中,然后从堆栈中取出信息,然后将其放入对话框中。
注意:如果您不知道,stack.pop()将返回它删除的值。

mqxuamgl

mqxuamgl2#

我会给你提示,而不是代码,因为这是给你的练习。
编写一个应用程序,显示一系列4个调查问题,调查可以是你想要的社会或政治主题,每个问题应该有3个可能的选择。最后,使用对话框询问用户是否要输入(1)对同一组问题输入另一组回答,或(2)退出。
到目前为止,你的代码还可以。
继续接受响应集,直到用户选择退出,
这意味着您需要具有能够在每次迭代中存储4个答案的存储
它不能有静态存储,因为用户选择继续的次数可能会有所不同。
提示:思考 UDT阶级and something to store as many objects of that maybe fromjava.util
然后显示调查结果-对于每个问题,显示有多少用户选择了第一个、第二个或第三个选项的结果
这就是问题变得复杂的地方。到目前为止,它还没有提到多个用户。这有两个含义,任何一个都可以:
更简单:修改上面的内容 UDT阶级storageto have multiple answers for single user which is stored in expandable data structure possibly fromjava.util
更改main中的循环,以便您可以选择更改不同的用户名,另一个外部用户名 while\for 更困难的是:实现一种方法,使用来自 java.io 比如说一些存储设备 jsonxml 在应用程序开始读取现有数据时,获取新数据并取平均值。输出 JOptionPane 应类似于:
q1:用户数量q2:用户数量q3:用户数量q4:用户数量
应该很有趣。对于多个用户,使用选项#1,可能需要在几个小时内完成。对于#2,你至少要谈几天。

相关问题