控制语句 for while if && 其他
package Tutorial_1_Java_basic;
import java. util. *;
public class Tutorial_1 {
public void dataType ( ) {
int hello_world = 1 ;
double num2 = 5.0 ;
boolean bo = true ;
char ch = 'a' ;
String str = "hello, world" ;
int num3 = ( int ) num2;
System. out. print ( num3 + num2) ;
int a = 5 ; int b = 4 ; int c = 7 ;
double sum = a + b - c;
float other = a * b - c/ 4 ;
int zhishu = ( int ) Math. pow ( 2 , 3 ) ;
int quyu = 56 % 5 ;
System. out. print ( zhishu + " " + sum + " " + other) ;
}
public void Scanner ( ) {
Scanner sca = new Scanner ( System. in) ;
String scanned = sca. next ( ) ;
int scanned1 = sca. nextInt ( ) ;
int trans = Integer. parseInt ( scanned) ;
System. out. println ( scanned + " " + scanned1 + " " + trans) ;
}
public void logicOperate ( ) {
int a = 6 ; int b = 7 ; int c = 10 ;
boolean compare;
compare = a <= b;
String x = "Hello" ; String y = "hello" ;
compare = x== y && a< b || a< c ;
System. out. print ( compare) ;
}
public void ifDecision ( ) {
Scanner sc = new Scanner ( System. in) ;
String s = sc. nextLine ( ) ;
if ( s. equals ( "Good" ) ) {
System. out. println ( "You input GOOD not good!" ) ; }
else if ( s. equals ( "hi" ) ) {
System. out. println ( "hello" ) ; }
else { System. out. println ( s + " world! " ) ; }
}
public void NestedSentence ( ) {
System. out. println ( "输入您的年龄: " ) ;
Scanner sc = new Scanner ( System. in) ;
String s = sc. nextLine ( ) ;
int age = Integer. parseInt ( s) ;
if ( age >= 18 ) {
System. out. println ( "成年啦,你喜欢什么食物" ) ;
String food = sc. nextLine ( ) ;
if ( food. equals ( "面条" ) ) {
System. out. println ( "我也喜欢面条" ) ;
}
else { System. out. println ( "不是我的菜" ) ; }
}
else if ( age >= 14 ) System. out. println ( "青少年" ) ;
else System. out. println ( "不知道" ) ;
}
public void CreateArray ( ) {
String[ ] strArray = new String [ 10 ] ;
int [ ] newArray = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ;
strArray = new String [ ] { "Hello" , "Good" , "Byebye" , "Nice" , "pppp" } ;
System. out. println ( strArray[ 2 ] + " " + newArray[ 0 ] ) ;
}
public void controlLoop ( ) {
int x = 0 , y= 0 ;
for ( int i= 0 ; i< 5 ; i++ ) {
y += 1 ;
System. out. println ( i + " " + x + " " + y) ;
x += 1 ;
if ( x> 3 ) break ;
}
System. out. println ( "数组" ) ;
int [ ] arr = new int [ 6 ] ;
for ( int i= 0 ; i< 5 ; i++ ) {
arr[ i] = i+ 1 ;
System. out. println ( arr[ i] ) ;
}
System. out. println ( Arrays. stream ( arr) ) ;
for ( int element: arr) {
element += 1 ;
System. out. println ( element) ;
}
String[ ] newstr = new String [ 5 ] ;
Scanner sc = new Scanner ( System. in) ;
for ( int i= 0 ; i< newstr. length; i++ ) {
System. out. print ( "请输入,退出请输入q:" ) ;
newstr[ i] = sc. nextLine ( ) ;
if ( newstr[ i] . equals ( "q" ) )
break ;
}
for ( String i: newstr)
System. out. println ( i) ;
int i= 0 ;
while ( i< 10 ) {
i += 1 ;
if ( i== 5 )
System. out. print ( "i == 5" ) ;
continue ;
}
}
public static void main ( String[ ] args) {
Tutorial_1 tu_1 = new Tutorial_1 ( ) ;
tu_1. controlLoop ( ) ;
}
}
集合、列表
普通哈希集合,树集合,链表哈希集合
Map, key-value 键-值对的结构,类似python中的字典
public class Set_List_Hashmap_2 {
public void Set_ ( ) {
Set< Integer> t = new HashSet < Integer> ( ) ;
t. add ( 100 ) ; t. add ( 12 ) ; t. add ( - 2 ) ;
t. add ( 56 ) ; t. add ( 5 ) ; t. add ( 30 ) ; t. add ( - 9 ) ;
boolean co = t. contains ( 3 ) ;
t. remove ( - 9 ) ;
System. out. println ( t + " 普通集合 " + co + " 集合大小 " + t. size ( ) ) ;
Set< Integer> tree_set = new TreeSet < > ( t) ;
System. out. println ( tree_set + " 树集合是有序的" ) ;
Set< Integer> linkSet = new LinkedHashSet < > ( t) ;
System. out. println ( "链表集合: " + linkSet) ;
}
public void List_ ( ) {
Set< Integer> t = new HashSet < Integer> ( ) ;
t. add ( 100 ) ; t. add ( 12 ) ; t. add ( - 2 ) ;
t. add ( 56 ) ; t. add ( 5 ) ; t. add ( 30 ) ; t. add ( - 9 ) ;
boolean co = t. contains ( 3 ) ;
ArrayList< Integer> list_t = new ArrayList < > ( t) ;
list_t. add ( 6 ) ; list_t. get ( 6 ) ;
list_t. set ( 2 , 60 ) ;
System. out. println ( list_t + " 数组列表 " + list_t. subList ( 1 , 4 ) ) ;
LinkedList< Integer> linkList = new LinkedList < Integer> ( list_t) ;
System. out. println ( " 链表列表 " + linkList) ;
}
public void Map_ ( ) {
Map m = new HashMap ( ) ;
m. put ( "Tom" , 6 ) ; m. put ( 11 , 1111 ) ;
m. put ( "Peter" , 23 ) ; m. put ( "Sasa" , "zzc" ) ;
System. out. println ( "哈希表元素:" + m + " 键:" + m. get ( "Tom" ) ) ;
Map tree_m = new TreeMap ( ) ;
tree_m. put ( "peter" , 5 ) ; tree_m. put ( "Petter" , 5 ) ;
tree_m. put ( "Ali" , 11 ) ; tree_m. put ( "aaa" , - 11 ) ;
System. out. println ( "树表 " + tree_m) ;
Map linked_m = new LinkedHashMap ( m) ;
Map linked_n = new LinkedHashMap ( tree_m) ;
System. out. println ( "树变换后的: " + linked_n +
" 键:" + linked_n. keySet ( ) ) ;
System. out. println ( "链表键值对: " + linked_m +
" 值:" + linked_m. values ( ) ) ;
}
public void Map_example ( ) {
Map m = new HashMap ( ) ;
String str = "Hello, world Maybe I do not know who are you " ;
for ( char x: str. toCharArray ( ) ) {
if ( m. containsKey ( x) ) {
int old = ( int ) m. get ( x) ;
m. put ( x, old+ 1 ) ;
}
else {
m. put ( x, 1 ) ;
}
}
m. remove ( ' ' ) ;
System. out. println ( m) ;
int [ ] x = { - 123 , 1 , 43 , 562 , 123 , 10 , 11 , 35 , 687 , - 11 , 54 } ;
Arrays. sort ( x, 1 , 7 ) ;
for ( int i: x) {
System. out. print ( i + " , " ) ;
}
}
public static void main ( String[ ] args) {
Set_List_Hashmap_2 ss = new Set_List_Hashmap_2 ( ) ;
ss. Map_example ( ) ;
}