一、第一个Java程序
1、helloworld
public class Hello {
public static void main(String[] args){
System.out.println("Hello world!");
}
}
2、变量
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int price;
System.out.println("请输入金额:");
price = in.nextInt();
System.out.println("找零:"+(100-price));
}
}
3、赋值
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int amount;
int price;
System.out.print("请输入票面:");
amount = in.nextInt();
System.out.print("请输入金额:");
price = in.nextInt();
int change = amount - price ;
System.out.println(amount+"-"+price+"="+change);
}
}
4、浮点数
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int foot;
double inch;
foot = in.nextInt();
inch = in.nextDouble();
System.out.println("foot="+foot+" inch="+inch);
System.out.println((foot + inch/12)*0.3048);
/*
int inch;
inch = in.nextInt();
inch/12.0
*/
//浮点数计算有误差
System.out.println(1.2-1.1);
}
}
5、优先级
6、关系运算
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double a = 5.0;
int b = 5;
System.out.println(a==b);//比较浮点数和整数是否相等,结果为true
double c = 1.0;
double d = 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
System.out.println(c==b);//c和b不相等,结果为false,浮点运算有误差
System.out.println("c="+c+" d="+d);
//比较两个浮点数是否相等:
System.out.println(Math.abs(c-d)<1e-6);//c和d差的绝对值是否小于一个比较小的数,10的-6次方
}
}
7、判断
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("请投币:");
int amount = in.nextInt();
if (amount >= 10){
//打印车票
System.out.println("*****************");
System.out.println("****Java城际铁轨***");
System.out.println("***无指定座位票*****");
System.out.println("****票价10元******");
System.out.println("*****************");
//计算并打印找零
System.out.println("找零:"+(amount-10));
}
}
}
8、做判断
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final int MINOR = 35;
System.out.print("请输入你的年龄:");
int age = in.nextInt();
System.out.println("你的年龄是:"+age);
if (age < MINOR){
System.out.println("年轻是美好的!");
}
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int max;
if (x > y){
max = x;
}else{
max = y;
}
System.out.println(max);
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final double RATE = 8.25;
final int STANDARD = 40;
double pay = 0.0;
System.out.print("Enter the number of hours worked: ");
int hours = in.nextInt();
System.out.println();
if (hours > STANDARD){
pay = STANDARD * RATE + (hours - STANDARD) * (RATE * 1.5);
}else{
pay = hours * RATE;
}
System.out.println("Gross earnings:" + pay);
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final int PASS = 60;
System.out.print("请输入成绩:");
int score = in.nextInt();
System.out.println("你的成绩是:"+score);
if (score >= PASS){
System.out.println("恭喜你,成绩合格!");
}else{
System.out.println("很遗憾,成绩不及格!");
}
System.out.println("再见!");
}
}
9、嵌套和级联判断
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
System.out.println("请输入三位数:");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
int max = 0;
if (x > y){
if(x > z){
max = x;
}else{
max = z;
}
}else{
if (y > z){
max = y;
}else{
max = z;
}
}
System.out.println(max);
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y;
if (x < 0){
y = -1;
}else{
if (x == 0){
y = 0;
}else{
y = 2 * x;
}
}
System.out.println("x="+x+" y="+y);
}
}
10、多路分支 Switch Case语句
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int type = scan.nextInt();
switch (type){
case 1:
case 2:
System.out.println("你好");
break;
case 3:
System.out.println("晚上好");
case 4:
System.out.println("再见");
break;
default:
System.out.println("what are you doing?");
break;
}
}
}
标签:Java,入门,int,练习,System,Scanner,println,public,out From: https://www.cnblogs.com/feimanzhh/p/16926422.html