<span style="font-family: Arial, Helvetica, sans-serif;"></span><div>
</div>
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("你好");
// Scanner in = new Scanner(System.in);
// System.out.println("echo:" + in.nextLine());
System.out.println("2+3="+5);
}
}
知识点:println里面“+”做为连接,输出结果:
你好
2+3=5
知识点:ctrl+/ 将代码切换为注释
写程序要习惯所有的事情都用键盘处理,摆脱鼠标,提高效率。
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("2+3="+2+3);
}
}
输出结果:2+3=23
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("2+3="+(2+3));
}
}
输出结果:2+3=5package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(2+3+"=2+3="+(2+3));
}
}
输出结果:5=2+3=5
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
<span style="color:#ff0000;">int price;</span>
price = in.nextInt();
System.out.println("100-"+price+"="+(100-price));
}
}
<span style="font-size:14px;">package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入消费金额:");
Scanner in = new Scanner(System.in);
int price;
price = in.nextInt();
System.out.println("100-"+price+"="+(100-price));
}
}
</span>
输出结果:
请输入消费金额:
39
100-39=61
变量定义及赋值:
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入金额");
Scanner in = new Scanner(System.in);
<span style="color:#ff0000;">int amount = 100;</span>
<span style="color:#ff0000;">int price = 0;</span>
price = in.nextInt();
<span style="color:#ff0000;">System.out.println(amount + "-" + price + "=" + (amount - price));</span>
}
}
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入金额");
Scanner in = new Scanner(System.in);
<span style="color:#ff0000;">final int amount = 100;</span>
int price = 0;
price = in.nextInt();
System.out.println(amount + "-" + price + "=" + (amount - price));
}
}
更进一步,amount的值也是读取用户的输入:
package helloworld;
import java.util.Scanner;
public class hellll {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>System.out.println("请输入面额:");
<span style="white-space:pre"> </span>Scanner in = new Scanner(System.in);
//<span style="white-space:pre"> </span>final int amount = 100;
<span style="white-space:pre"> </span>int amount = 0;
<span style="white-space:pre"> </span>int price = 0;
<span style="white-space:pre"> </span>amount = in.nextInt();
<span style="white-space:pre"> </span>System.out.println("请输入消费金额:");
<span style="white-space:pre"> </span>price = in.nextInt();
<span style="white-space:pre"> </span><span style="color:#ff0000;">System.out.println("收您" + amount + "元,应收" + price + "元,找您" + (amount - price)+"元");</span>
<span style="white-space:pre"> </span>System.out.println(amount + "-" + price + "=" + (amount - price));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}
输出结果:
请输入面额:
50
请输入消费金额:
4
收您50元,应收4元,找您46元
50-4=46
再做优化:
package helloworld;
import java.util.Scanner;
public class hellll {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
// final int amount = 100;
int amount = 0;
int price = 0;
System.out.print("请输入面额:");
amount = in.nextInt();
System.out.print("请输入消费金额:");
price = in.nextInt();
System.out.println("收您" + amount + "元,应收" + price + "元,找您" + (amount - price)+"元");
System.out.println(amount + "-" + price + "=" + (amount - price));
}
}
输出结果:
请输入面额:50
请输入消费金额:44
收您50元,应收44元,找您6元
50-44=6
读输入
Scanner in = new Scanner(System.in);
任何需要读用户输入的程序都需要这行。
读入一行文字
System.out.println(in.nextint());
让in这个对像做读入下一行的动作,交给System.out这个对像去打印一行。
字符串的+
用来连接两个字符串形成更长的
System.out.println("Hello" + “World”);
读整数
price = in.nextint();
要求in这个对像做读入下一个整数的动作,读到的结果赋值给变量price
变量类型
int price = 0
这一行,定义了一个变量,变量的名字是price,类型是int,初始值是0.
Java是一种强类型语言,所有的变量在使用之前必须定义或声明,所有的变量必须具有确定的数据类型。数据类型表示在变量中可以存放什么样的数据,变量中只能存放指定类型的数据。
常量
定义常量的方法:
final amount = 100;
tips
程序要求读入多个数字时,可以在一行输入,中间用空格分开,也可以在多行输入
每次召唤in.nextint(),它就等待用户输入一个整数。