Scanner对象
基本语法:
Scanner s = new Scanner(System.in);
next(),nextLine() 来获取字符串
package 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()){
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用内存,用完就关掉
scanner.close();
}
}
选择结构
if单选结构
if(布尔表达式){
true;
}
false;
//equals 判断字符串是否相等
if双选结构
if(布尔表达式){
true;
}
else {
false;
}
if多选结构
if(布尔表达式1){
布尔表达式1为true;
}
else if(布尔表达式2){
布尔表达式2为true;
}
else if(布尔表达式3){
布尔表达式3为true;
}
else {
以上布尔表达式都不为true;
}
if嵌套结构
switch多选结构
switch(expression){
case value;
//语句
break; //可选
case value;
//语句
break; //可选
//可以任意数量case
default: //可选
//语句
}
循环结构
while循环(先判断,后执行)
while (布尔表达式){
//循环语句
}
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i =0;
while(i<100){
i++;
System.out.println("i="+i);
}
}
}
do…while循环(先执行,后判断)
public class DoWhileDemo01 {
public static void main(String[] args) {
//输出1~100和
int i = 0;
int sum = 0;
do {
sum=sum+i;
i++;
}
while(i<=100);
System.out.println("sum="+sum);
}
}
do…while总是保证循环体会至少执行一次(这是与while的主要差别)
for循环(重中之重)
for(初始化;布尔表达式;迭代){
//代码语句
}
//初始化;布尔表达式;迭代
for(i=0;i<100;i++){
System.out.println(i);
}
练习题
练习1
public class Test01 {
public static void main(String[] args) {
int sum0 = 0;
int sum1 = 0;
for (int i = 0; i <= 100; i += 2) {
sum0 = sum0 + i;
}
System.out.println("sum0=" + sum0);
for (int j = 1; j <= 100; j += 2) {
sum1 = sum1 + j;
}
System.out.println("sum1=" + sum1);
System.out.println("=========================");
sum0 =0;
sum1 =0;
for (int k = 0; k <= 100; k ++) {
if(k%2==0){
sum0 = sum0 + k;
}
else{
sum1 = sum1 + k;
}
}System.out.println("sum1=" + sum1+"sum0="+sum0);
}
}
练习2
public class Test02 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.print("\n");
}
}
}
}
9*9乘法表
public class Test03 {
public static void main(String[] args) {
for(int i=1; i<=9; i++){
for(int j=1; j<=i; j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
增强for循环(循环数组和集合的)
public class ForDemo01 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50}; //定义数组
for (int i=0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("====================");
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
}
}
打印三角形
public class Test04 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
for (int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
倒三角
public class Test05 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++){
System.out.print(" ");
}
for (int x = 1; x <= 9-2*(i-1); x++){
System.out.print("*");
}
System.out.println();
}
}
}