package scanner;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
package 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();
}
}
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接受有数据
int i =0;
float f =0.00f;
System.out.println("请输入整数:");
//判断输入的是不是整数
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据;"+i);
}else {
System.out.println("输入的不是整数数据:");
}
scanner.close();
}
}
package scanner;
import java.sql.SQLOutput;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接受有数据
int i =0;
float f =0.00f;
System.out.println("请输入小数:");
//判断输入的是不是小数
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数;"+f);
}else {
System.out.println("输入的不是小数数据:");
}
scanner.close();
}
}
package scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入 了多少个数字
int m = 0;
System.out.println("请输入数据");
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
m = m + 1; //m++
sum = sum + x;
}
System.out.println(m+"个数的和为:"+sum);
System.out.println(m+"个数的平均值是:"+(sum/m));
scanner.close();
}
}
package struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String str = scanner.nextLine();
//equals:判断字符串是否相等
if (str.equals("Hello")){
System.out.println(str);
}
scanner.close();
}
}
package struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int count = scanner.nextInt();
if (count <60){
System.out.println("不及格");
}else {
System.out.println("及格");
}
scanner.close();
}
}
package 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 >= 90) {
System.out.println("A");
} else if (score >= 80 && score <= 90) {
System.out.println("B");
} else {
System.out.println("c");
}
scanner.close();
}
}
package struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//
char grade = 'F';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("不及格");
break;
}
}
}
package struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1到100
int i =0;
while(i<100){
i++;
System.out.println(i);
}
}
}
package struct;
public class WhileDemo02 {
public static void main(String[] args) {
int i =0;
int sum = 0;
while (i <= 100){
sum=sum+i;
i++;
}
System.out.println(sum);
}
}
package struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i =0;
int sum =0;
do {
sum = sum +i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package struct;
public class ForDemo01 {
public static void main(String[] args) {
int sum = 0;
for (int a=1;a<=100;a++){
sum =sum + a;
}
System.out.println(sum);
}
}
package struct;
public class ForDemo02 {
public static void main(String[] args) {
int sumJ=0;
int sumO=0;
for (int i = 0; i <= 100; i++) {
if (i%2==0){
sumO=sumO+i;
}else{
sumJ=sumJ+i;
}
}
System.out.println(sumO);
System.out.println(sumJ);
}
}
package struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
}
}
}
package struct;
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
// 1X1=1
// 2X1=2 2X2=4
// 3X1=3 3X2=6 3X3=9
// 4X1=4 4X2=8 4X3=12 4X4=16
// 5X1=5 5X2=10 5X3=15 5X4=20 5X5=25
// 6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 6X6=36
// 7X1=7 7X2=14 7X3=21 7X4=28 7X5=35 7X6=42 7X7=49
// 8X1=8 8X2=16 8X3=24 8X4=32 8X5=40 8X6=48 8X7=56 8X8=64
// 9X1=9 9X2=18 9X3=27 9X4=36 9X5=45 9X6=54 9X7=63 9X8=72 9X9=8
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"X"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
-例子:遍历数组
package struct;
public class ForDemo05 {
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]);
}
//方法二:
for (int x:numbers){
System.out.println(x);
}
}
}