JAVA基础2
1、用户交互Scanner
- java.util.Scanner 获取用户的输入
- 通过next()和 nextLine()方法获取输入的字符串
- next():将空格作为结束符
- nextLine():将回车作为结束符
- 读取前,通过hasNext()和hasNextLine()方法来判断是否还有输入的数据。
package cn.wisdex.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(); //输入为:hello world时
System.out.println("输入的内容为:"+str); //输出为:hello
}
//凡是属于IO流的类,会一直占用资源,用完就关养成好习惯!
scanner.close();
}
}
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(); //输入为:hello world时
System.out.println("输出的内容为:"+str); //输出为:hello world
}
scanner.close();
}
}
import java.util.Scanner;
public class Demo03 {
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.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("整数数据:"+ f);
}else{
System.out.println("输入的不是小3.2数数据!");
}
scanner.close();
}
}
import java.util.Scanner;
public class Demo04 {
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; //sum+=x
}
System.out.println(m + "个数的和为:"+sum);
System.out.println(m + "个数的平均值为:"+(sum/m));
scanner.close();
}
}
2、三种结构
public class SequenceDemo {
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println("6");
}
}
-
b、选择结构
判断范围用if,匹配具体的值用switch
- if单选结构
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(); //equals:判断字符串是否相等 if(s.equals("Feild")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
- if双选结构
import java.util.Scanner; public class ifDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); while(scanner.hasNextFloat()) { float score = scanner.nextFloat(); if (score < 90) { System.out.println("不及格"); } else { System.out.println("及格"); } } scanner.close(); } }
- if多选择结构
import java.util.Scanner; public class ifDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); while(scanner.hasNextFloat()) { float score = scanner.nextFloat(); /** * if语句中至多有1个else语句,其他中间有else if语句 * 一旦一个语句为真,后面的都跳过执行 */ if (score < 60) { System.out.println("不及格"); } else if(score < 101){ System.out.println("及格"); } else { System.out.println("输入错误!"); } } scanner.close(); } }
- 嵌套的if结果
- switch多选择结构
public class SwitchDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩等级:"); String score = scanner.next(); //switch 匹配一个具体的值 switch (score){ //case穿透,若要中止加break 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; } scanner.close(); } }
public class SwitchDemo02 { public static void main(String[] args) { //JDK7新特性,表达式结果可以是字符串! //字符的本质还是数字 String name = "Feild"; switch(name){ case "wisdex": System.out.println("智德信"); break; case "Feild": System.out.println("Feild"); break; default: System.out.println("知不道!"); } } }
-
c、循环结构
- while循环
- 为真才循环,先判断后执行
public class WhileDemo01 { public static void main(String[] args) { System.out.println("输出0~99:"); int i = 0; while(i<100) { System.out.println(i++); } System.out.println("-----------"); System.out.println("输出1~100:"); int j = 0; while(j<100){ System.out.println(++j); /**死循环 * while(true) { * 等待客户连接 * 定时检查 * 服务器的请求响应监听 * } */ } } }
public class WhileDemo02 { //分步计算1+2+3+...+100=? public static void main(String[] args) { int i = 0; int sum = 0; while (i<100){ sum += ++i; System.out.println("i为"+i+"时,和为:"+sum); } } }
- do…while循环
- 至少先执行一次,先执行后判断
public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum += ++i; System.out.println("i为"+i+"时,和为:"+sum); }while (i<100); } }
- for循环(重要)
- java5中加入,主要用于数组或集合
- for循环语句是支持迭代的一种通用结构
- 最有效,最灵活的循环结构
- 快捷方式:后开.for
public class ForDemo01 { public static void main(String[] args) { int sum = 0; //初始化//条件判断//迭代 for(int i=0;i<=100;i++){ sum += i; System.out.println("i为"+i+"时,和为:"+sum); } // for(int i=0;i<=100;sum += ++i){ // System.out.println("i为"+i+"时,和为:"+sum); // } /** * 死循环 * for( ; ;){} */ } }
public class ForDemo02 { public static void main(String[] args) { //计算1~100之间的奇数和偶数的和 int evensum = 0; int oddsum =0; //快捷方式:100.for for (int i = 0; i <= 100; i++) { if(i%2==0){ evensum += i; }else{ oddsum += i; } } System.out.println("0到100之间的偶数和为:"+evensum); System.out.println("0到100之间的奇数和为:"+oddsum); } }
public class ForDemo03 { public static void main(String[] args) { //循环输出0~1000之间能被5整除的数,且每行输出3个 // int fives = 0; // int count = 0; // // for (int i = 1; i <= 1000; i++) { // if(i % 5 == 0){ // fives = i; // count++; // while(count>3){ // System.out.println(); // count = 1; // } // System.out.print(fives+","); // // }else // continue; // } for (int i = 0; i < 1000; i++) { if(i % 5 == 0){ System.out.print(i+"\t"); } if(i % 15 == 0){ System.out.print("\n"); } } } }
public class ForDemo04 { /*打印99乘法表 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 */ public static void main(String[] args) { // int x = 2; // for (int j = 1; j < 10; j++) { // for (int i = 1; i < x; i++) { // System.out.print(i+"*"+j+"="+i*j+"\t"); // } // x++; // System.out.println(); // } for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(i+"*"+j+"="+(i*j)+"\t"); } System.out.println(); } } }
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); } } }
public class ForDemo06 { public static void main(String[] args) { //打印5行的等边三角形 /* #####$ ####$$$ ###$$$$$ ##$$$$$$$ #$$$$$$$$$ */ for (int i = 0; i < 5; i++) { for (int j = 5; j > i; j--) { System.out.print("#"); } for(int y=0;y<(2*i+1);y++){ System.out.print("$"); } System.out.println(); } } }
- while循环
3、中断、继续
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if(i == 30){
break; //强制中止循环
}
}
System.out.println("继续加油!"); //程序继续顺序执行
}
}
public class ContinueDemo {
public static void main(String[] args) {
//打印1~100中不含10倍数的所有数
int i = 0;
while (i<100){
i++;
if(i % 10 == 0){
System.out.println();
continue; //立即回城,不执行后面的程序了
}
System.out.print(i+"\t");
}
}
}
public class LabelDemo {
public static void main(String[] args) {
//打印101~150之间的所有质数
outer:for (int i = 101; i < 150; i++) {
for(int j = 2; j < i/2 ;j++) {
if (i % j == 0) {
continue outer;
}
}
System.out.print(i+"\t");
}
}
}