public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//int i = sc.nextInt();//键盘录入整数
//System.out.println(i);
if (sc.hasNextInt()){
int i = sc.nextInt();
System.out.println(i);
}else{
System.out.println("输入的类型错误");
}
}
//同一个对象先获取int值,在获取String值的代码,但是会出现问题
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数");
int i = sc.nextInt();
System.out.println("请输入第一个字符串");
String line = sc.nextLine();
System.out.println("i="+i+" line="+line);
}
1.true true
2.2个
3.false true
4.true true
5.false true
}
package com.heima.scanner;
import java.util.Scanner;
public class Demo2_Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i=1; i <=3 ; i++) {
System.out.println("请输入用户名:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
//如果是字符串常量和字符串变量比较,通常都是字符串常量调用方法,将变量作为参数传递,防止空指针异常
if("admin".equals(userName)&&"admin".equals(password)){
System.out.println("欢迎"+userName+"登录");
break;
}else{
if (i==3) {
System.out.println("输入错误已达上限");
}
else{
System.out.println("录入错误,您还有"+(3-i)+"次机会");
}
}
}
}
public static void main(String[] args) {
String s = "heima";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);//获取按位置索引的字符
System.out.print(c);
}
public static void main(String[] args) {
String s = "ABCDEabcd123456!@#$%^";
int big=0;
int small=0;
int num = 0;
int other=0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c>='A'&&c<='Z') {
big++;
}
else if (c>='a'&&c<='z') {
small++;
}else if (c >= '0'&&c<='9') {
num++;
}
else{
other++;
}
}
System.out.println(s+"中大写字母有"+big+"个,"+"小写字母有"+small+"个,"+"数字有"+num+"个,"+"其他有"+other+"个");
}
public static void main(String[] args) {
String s = "woaiHEimAniaima";
String s2 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s2);
}
public static void main(String[] args) {
int[] arr = {1,2,3};
String s = "[";
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
s = s+arr[i]+"]";
}else{
s= s +arr[i]+",";
}
}
System.out.println(s);
}
//字符串反转
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
char[] arr = line.toCharArray();//转换成字符数组
String s ="";
for (int i = arr.length-1; i >=0; i--) {
s = s+arr[i];
}
System.out.println(s);
}
public static void main(String[] args) {
//定义大串
String max = "woaiheima,heimabutongyubaima";
//定义小串
String min = "heima";
//定义计数器变量
int count = 0;
//定义索引
int index= 0;
//定义循环,判断小串是否在大串中出现
while ((index = max.indexOf(min))!=-1) {
count++;
max = max.substring(index+min.length());
}
System.out.println(count);
}