【问题标题】:Counting actors and storing and displaying their name and role计算演员并存储和显示他们的姓名和角色
【发布时间】:2015-11-09 14:27:43
【问题描述】:

我是 Java 新手,我正在尝试从不同的网站和视频中学习,但我已经被一个问题困住了几天,我想知道任何人都可以帮忙。我想做的是询问用户一部电影中有多少关键演员。然后我想问演员的名字和他们在电影中扮演的角色,在最终显示演员的名字和他的角色之前,应该询问每个演员,无论用户指定的电影中有多少人?

import java.util.Scanner;

public class rough {

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


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


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

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


            System.out.print(actor + " - " + character);

        }

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

}

【问题讨论】:

  • 您使用float 代替String 作为文本值有什么特别的原因吗?我认为你应该看看 thisthis
  • 演员25.42太棒了。

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


【解决方案1】:

这就是我会做的:

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 循环之外并将角色/演员存储到某个输出字符串。此外,我看不出你为什么对角色/演员使用浮点变量,字符串是最合乎逻辑的。

【讨论】:

    【解决方案2】:

    所以你应该这样做:

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

    【讨论】:

    • 它将为您工作 - 您必须将方法“nextFloat()”更改为“next()”。它允许您读取字符串。
    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多