简体   繁体   English

我不知道如何通过 windows 命令提示符输入

[英]I don't know how to input through windows command prompt

I made a simple program on java:我在java上做了一个简单的程序:

public class HelloWorld {

    public static void main(String[]args) {
        System.out.print(" Hello World");
        int n=Integer.parseInt(args[0]);
        StdOut.println();
        StdOut.print(n);
    }
}

When I run the program through windows command prompt it just prints "Hello World" and the program ends, with no error messages, not waitting for me to input the integer.当我通过 windows 命令提示符运行程序时,它只打印“Hello World”,程序结束,没有错误消息,不等待我输入整数。 I don't know what I do wrong.我不知道我做错了什么。 Is there any trick I don't know?有什么我不知道的技巧吗?

Think of why you are passing a string array, called args, into your program.想一想为什么要将名为 args 的字符串数组传递到程序中。 Perhaps for something like arguments ?也许是为了诸如争论之类的事情?

That is an array of arguments passed into your program as strings, which is whatever follows in on the command.这是作为字符串传递到您的程序中的参数数组,它是命令后面的任何内容。 If the zeroth argument does not work, then try the first (args[1])如果第零个参数不起作用,则尝试第一个 (args[1])

Try this尝试这个

java hello 23

This passes 23 into the program as a string.这将 23 作为字符串传递到程序中。 Integer.parseInt(args[0]) parses it as an integer, and then the other lines display it. Integer.parseInt(args[0])将其解析为整数,然后其他行显示它。 Try this and come back if it still fails.试试这个,如果仍然失败就回来。

On another note - this is fairly basic and many would expect you read each line and understand what it does before coding and running it.另一个注意事项 - 这是相当基本的,许多人希望您在编码和运行之前阅读每一行并了解它的作用。 It's tempting to copy from StackOverflow and forget about it, but its not a good way to learn.很容易从 StackOverflow 复制并忘记它,但这不是一种学习的好方法。 One day you will end up running sudo rm -rf / without thinking twice ;)总有一天你会sudo rm -rf /运行sudo rm -rf / ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM