package com.cwk.scanner;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容");
// 判断是否有输入的数据,用Scanner接受数据时,可以先判断是否为该数据类型(hasInt hasDouble)
if(scanner.hasNext()){
//next 字符串开始不会接受空格,以空格为结尾 ,即next()不包含空格
String s = scanner.next();
System.out.println(s);
}
//IO流,要及时关闭
scanner.close();
}
}
package com.cwk.scanner;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容");
// 判断是否有输入的数据,用Scanner接受数据时,可以先判断是否为该数据类型(hasInt hasDouble)
//hasNextLine 接受空格,以enter结尾
if(scanner.hasNextLine()){
String s = scanner.nextLine();
System.out.println(s);
}
//IO流,要及时关闭
scanner.close();
}
}
package com.cwk.scanner;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
//用于输入自己想要的是,避免犯错
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()){
int i = scanner.nextInt();
System.out.println("该整数是:"+i);
}else {
System.out.println("输入不是数字");
}
scanner.close();
}
}
package com.cwk.scanner;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字
//来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
double sum=0;
int m=0;
while(scanner.hasNextDouble()){
double v = scanner.nextDouble();
sum+=v;
m++;
}
System.out.println("总和为:"+sum);
System.out.println("平均数:"+sum/m);
scanner.close();
}
}
java基本结构,一条条按顺序执行
package com.cwk.struct;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//if单选择结构,if中布尔表达式为true执行大括号内容,不然跳过
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
if("hello".equals(s)){
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
package com.cwk.struct;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//if双选择结构:if else,if中布尔表达式为true执行里面内容,不然执行else内容
Scanner scanner = new Scanner(System.in);
double score = scanner.nextDouble();
if(score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
System.out.println("end");
scanner.close();
}
}
package com.cwk.struct;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//if多选择结构:if{}else if{}else{},那个为true,执行里面内容
Scanner scanner = new Scanner(System.in);
double score = scanner.nextDouble();
if(score>=80){
System.out.println("优秀");
}else if(score>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
System.out.println("end");
scanner.close();
}
}
package com.cwk.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//switch 括号中的值是int byte short char String也可以
//case 有个case穿透(遇到break前停止),想要避免的话,每个case后面加个break
char c = ‘A‘;
switch (c) {
case ‘A‘:
System.out.println("优秀");
break;
case ‘B‘:
System.out.println("良好");
break;
case ‘C‘:
System.out.println("及格");
break;
default:
System.out.println("不及格");
}
}
}
package com.cwk.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
//switch支持用字符串(hashcode 判断),可以用反编译看(从project structure看class文件位置
// 在对应的包下面点show in explorer,把class文件从文件夹移过去,在idea打开)
String s="哈哈";
switch(s){
case "你好":
System.out.println(s);
break;
case "哈哈":
System.out.println("哈哈");
break;
default:
System.out.println();
}
}
}
package com.cwk.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//算1-100
int sum=0;
int i=1;
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
package com.cwk.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
//while(先判断,看能否进循环体循环)
//do while(一定会先执行一次,看能否循环)
int sum=0;
int i=0;
do{
sum+=i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package com.cwk.struct;
public class ForDemo01 {
public static void main(String[] args) {
//初始化, 循环条件判断 条件迭代
for (int i = 0; i <=100; i++) {
System.out.println(i);
}
//死循环
for(;;){
}
}
}
package com.cwk.struct;
public class ForDemo02 {
public static void main(String[] args) {
//0-100 奇数之和,偶数之和
int sum=0;
int oddSum=0;
int evenSum=0;
for (int i = 0; i <= 100; i++) {
if(i%2==0){
evenSum+=i;
}else {
oddSum+=i;
}
sum+=i;
}
System.out.println(oddSum);
System.out.println(evenSum);
System.out.println(sum);
}
}
package com.cwk.struct;
public class ForDemo03 {
public static void main(String[] args) {
//1-1000 能除五的数,三个输出一行
int sum=0;
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
sum++;
//这个if在if内部
if(sum%3==0){
System.out.println();
}
}
}
}
}
package com.cwk.struct;
public class ForDemo04 {
public static void main(String[] args) {
//99乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
//i j 位置要分好
System.out.print(j+" * "+i+" = "+j*i+"\t");
}
System.out.println();
}
}
}
package com.cwk.struct;
public class ForDemo05 {
public static void main(String[] args) {
// 增强for循环,遍历数组或集合
int[] num={1,2,3,4,5};
for (int i : num) {
System.out.println(i);
}
}
}
package com.cwk.struct;
public class BreakDemo {
public static void main(String[] args) {
//break 强制退出循环,也可以在switch(防止case穿透)
int i = 0;
while (i <= 100) {
i++;
if (i == 30) {
break;
}
System.out.println(i);
}
//continue 用于循环,跳过某次循环未执行的部分,开始下一次循环
//输出1-100 不输出10的倍数
for (int i1 = 1; i1 <= 100; i1++) {
if (i1 % 10 == 0) {
System.out.println();
continue;
}
System.out.print(i1 + "\t");
}
System.out.println("--------------------");
//打印101-150质数
//判断出质数,怎么保存呢,可以用boolean 来保存该数是否为质数
for (int j = 101; j < 150; j++) {
boolean flag=true;
for (int j2 = 2; j2 < j / 2; j2++) {
if (j % j2 == 0) {
flag=false;
break;
}
}
if(flag){
System.out.println(j);
}
}
}
}
package com.cwk.struct;
public class Test01 {
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();
}
}
}
原文:https://www.cnblogs.com/cwenk/p/14656282.html