java.util.Scanner获取用户的输入(Java5的新特征)
import java.util.Scanner;
Scanner s = new Scanner(System.in);
next()
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方法接收:");
if(scanner.hasNext()){
String str = scanner.next();
System.out.println("输入的内容为:"+str);
}
//下面是自己练习的时候发现:输入Hello World的话,上下两个分别输出Hello和World,没有空格,可以理解成,上面的next接收之后,scanner会把多余的空格和剩余的单词保留下来;第二次接收的时候会对剩下的内容进行接收,但还是会省略空格;若是下面改成nextLine接收,则会出现 World含空格。
if(scanner.hasNext()){
String str = scanner.next();
System.out.println("输入的内容为:"+str);
}
scanner.close();
//凡是属于IO流的类如果不关闭会一直占用资源,要养成良好的习惯用完就关掉。
nextLine()
Scanner scanner = new Scanner(System.in);
System.out.printlin("使用nextLine方法接收:");
if(scanner.hasNext()){
String str = scanner.nextLine();
System.out.println("输入的内容为:"+str);
}
//自己练习的时候发现,如果上一次将全部输入都用光的话,下一次执行会要求用户再次输入,以进行新的输出
if(scanner.hasNext()){
String str2 = scanner.nextLine();
System.out.println("输入的内容为:"+str2);
}
//问题:scanner每次被输入的时候会被刷新吗?就是重新输入的时候scanner会清零,但是仍旧识别为有文字的状态?
scanner.close();
瞎搞的判断
public static void main(Sring[] 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);
}esle{
System.out.println("输入的不是整数!");
}
System.out.println("请输入小数:");
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("输入的小数为:"+f);
}esle{
System.out.println("输入的不是小数!");
}
}
输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并发出执行结果
public static void main(String[] args){
Scanner scanner = Scanner(System.in);
double sum = 0;
int m = 0;
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m+1;
sum = sum+x;
}
System.out.println(m+"个数字的和为:"+sum);
System.out.println(m+"个数字的平均值为:"+(sum/m));
}
Java的基本结构就是顺序结构
if单选泽结构(不含else)/双选择结构(含else)
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
if(s.equals("Hello")){
System.out.println("Hello");
}else {
System.out.println("End");
scanner.close();
}
System.out.println("-------------------------------------------------------------")
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your score:");
int score = scanner.nextInt();
if(score>=60){
System.out.println("Congratulations!You pass the examination!");
}else{
System.out.println("What a pity!You don‘t pass the examination!");
}
if多选结构(else if)
Scanner scanner = new Scanner(System.in);
System.out.println("Please input your score:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("Congratulations!You have killed it!");
}else if (score>=90 && score<100){
System.out.println("Congratulations!Your class is A!");
}else if (score>=80 && score<90){
System.out.println("Congratulations!Your class is B!");
}
else if (score>=0 && score<80){
System.out.println("Your class is C!");
}else{
System.out.println("Illegal input!");
}
scanner.close();
嵌套的if结构
switch多选择结构
//case穿透 没有break会被击穿的
char grade = ‘C‘;
switch (grade){
case ‘A‘:
System.out.println("Excellent!");
break;
case ‘B‘:
System.out.println("Good!");
break;
case ‘C‘:
System.out.println("OK!");
break;
case ‘D‘:
System.out.println("Alright!");
break;
case ‘F‘:
System.out.println("Alright!");
break;
default:
System.out.println("None.");
从JDK7开始 支持字符串类型了
String name = "Faye";
//反编译 java->class 通过IDEA进行反编译 class丢进来
switch (name){
case "Faye":
System.out.println("Godness!");
break;
default:
System.out.println("Nobody!");
break;
while循环
//输出1~100
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
do···while循环:即使不满足条件,也至少执行一次
int i = 0;
int sum = 0;
do{
i++;
sum = sum+i;
}while(i<100);
System.out.println(sum);
for循环
int a = 1;//初始化条件
while (a<=100){//条件判断
System.out.println(a);//循环体
a+=2;//迭代
}
System.out.println("while循环结束");
//
for(int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
/*
* 最先执行的初始化步骤,可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句。
* 然后,检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句
* 执行一次循环后,更新循环控制变量
* 再次检测布尔表达式,循环执行上面的过程
* */
System.out.println("-------------------------------------------------------------")
int s1 = 0;
int s2 = 0;
for (int i = 0;i<=100;i++){
if (i%2==0){
s2 = i+s2;
}else{
s1 = i+s1;
}
}
System.out.println("奇数和为:"+s1);
System.out.println("偶数和为:"+s2);
System.out.println("-------------------------------------------------------------")
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");//println输出之后会换行 print输出之后不会换行
}
if (i%15 == 0){
System.out.println("\n");
}
System.out.println("-------------------------------------------------------------")
/*打印第一列
* 用一个j把循环包起来
* 去掉重复
* 调整样式
* */
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
System.out.println("-------------------------------------------------------------")
int[] numbers = {10,20,30,40,50};//定义了一个数组
for (int i = 0;i<5;i++){
System.out.print(numbers[i]+"\t");
}
System.out.print("\n");
//遍历数组元素
for (int x:numbers){
System.out.println(x);
}
增强for循环
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i == 30){
break;//强制跳出整个循环,不执行剩余的语句
}
}
System.out.println("-------------------------------------------------------------")
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;//用于终止某次循环
}
System.out.print(i+"\t");
}
打印三角形
//打印三角形
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/YabinWang/p/13292978.html