1.输入输出
package com.scanner;
//1.导包
import java.util.Scanner;
public class Demo1 {
public static void main() {
print();
}
//需求:我是一个零基础小白,请帮我写一个程序,可以让用户键盘输入用户名和年龄,然后打印出来。
public static void print() {
//2.创建对象(得到一个scanner扫描对象)
Scanner sc = new Scanner(System.in);
//3.获取用户输入
System.out.println("请输入用户名:");
//程序在这一行暂替,等到用户输入回车将变量交给username后继续运行。
String username = sc.nextLine();
System.out.println("请输入年龄:");
int age = sc.nextInt();
//打印
System.out.printf("用户名:%s,年龄:%d\\n", username, age);
}
}
2.运算符
(1)基本算数运算符
package com.operator;
public class Demo1 {
public static void main(String[] args) {
//目标:搞清楚基础的算数运算符。
print(10,2);
System.out.println("==================");
print(10,3);
}
public static void print(int a,int b) {
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(1.0 * a / b);
System.out.println(a % b);
}
}
‘+’符号在JAVA中有特殊的用途
public static void main() {
int a = 5;
System.out.println(1 + a);//6
System.out.println("bc" + a);//bc5
System.out.println("a" + 1);//a1
System.out.println(1 + 2 + "a");//3a
System.out.println('a' + a + "CC");//102CC
}
(2)自增自减运算符
package com.operator;
public class Demo2 {
public static void main(String[] args) {
//目标:掌握自增自减运算符的使用。
print(10);
print2(10);
}
//设计一个方法,接受一个整数
public static void print(int a) {
//打印a的值,再打印a+1的值,再打印a的值,再打印a-1的值。
++a;
System.out.println(a);//11
a++;
System.out.println(a);//12
--a;
System.out.println(a);//11
a--;
System.out.println(a);//10
}
//设计一个方法,理解自增自减放在白能量前后的区别。
public static void print2(int a) {
int b = a++;//先加后用
System.out.println(a);//11
System.out.println(b);//10
int c = ++a;//先用后加
System.out.println(a);//12
System.out.println(c);//12
}
}
(3)赋值运算符
扩展的赋值运算符隐含了强制类型转换
package com.operator;
public class Demo3 {
public static void main(String[] args) {
//目标:掌握扩展的赋值运算符
receive(100);
}
//需求:收红包
public static void receive(int money) {
int a = 100;
money += a;
System.out.println("收了"+money+"元");
byte a1 = 100;
byte a2 = 20;
a1 += a2;//等价于 a1 = (byte)(a1 + a2);
System.out.println(a1);
}
}
(4)关系运算符
package com.operator;
public class Demo4 {
public static void main(String[] args) {
//目标:理解关系运算符的使用。
print(10,20);
}
public static void print(int a,int b) {
System.out.println(a>b);//false
System.out.println(a<b);//true
System.out.println(a>=b);//false
System.out.println(a<=b);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
}
}
(5)三元运算符
package com.operator;
public class Demo5 {
public static void main(String[] args) {
//目标:掌握三元运算符。
print(10,20);
receive(90);
System.out.println(getMax(10,20,30));
}
public static void print(int a,int b) {
int max = a > b ? a : b;
System.out.println(max);
}
//需求:判断成绩是否会挂科
public static void receive(int money) {
String result = money >= 60 ? "及格" : "不及格";
System.out.println(result);
}
//需求:求三个整数的较大值返回
public static int getMax(int a,int b,int c) {
int max = a > b ? a : b;
max = max > c ? max : c;
return max;
}
}
(6)逻辑运算符
package com.operator;
public class Demo6 {
public static void main(String[] args) {
//目标:掌握逻辑运算符
receive(1500);
receive2(1500);
receive3(1500);
receive4(1500);
}
public static void receive(int money){
if(money>=1000 && money<=2000){
System.out.println("恭喜你,获得100积分");
}else if(money>=2000 && money<=3000){
System.out.println("恭喜你,获得200积分");
}else if(money>=3000 && money<=4000){
System.out.println("恭喜你,获得300积分");
}else{
System.out.println("恭喜你,获得400积分");
}
}
//或
public static void receive2(int money){
if(money>=1000 || money<=2000){
System.out.println("恭喜你,获得100积分");
}else if(money>=2000 || money<=3000){
System.out.println("恭喜你,获得200积分");
}else if(money>=3000 || money<=4000){
System.out.println("恭喜你,获得300积分");
}else{
System.out.println("恭喜你,获得400积分");
}
}
//非
public static void receive3(int money){
if(!(money>=1000)){
System.out.println("恭喜你,获得100积分");
}else{
System.out.println("恭喜你,获得400积分");
}
}
//异或
public static void receive4(int money){
if(money>=1000 ^ money<=2000){
System.out.println("恭喜你,获得100积分");
}else{
System.out.println("恭喜你,获得400积分");
}
}
}
运算符原理
左移依次*2,右移依次/2
标签:JAVA,int,输入输出,System,运算符,static,println,public,out From: https://blog.csdn.net/2301_80187237/article/details/142965646