獲取一個數二進制序列中所有的偶數位和奇數位, 分別輸出二進制序列
import java. util. Scanner;
public class TestDemo {
public static void main ( String[ ] args) {
Scanner scan = new Scanner ( System. in) ;
int n = scan. nextInt ( ) ;
for ( int i = 31 ; i >= 1 ; i -= 2 ) {
System. out. print ( ( ( n >>> i) & 1 ) + " " ) ;
}
System. out. println ( ) ;
for ( int i = 30 ; i >= 0 ; i -= 2 ) {
System. out. print ( ( ( n >>> i) & 1 ) + " " ) ;
}
}
}
編寫代碼模擬三次密碼輸入的場景。 最多能輸入三次密碼,密碼正確,提示“登錄成功”,密碼錯誤, 可以重新輸 入,最多輸入三次。三次均錯,則提示退出程序
import java. util. Scanner;
public class InputPassword {
public static void main ( String[ ] args) {
Scanner scan = new Scanner ( System. in) ;
int count = 3 ;
System. out. println ( "請輸入正確的密碼,您有3次機會!" ) ;
while ( count != 0 ) {
System. out. println ( "請輸入正確的密碼:" ) ;
String password = scan. next ( ) ;
if ( password. equals ( "6928360" ) ) {
System. out. println ( "登錄成功!" ) ;
break ;
} else {
count-- ; if ( count== 0 ) {
System. out. println ( "你已經輸錯3次密碼,程序退出 " ) ;
} else {
System. out. println ( "登錄失敗,你還有 " + count + "次機會! " ) ;
}
}
}
}
}
輸出n*n的乘法口訣表,n由用戶輸入。
import java. util. Scanner;
public class TestDemo {
public static void main ( String[ ] args) {
Scanner scan = new Scanner ( System. in) ;
int n = scan. nextInt ( ) ;
System. out. println ( n+ "*" + n+ "乘法表" ) ;
System. out. println ( ) ;
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = 1 ; j <= i ; j++ ) {
System. out. print ( i+ "*" + j+ "=" + i* j + " " ) ;
}
System. out. println ( ) ;
}
}
}
遞歸輸出一個整數n的每一位
import java. util. Scanner;
public class TestDemo {
public static void main ( String[ ] args) {
Scanner scanner = new Scanner ( System. in) ;
int n = scanner. nextInt ( ) ;
OneBit ( n) ;
}
public static void OneBit ( int n) {
if ( n > 9 ) {
OneBit ( n / 10 ) ;
}
System. out. print ( n % 10 + " " ) ;
}
}