我如何将输入添加到“valor2” ? - 爪哇

发布于 2025-01-14 22:27:55 字数 413 浏览 1 评论 0原文

我刚开始学习java,有人知道,我怎样才能问“输入你的值”,并将 id 添加到 double[] valor2 ?

class test { // body start
    public static void main(String [] args) {
      
        double[] valor2 = {15, 4864, 4, 21}; // here is my doubt
     
        for (int a=0; a<valor2.length; a++){
            System.out.format("%s %d: O valor deste item é %10.2f %s%n", "Item", a+1, valor2[a], "reais");
        }
    }
}

I just started to learn java, someone knows, how can i ask like "Type your values", and add id to the double[] valor2 ?

class test { // body start
    public static void main(String [] args) {
      
        double[] valor2 = {15, 4864, 4, 21}; // here is my doubt
     
        for (int a=0; a<valor2.length; a++){
            System.out.format("%s %d: O valor deste item é %10.2f %s%n", "Item", a+1, valor2[a], "reais");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2025-01-21 22:27:55

使用扫描仪进行输入。

public class TypeYours {

    public static void main(String[] args) {
        // Double array to contain the values
        double[] values = new double[4];

        // The scanner take System.in (console input stream) as input.
        Scanner scanner = new Scanner(System.in); /
        
        for (int a = 0; a < values.length; a++) {
            System.out.print("Enter a price:");

            // Store the scanned value
            values[a] = scanner.nextDouble();

            // Some %s is unnecessary so I removed them. 
            // Also, not %n but \n to start a new Line
            System.out.format("Item %d: O valor deste item é %10.2f reais\n", a + 1, values[a]);
        }
    }

}

请注意,这是一个非常简单的示例:1)double[] 的大小在源代码中硬编码为 4。如果您想动态更改大小,请使用 ArrayList或其他替代。 2) Scanner 也可以接受 intfloatString,请在您的男女混合课程中尝试一下。 3) System.out.format 可以替换为 System.out.print()String.format(format, ...args) 的组合,更加灵活。

Use a Scanner for inputs.

public class TypeYours {

    public static void main(String[] args) {
        // Double array to contain the values
        double[] values = new double[4];

        // The scanner take System.in (console input stream) as input.
        Scanner scanner = new Scanner(System.in); /
        
        for (int a = 0; a < values.length; a++) {
            System.out.print("Enter a price:");

            // Store the scanned value
            values[a] = scanner.nextDouble();

            // Some %s is unnecessary so I removed them. 
            // Also, not %n but \n to start a new Line
            System.out.format("Item %d: O valor deste item é %10.2f reais\n", a + 1, values[a]);
        }
    }

}

Note that this is a very simple example: 1) The size of the double[] is hardcoded in the source as 4. If you want to dynamically change the values's size, use ArrayList<Double> or some other instead. 2) Scanner can take int, float, String as well, try them in your coed. 3) System.out.format can be replaced by the combination of System.out.print() and String.format(format, ...args), which is more flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文