计算演员并存储和显示他们的名字和角色

时间:2015-11-09 14:27:44

标签: java for-loop java.util.scanner

我是Java的新手,我正在尝试学习不同的网站和视频,而且我已经坚持了几天的问题,我想知道任何人都可以提供帮助。我想要做的是询问用户电影中有多少关键角色。然后我想问一下演员的名字和他们在电影中扮演的角色,这应该被问到每个演员,因为在最终显示演员姓名和他的角色之前,用户指定的用户是多少?

LazyFixture

3 个答案:

答案 0 :(得分:3)

这就是我要做的事情:

import javax.swing.*;

public class rough {

    public static void main(String[] args) {
    // TODO Auto-generated method stub


    int actorCount;
    String output = "";

    actorCount = Integer.parseInt(JOptionPane.showInputDialog(null,"How many actors are in the film? "));

    for (int k = 1; k <= actorCount ; k++)
    {
        String actor, character; 
        actor = JOptionPane.showInputDialog(null, "What is the actors name?");
        character = JOptionPane.showInputDialog(null, "What is " +actor + "s character?");


        output +=  actor + " - " + character + "\n";
    }

    JOptionPane.showMessageDialog(null, output);
    }
}

请记住,如果您希望同时打印所有角色/角色,则必须将打印放在for循环之外,并将角色/角色存储到某个输出String。此外,我不明白为什么你为角色/角色使用了一个浮点变量,而String最合乎逻辑。

答案 1 :(得分:0)

我真的只是想评论而不是回答你的问题,因为我没有看到问题,但我没有足够的声誉对你的帖子发表评论。

所以我假设你的问题是你试图输入一个字符串的人名,你得到InputMismatchException。

  1. 您不需要两个扫描仪对象
  2. 您使用String not float读取文本,因此我建议您更改
  3. 浮动演员,角色; to String actor,character;

    也改变了actor = kb.nextFloat(); to actor = kb.nextLine();并改变 character = kb.nextFloat(); to character = kb.nextLine();

答案 2 :(得分:0)

所以你应该这样做:

public static void main(String[] args) {
     int actorCount;

    Scanner input = new Scanner(System.in);
    Scanner kb = new Scanner(System.in);


    System.out.println("How many actors are in the film? ");
    actorCount = kb.nextInt();

    String[][] actorValues = new String[actorCount][2];

    for (int k = 0; k < actorCount ; k++)
    {
        String actor, character; 
        System.out.println("What is the actors name? ");
        actor = kb.next();

        System.out.println("What is " + actor + "'s character?");
        character = kb.next();


        System.out.print(actor + " - " + character +"\n");
        actorValues[k][0] = actor;
        actorValues[k][1] = character;

    }


    //Printout the Characters in the List
    System.out.println("All Actors:");
    for (int i = 0; i < actorValues.length; i++) {
        String[] strings = actorValues[i];
        System.out.println("Actorssname: "+strings[0]+" Character:"+strings[1]);

    }

    kb.close();
    input.close();
}