ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#2 怎样编写Java程序 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#2 怎样编写Java程序

How do I create a Java application? - 002

当学习一门新的编程语言时 我们通常从”Hello World”开始
When learning a new programming language, we usually start with a simple Hello World.
有这么几个原因
There are several reasons for this.
第一 它是一个非常简单的起点
The first is its a very simple starting point.
如此简单就很难导致混乱
It’s so simple, it’s really difficult to mess up.
第二 提供一个可以被编译的简单程序去测试你的开发环境
The second is to give a simple program that can be compiled to test your environment.
如果能编译和运行这个程序 就可以排除其他可能的问题
If we can compile and run this program, its going to eliminate other possible problems.
那么 让我们看看这个“Hello World”
So let’s go look at Hello World.
我们将忽略大部分细节
We’re going to ignore most of the details.
例如 “Hello World”里面类的概念部分我们今后会讲
For example we’ll cover the class parts of hello world later.
本节课 我们将会看到在代码中添加备注的不同方式
In this lesson we’re looking at the different ways you can leave notes to yourself in code.
它们被称为注释
These are called comments.
你可以用它们来描述你的代码
You include them to describe your code.
它能让你要做的事情更加清楚
It makes it clearer what you are trying to do.
但对你的程序却没有任何影响
There’s no affect on your application.
编译时 它们会被忽略和移除
They’re ignored and get removed when you compile.
我们将会看到如何编译Java应用程序
We’ll also look at how to compile your Java application.
你需要知道编译生成了什么
You need know what compiling produces.
以及如何运行你的程序
How to run your program.
最后 我们会介绍几种从用户那里获取一些输入的方法
and finally we’ll get cover a few ways to get stuff to and from the user.
Java的注释有两种方式
A Java comment can be created in two ways.
如果你想用单行注释 你只需用两个正斜杠 即 “//”
If you want a single line comment, use two forward slashes.
两个正斜杠意味着忽略这行剩余的部分
Two forward slashes say ignore the rest of the line.
如果你想用多行注释 用一个正斜杠后面跟着一个星号 即 “/*”
When you want a comment that spans multiple lines, you need to use a forward slash followed by a asterisk.
之后的所有内容都是注释 直到编译器遇到跟着一个正斜杠的星号 即“*/”
Then everything following is a comment until the compiler reaches a asterisk followed by another forward slash.
有疑问?
Confused?
好的 这没什么可怕的
Alright, That wasn’t awful.
让我们看看“Hello World”程序中的注释
Let’s take a look at this in the hello world program.
先来创建一个java文件
To start, lets create a java file.
就是一个文本文件
It’s just a text file.
我们将它命名为HelloWorld.java H和W是大写的
We need to name it HelloWorld.java where the H and W are capitalized.
注意下大小写 因为文件名要匹配文件内部的类名
The case is important, because it needs to match the name inside the file.
查一下文件列表 文件在这里
Do a listing, and it looks like the file is there.
该编辑它了
Time to edit it.
我在Textwrangler中打开文件
I’m opening the file in Textwrangler.
你可以用任何你喜欢的文本编辑器
You can use whaterver text editor you like.
以后的课程里我们将使用IDE
In future lessons we’ll be using an IDE.
现在我们仅仅想确保java能正常工作
We just want to make sure Java works.
如果我们在IDE里做会有一个问题 那就是不能确定是IDE还是Java做的
If we do it in an IDE and there’s a problem, we couldn’t be sure it was the IDE or Java.
好的 文档应该是空白的
Ok. the document should be blank.
我要做的第一件事就是添加一个多行注释
The first thing I’ll add is a multiline comment.
这是一种跨越好几行的注释
This is a comment that spans several lines.
它开始于斜杠星 结束于星斜杠
It starts with a slash asterisk, and then ends with an asterisk slash.
全部注释从第一行到第十行
The whole comment goes from line 1 to line 10.
编译器会忽略开始和结束之间的任何东西
The compiler will ignore anything you put between the start and end.
你可以在里面随意编写
You can add anything inside.
另一个注释是单行注释
The other comment is a single line comment.
只有两个斜杠
It’s just two slashes.
如果你把注释放到下一行 Java 将无法判断要做些什么
If you put comments on the next line, Java won’t know what to do with it.
它将产生一个错误
It’s going to be an error.
这只能是单行注释
This is just for one line comments.
接下来要演示的这部分 现在还没覆盖到
So the next bit I’ll show you, we haven’t really covered yet.
不幸地是没有这个没法创建一个Java程序
Unfortunately there’s no way to create a Java program without this.
只需要知道稍后会介绍详细内容
Just know we’ll cover everything in detail later.
我们要输入“public class HelloWorld”还要带上两个大括号
We’re adding public class HelloWorld with two braces.
大括号标记了类的起止位置
The braces note the beginning and end of our class.
名字HelloWorld必须匹配文件名
The name HelloWorld must match the name of the file.
这就是我之前说的为什么这一点非常重要
That’s why I said earlier, the case is important.
在大括号里面 我们将编写程序
Inside the braces we’ll add our program.
每个Java程序的入口是一个叫做“static public void main的方法”
The start of every Java program is a method called static public void main.
在方法的起止位置也有两个大括号
This has braces to start and end the method too.
同样的稍后我们会介绍详细内容 只要知道这里是运行Java代码的入口就可以了
Again we’ll cover this in detail later, just know this is the place where Java starts
圆括号包围的部分同样也是很重要的
The part between the parentheses is important as well.
我们在这可以传入命令行参数到程序中
Basically that’s the command line arguments we can pass to our program.
同样 上面说的这些我们将在后面详细讲
Again, something we’ll cover later.
明白最重要的一点 Java在这里启动
The important take away is, Java starts running here.
在屏幕上打印东西的方法 叫做System.out.println
To print things to the screen we use a method called system dot out dot println.
这可能对你来说没什么意义
And that might not make any sense to you.
这是个先有鸡还是先有蛋的问题
This is one of those chicken-and-egg things.
我们还没有介绍类或字符串
We haven’t covered classes or strings yet.
我们一会再说 这里只要知道当你调用这个方法 圆括号内的内容会被输出
We will, but just know when you call this, what ever is inside the parenthesis gets printed.
这就是我们用来打印Hello World的程序
This is what we use to print hello world.
在方法的大括号内 添加打印到屏幕的语句
Inside the method braces, add the statement to print to the screen.
输入javac HelloWorld.java来编译程序
To compile our program we type javac HelloWorld dot java.
应该不会有任何信息
We should get no messages.
这是一件好事
That’s a good thing.
当我们列出文件列表时 有两个文件
When we do a listing now, there are two files.
Java源文件和class文件
The java file and a class file.
class文件中包含发送到JVM去运行的指令
The class file contains instructions that send to the JVM to run.
这是Java源文件编译的结果
It’s the result of compiling the Java file.
为了运行程序
To run the program.
我们输入“ java HelloWorld”
We type “java HelloWorld”.
注意大小写 但不需要加“.class”
The case is important, but we don’t need to add the dot class part.
Java可以自己识别出来
Java is smart enough to figure that out.
屏幕上将打出Hello World并且在下一行返回命令提示符
It should print Hello world and then return to the prompt on the next line.
现在假如我们想让程序输出一些其他东西该怎么办呢?
Now what if we wanted it to say something else?
让我们回去修改括号里的语句为“Hello Gracie”
Let’s go back come change the phrase to say Hello Gracie.
我们来修改它
We just change it.
保存文件
Save the file.
使用javac命令重新编译
Recompile it using the same javac command.
重新运行
And run it again.
现在输出的是“Hello Gracie”
Now it says Hello Gracie.
最后我来向你们演示 当你的程序有错误将会发生什么
The last thing I want to show you is what happens when you have an error.
让我们制造一处代码中的错误
Let’s go and introduce an error into our code.
我将删除一个分号并试着编译
I’m going to remove a semicolon and try compiling.
现在让我们编译一下
Now lets go back and compile.
当我们编译时产生了一个错误
When we compile we get an error.
错误描述为需要分号
The error says a semicolon is expected.
它甚至告诉了你 错误发生在哪一行
It even tells you the line the error happened.
如果我们把分号还原回去 又可以正常编译了
If we put the semicolon back it should compile again.
的确
And it does.
现在我们需要改变的是 如果我们想问用户的名字
Now what would we need to change, if we wanted to ask the user their name,
然后向用户问好
and then say hello to the user.
我们需要一种方法来获取用户输入
To get input, we use this commands.
看起来有点像外星语言
Again it might look like alien language
只要知道使用这些语句能让用户输入一些东西就行了
Just know that when we use these lines, it prompts the user to enter something.
让我们来修改我们的程序来询问用户名
Let’s change our application to ask the user’s name.
然后当他们敲回车以后我们打印“Hello 名字”
Then when they hit enter, we will print hello name.
名字是用户输入的文本
Name is the text the user enters.
做的时候就更清楚了 我们来看代码
It will be clearer once we do it. Let’s go to the code.
好的 首先清空我们的程序代码
OK first let’s clear out the existing code of our program.
接下来加一行用来创建一个叫Scanner的东西
Next we’re going to add a line to create something called a scanner.
这是一个用于解析基本类型和字符串的对象
This is an object can parse primitive types and strings.
我们可以解析从命令行输入的东西
We’re parsing things entered from the command line.
不用关注这些东西 只要知道这些语句从命令行获取输入就行
You don’t really need to follow this, just know these statements get input from the command
下一行获取用户输入的文本
The next line gets the text the user enters.
最后我们会打印Hello加上任何你输入的内容
Finally we print out hello whatever you typed in.
让我们编译和看看实际效果
Lets compile and see it in action.
输入名字 就是这样
Enter a name, and there it is.
Hello DJ
Hello DJ.
关于这一点 下节课再说
And on that note, let’s go to the next lesson.

发表评论

译制信息
视频概述

通过Hello World的例子循序渐进的讲述如何从零开始开发java程序,主要介绍如何创建一个类,类文件结构,命名规范以及如何获取用户输入获取数据等

听录译者

收集自网络

翻译译者

谷子

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=InoExygAFFE

相关推荐