标签:alt 质数 def http 基本 增强for循环 com 结构 复制
通过Scanner对象获得用户输入
Scanner s = new Scanner(System.in)
通过Scanner类的next()和nextline()方法类获取输入的字符串,用hasNext()和hasNextLine()判断是否还有输入的数据。
IO流使用完后需关闭,否则会一直占用资源
package com.sgkurisu.scanner;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
String str = scanner.next();
System.out.println("使用next方法输出:"+str);
}
//IO流不关闭后会一直占用资源,应用完就关闭
scanner.close();
}
}
package com.sgkurisu.scanner;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextLine()) {
String str = scanner.nextLine();
System.out.println("使用nextLine方法输出:"+str);
}
//IO流不关闭后会一直占用资源,应用完就关闭
scanner.close();
}
}
next()和nextLine()的比较:
package com.sgkurisu.scanner;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = 0;
float f = 0.0f;
//整数
System.out.println("请输入整数:");
if (scanner.hasNextInt()) {
i = scanner.nextInt();
System.out.println("输入的整数:"+i);
} else {
System.out.println("输入非整数");
}
//小数
System.out.println("请输入小数:");
if (scanner.hasNextInt()) {
f = scanner.nextInt();
System.out.println("输入的小数:"+f);
} else {
System.out.println("输入非小数");
}
scanner.close();
}
}
package com.sgkurisu.scanner;
import java.util.Scanner;
public class Demo4 {
//使用Scanner完成多个数字相加,求和、平均值,遇到非数字内容结束
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0; //记录输入了多少次
System.out.println("请输入数字:");
while (scanner.hasNextDouble()) {
m++;
sum = sum + scanner.nextDouble();
}
System.out.println("输入了" + m + "次");
System.out.println("平均值为:" + (sum / m));
scanner.close();
}
}
基本结构,从上而下,依次执行
基本语法
if (布尔表达式) {
//布尔表达式为true
.....
} else {
//布尔表达式为false
.....
}
多选择结构
if (布尔表达式) {
.....
} else if (布尔表达式) {
.....
} else if(布尔表达式) {
.....
} else {
.....
}
switch匹配具体一个值
基本语法
switch (表达式或字符) {
case value:
....
break;//可选
case value:
....
break;//可选
.....
default: //可选
....
}
注意case穿透现象
package com.sgkurisu.struct;
public class Demo2 {
public static void main(String[] args) {
char grade1 = ‘A‘;
char grade2 = ‘C‘;
switch (grade1) {
case ‘A‘:
System.out.println("优秀");
break;
case ‘B‘:
System.out.println("良好");
case ‘C‘:
System.out.println("中等");
case ‘D‘:
System.out.println("差");
default:
System.out.println("挂科");
}
System.out.println("================================");
switch (grade2) {
case ‘A‘:
System.out.println("优秀");
break;
case ‘B‘:
System.out.println("良好");
case ‘C‘:
System.out.println("中等");
case ‘D‘:
System.out.println("差");
default:
System.out.println("挂科");
}
}
}
从java SE7开始,switch开始支持字符串String类型
package com.sgkurisu.struct;
public class Demo3 {
public static void main(String[] args) {
String name = "ABC";
//java SE7之后有的特性
switch (name) {
case "ABCD" :
System.out.println("ABCD");
break;
case "ABC":
System.out.println("ABC");
break;
}
}
}
利用IDEA进行反编译
基本语法
while (布尔表达式) {
//布尔表达式为true
.....
}
注意避免死循环
package com.sgkurisu.struct;
public class Demo4 {
public static void main(String[] args) {
//计算1+2+...+100
int i = 0;
int sum = 0;
while (i <= 100) {
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
基本语法
do {
//布尔表达式为true
....
} while (布尔表达式)
package com.sgkurisu.struct;
public class Demo5 {
public static void main(String[] args) {
//计算1+2+...+100
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
} while (i <= 100);
System.out.println(sum);
}
}
不满足条件,至少执行一次
基本语法
for(初始化;布尔表达式;更新) {
//布尔表达式为true
.....
}
for循环是最有效、最灵活的循环结构
三个例子:
1、
package com.sgkurisu.struct;
public class Demo6 {
public static void main(String[] args) {
//计算0-100之间奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i%2 != 0) {
oddSum = oddSum + i;
} else {
evenSum = evenSum + i;
}
}
System.out.println("奇数的和:" + oddSum);
System.out.println("偶数的和:" + evenSum);
}
}
2、
package com.sgkurisu.struct;
public class Demo7 {
//输出0-1000能被5整除的数,每行输出3个
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");
//System.out.print("\n");
}
}
}
}
3、
package com.sgkurisu.struct;
public class Demo8 {
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 + "=" + j * i + "\t");
}
System.out.println();
}
}
}
主要用于数组或集合
package com.sgkurisu.struct;
public class Demo9 {
public static void main(String[] args) {
//增强for循环
int[] numbers = {10, 20, 30, 40};
for (int x:numbers) {
System.out.println(x);
}
System.out.println("=======================");
for (int i = 0; i < 4; i++) {
System.out.println(numbers[i]);
}
}
}
break用于跳出循环,continue终止本次循环
package com.sgkurisu.struct;
public class Demo10 {
//寻找1-1000之间的质数
public static void main(String[] args) {
int m = 0;
for (int i = 1; i <= 1000; i++) {
int j = 2;
for (; j <= i; j++) {
if (i%j == 0) {
break;
}
}
if (j == i) {
System.out.print(i + "\t");
m++;
}
if (m == 0) {
System.out.println();
m = 0;
}
}
}
}
package com.sgkurisu.struct;
public class Demo11 {
//打印三角形
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();
}
}
}
Debug用于程序的调试
标签:alt 质数 def http 基本 增强for循环 com 结构 复制
原文地址:https://www.cnblogs.com/sgKurisu/p/14394031.html