java 基础知识点
1、输入方式:
1传统的输入方式:
public class MainRun { /** * @param args */ public static void main(String[] args) { try { BufferedReader strin=new BufferedReader(new InputStreamReader(System.in)); System.out.print("请输入一个字符串:"); String str = strin.readLine(); System.out.println("第一个:"+str); System.out.println("请输入第二个字符串:"); String str2 = strin.readLine(); System.out.println("第2个:"+str2); } catch (IOException e) { e.printStackTrace(); } } }
2、1.5后的Scanner来:
注意的是next()和nextLine()的区别,前者是以空格为区分,而nextLine()则是以换行符为区分的
import java.util.Scanner; public class Main { public static void main(String[] args) { String a = "word"; String b = "word"; int maxStringLength = 0; Scanner sc = new Scanner(System.in); a = sc.nextLine(); b = sc.nextLine(); for(int i = 0 ; i < a.length() ; i ++) for(int size = 1 ;size <= a.length() - i ; size ++) { String subString = a.substring(i, i + size); if(b.contains(subString) && maxStringLength < size) { maxStringLength = size; } } System.out.println(maxStringLength); } }