码迷,mamicode.com
首页 > 编程语言 > 详细

Java variables 01

时间:2016-08-19 11:16:45      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

Understanding : 

At the very beginning, I should understand clearly that every program can be understood as a progress that make a record of some inputs in order to calculate, then output the result.

And the most important thing to learn Java is you have the ability to read codes.

Then let me begin to learn Java! o(* ̄▽ ̄*)o

The first Java program, of course, is "Hello World!":(IDE:eclipse)

  The codes:

package hello;

public class Hello {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello World!");
		
	}

}

  Let me show the catalogue:

技术分享

We can see our Hello.java program is in the package "hello", (Up to now, I don‘t know why there is a package, however, I can learn why later) It can also throw the package in this program.

Let me add something into this easy program in order to make it more fantastic —— the "yesman" program.

  The codes:

package hello;

import java.util.Scanner;  // 这里就出现了调用import,类似于C里的include 

public class Hello {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello World!");
		Scanner in = new Scanner(System.in);   // 输入语句 
		System.out.println(in.nextLine());   //输入一整行 
	}

}

  The Output:

  技术分享

We can learn something basic about string from "yesman" program:

System.out.println("2+3="+5);  ===> 2+3=5

System.out.println("2+3="+2+3); ===> 2+3=23

System.out.println("2+3="+(2+3)); ===> 2+3=5 (字符串+数字===>全部看作字符串)

System.out.println(2+3+"=2+3="+(2+3)); ===> 5=2+3=5 (运算+字符串+运算)===> 先计算后当作字符串输出)

System.out.println(2+3+"=2+3="+2+3); ===> 5=2+3=23 

And we can learn how to define variables from this program:

package hello;

import java.util.Scanner;

public class Hello {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int price;
		price = in.nextInt();
		System.out.println("100-"+price+"="+(100-price));
	}

}

  The Output:

  技术分享

define constants: final int amount = 100; (final为常量关键字)

define float number: double variables;

Change double type to int type:(把double类型强制转化为int类型) (int)meter; (meter 以前是double类型)

Attention:

We often use this sentence to judge two doubles are equal: Math.abs(f1 - f2) < 0.00001; 

Java variables 01

标签:

原文地址:http://www.cnblogs.com/yojiaku/p/5786704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!