Scanner s = new Scanner(System.in);
使用Scanner.next方法
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();
}
----------------------------------------------------------------------------------------
使用next方法接收:
hello word
输出的内容为:hello
Process finished with exit code 0
使用Scanner.nextLine方法
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);
}
//凡是属于IO流的类如果不关闭会一直占用资源,所以必须要关掉
scanner.close();
}
-------------------------------------------------------------------------------------
使用nextLine方法接收:
hello Word
输出的内容为:hello Word
Process finished with exit code 0
二者比较
1.一定要读取有效字符后才可以结束输入
2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉
3.只有输入有效字符才将其后面输入的空白作为分隔符或者结束符
4.next()不能得到带有空格的字符串
1.以Enter未结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
2.可以获取空白
public static void main(String[] args) {
//创建一个扫描对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
//从键盘接收数据
int i =0;
float f =0.1f;
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("你输入的不是小数数据");
}
scanner.close();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//和
double sum = 0 ;
//计算输入多少个数字
int m= 0;
while (scanner.hasNextDouble()){
double x =scanner.nextDouble();
m++;
sum =sum+x;
}
System.out.println(m+"个数的和为:"+sum);
System.out.println(m+"平均数为:"+sum/m);
scanner.close();
}
------------------------------------------------------------------------------------
10
20
30
40
80
200
x
6个数的和为:380.0
6平均数为:63.333333333333336
Process finished with exit code 0
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("亲输入一个内容:");
String s = scanner.nextLine();
//判断字符串是否相等equals
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("end");
}
public static void main(String[] args) {
//考试分数大于60分及格,小于60分不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
Double score = scanner.nextDouble();
if (score > 60) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
public static void main(String[] args) {
//考试分数大于60分及格,小于60分不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
Double score = scanner.nextDouble();
if (score > 60) {
System.out.println("及格");
}else if (score>80&&score<90){
System.out.println("良好");
} else if (score<100&& score>90) {
System.out.println("优秀");
} else {
System.out.println("不及格");
}
scanner.close();
public static void main(String[] args) {
char grade = ‘C‘;
switch (grade){
case ‘A‘: //case穿透现象
System.out.println("优秀");
break;
case ‘B‘:
System.out.println("良好");
break;
case ‘C‘:
System.out.println("及格");
break;
case ‘D‘:
System.out.println("再接再厉");
break;
default:
System.out.println("为止等级");
}
}
while是最基本的循环
只要布尔表达式为true,循环会一直执行下去。
我们大多数情况是会让循环停止下来的,我们需要一个表达式失效的当时来结束循环。
少部分情况需要循环一直执行,比如服务器请求相应监听等
循环条件一直为true就会造成无线循环【死循环】,我们正常编程中应该尽量避免死循环,会影响程序的性能或者造成卡死崩溃
public static void main(String[] args) {
//输出1-100;
int i =0;
while(i<100){
i++;
System.out.println(i);
}
}
public static void main(String[] args) {
//初始化值 条件判断 迭代
for (int i=1;i<=100;i=i+2){
System.out.println(i);
}
}
题目1:计算0-100之间的奇数和和偶数和
public static void main(String[] args) {
int oddsum =0;
int evensum =0;
for (int i = 0; i < 100; i++) {
if(i%2==0){
evensum+=i;
}else {
oddsum+=i;
}
}
System.out.println("偶数为:"+evensum);
System.out.println("奇数为:"+oddsum);
}
----------------------
偶数为:2450
奇数为:2500
用while和for循环输出1-1000之间能被5整除的数,并且每行输出3个
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();
System.out.print("\n");
}
}
}
打印九九乘法表
public static void main(String[] args) {
for (int j = 0; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
------------------------------------------------------------------------------
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
public static void main(String[] args) {
//定义一个数组
int [] number ={10,20,30,40,50};
for(int x :number){
System.out.println(x);
}
}
1.打印三角形 5行
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/suwenwu/p/14998263.html