java-day02
自增 自减
//自增 自减
int a= 3;
int b =a++;//先打印 再增加
System.out.println(b);
int c=++a;//先增加 再打印
System.out.println(c);
输出 b=3 c=5
幂运算
//幂运算
double d = Math.pow(2,3);
System.out.println(d);
d=8.0
逻辑运算符、位运算符
&& 与
||或
!非
boolean a = false;
boolean b = true;
System.out.println( a&&b);//与 false
System.out.println(a||b);//或 true
左移<<
右移>>
System.out.println(2<<3);//0010 左移3位 0001 0000
输出16
字符串连接符
//字符串连接符 + ,只要左有string类型
int a =10 ;
int b = 20;
System.out.println(a+b);//30
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30
三元运算符
//三元运算符 ? :
int a =90;
String type = a>60?"及格":"不及格";
System.out.println(type);
输出及格
包机制
JavaDoc
package com.monster.blog.day02;
/**
* @author monster
* @version 1.0
* @since 1.7
*/
public class Doc {
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name)throws Exception{
return name;
}
}
Scanner对象
我们可以使用Scanner对象来获取用户的输入
基本语法
Scanner a = new Scanner(System.in);
next()接收
package com.monster.blog.scanner;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
//创建一个扫描对象,接受用户信息
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方法接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//使用next方法接收
String str = scanner.next();
System.out.println("输入内容为"+str);
}
scanner.close();
}
}
//输出hello
nextLine()接收
package com.monster.blog.scanner;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextline方法接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输入内容为:"+str);
}
scanner.close();
}
}
//输入内容为:hello world
package com.monster.blog.scanner;
import java.util.Scanner;
public class demo03 {
public static void main(String[] args) {
int i =0;
float j = 0.0f;
System.out.println("请输入一个整数:");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("输入整数为:"+i);
}else {
System.out.println("你输入的不是整数");
}
System.out.println("请输入一个小数:");
Scanner scanner1 = new Scanner(System.in);
if (scanner.hasNextFloat()){
j = scanner.nextFloat();
System.out.println("输入小数为:"+j);
}else {
System.out.println("你输入的不是小数");
}
scanner.close();
}
}
package com.monster.blog.scanner;
import java.util.Scanner;
public class demo04 {
public static void main(String[] args) {
//输入几个数,没输入一个数字用回车确认,求总和和平均数
double sum;
sum = 0;
int m =0;
Scanner scanner = new Scanner(System.in);
System.out.println("输入数字,回车输入下一个数字,输入其他字符停止:");
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum = sum +x;
}
System.out.println("总和为:"+sum);
System.out.println("平均值为:"+sum/m);
scanner.close();
}
}
输入数字,回车输入下一个数字,输入其他字符停止:
1
2
3
4
5
6
sf
总和为:21.0
平均值为:3.5
Process finished with exit code 0
顺序结构
package com.monster.blog.struct;
public class order {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
hello1
hello2
hello3
hello4
hello5
Process finished with exit code 0
选择结构
if单选择结构
package com.monster.blog.struct;
import java.util.Scanner;
public class ifdemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
if(s.equals("hello")){
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
请输入内容:
hello
hello
end
Process finished with exit code 0
if双选择结构
package com.monster.blog.struct;
import java.util.Scanner;
public class ifdemo02 {
public static void main(String[] args) {
//如果分数大于60 及格,否则不及格
Scanner scanner = new Scanner(System.in);
System.out.println("输入成绩:");
int score = scanner.nextInt();
if(score<60){
System.out.println("不及格");
}else {
System.out.println("及格");
}
scanner.close();
}
}
输入成绩:
60
及格
Process finished with exit code 0
if多选择结构
package com.monster.blog.struct;
import java.util.Scanner;
public class ifdemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入成绩:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("恭喜满分!");
}else if(score<100&&score>=90){
System.out.println("获得A");
}else if(score<90&&score>=80){
System.out.println("获得B");
}else if(score<80&&score>=70){
System.out.println("获得c");
}else if(score<70&&score>=60){
System.out.println("获得D");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
输入成绩:
79
获得c
Process finished with exit code 0
switch多选择结构
package com.monster.blog.struct;
public class Switchdemo01 {
public static void main(String[] args) {
char score ='A';
switch (score){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("加油");
break;
case 'E':
System.out.println("不及格");
break;
default:
System.out.println("未知");
break;
}
}
}
优秀
Process finished with exit code 0
循环结构
while
package com.monster.blog.struct;
public class whiledemo01 {
public static void main(String[] args) {
//输出1-10
int i=0;
while (i<10){
i++;
System.out.println(i);
}
}
}
1
2
3
4
5
6
7
8
9
10
Process finished with exit code 0
package com.monster.blog.struct;
public class whiledemo02 {
public static void main(String[] args) {
//输出1+2+3+···+10
int i = 0;
int sum =0;
while(i<10){
i++;
sum=sum+i;
}
System.out.println(sum);
}
}
55
Process finished with exit code 0
do...while
package com.monster.blog.struct;
public class dowhiledemo03 {
public static void main(String[] args) {
//输出1+2+3+···+10
int i = 0;
int sum =0;
do{
i++;
sum=sum+i;
}while (i<10);
System.out.println(sum);
}
}
55
Process finished with exit code 0
posted on 2021-04-20 18:43 monster-little 阅读(42) 评论(0) 编辑 收藏 举报