简体   繁体   English

如何与 Java Scanner 类交互

[英]How to interact with Java Scanner Class

I am super newb with Java and I am trying simple codes with Atom editor (osx).我是 Java 的超级新手,我正在尝试使用 Atom 编辑器 (osx) 的简单代码。 Please, someone can help me to understand how can I interact with code with Atom?拜托,有人可以帮助我了解如何与 Atom 的代码交互?

I use Script package (Atom) to run the code (cmd+i).我使用脚本包(Atom)来运行代码(cmd+i)。

For example if I use the Scanner class:例如,如果我使用 Scanner 类:

import java.util.Scanner;
public class Les {

    public static void main(String[] args) {

         String s;
         Scanner in = new Scanner(System.in);
         System.out.println("Enter a string");
         s = in.nextLine();
         System.out.println("You entered string " + s);
    }
}

the output will be "enter a string"输出将是“输入一个字符串”

Where I can insert the Strings?我可以在哪里插入字符串?

That is not a final output.那不是最终的输出。 It is the first line ( "Enter a string" ) that you printed.这是您打印的第一行( "Enter a string" )。 Further, you have to type some value under this message in your console (I wrote "hello" ) and press Enter .此外,您必须在控制台中的此消息下键入一些值(我写的是"hello" ),然后按Enter After all this, you will see your last message (I got "You entered string hello" ).完成这一切之后,您将看到最后一条消息(我收到了"You entered string hello" )。

In the console, it will look like在控制台中,它看起来像

Enter a string
hello
You entered string hello

I think you are asking about Execution of java classes using Atom Editor, the heading of the question makes it confusing.我认为您是在询问使用 Atom 编辑器执行 java 类的问题,问题的标题使它变得混乱。 As you know Atom is an editor for text files.如您所知,Atom 是文本文件的编辑器。 It is a very customizable and hackable editor and people are making packages that extend it to have more IDE-like features such as build and run commands like you're asking for.它是一个非常可定制和可破解的编辑器,人们正在制作扩展它的包,以具有更多类似 IDE 的功能,例如您要求的构建和运行命令。 If you installed proper scripts on atom for java.如果您在 atom 上为 java 安装了正确的脚本。 After you use the Keystroke for compiling ie., CTRL+i, it should be waiting on the console with string "enter a string" where you can enter here your input.在您使用按键进行编译后,即 CTRL+i,它应该在控制台上等待字符串“输入字符串”,您可以在此处输入您的输入。

Personally I recommend you using JCreator or other editor especially made for java, it makes things easier for you.我个人建议您使用 JCreator 或其他专为 Java 设计的编辑器,它使您的工作更轻松。

But if you wanted to understand what happens form the base, then try to use terminal for your code execution until you become familiar and move on to using editors later.但是,如果您想了解基础上发生了什么,那么请尝试使用终端来执行代码,直到您熟悉之后再继续使用编辑器。

this is the Atom interface where I do not find where type the input for the program.这是 Atom 界面,我找不到在哪里输入程序的输入。

在此处输入图片说明

  1. You need to install the platfrmio-ide-terminal from the install packages window.您需要从安装包窗口安装 platfrmio-ide-terminal。
  2. After writing your program toggle on your terminal using ctrl+shift+P使用 ctrl+shift+P 在终端上编写程序后切换
  3. Then write- javac "YourPackageName".java然后写- javac "YourPackageName".java
  4. Press enter and then write- java "YourPackageName"按回车然后写- java "YourPackageName"
  5. Press Enter and then you should be able to use the terminal for taking user input for your program.按 Enter,然后您应该能够使用终端为您的程序获取用户输入。
  6. Press enter to execute your code inside the terminal.按回车键在终端内执行您的代码。 enter image description here在此处输入图片说明

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

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