java學習階段三:運算子和結構學習

來源:互聯網
上載者:User

標籤:

import java.util.Scanner;
/*
* JAVA中運算子的學習:
* 算術運算子:+、-、*、/ 和 %,兩個整數相除,結果還是整數。
*賦值運算子:=、+=、-=、*=、/=、%=、&=、|=、~=、^=、<<=、>>= 、 >>>=、++ 和 --。
*比較子:==、!=、<、<=、> 和 >=。
*邏輯運算子:&&、|| 和 !。
* 位元運算符:&、|、~、^、<<、>> 和 >>>。
*
*/
public class test {

public static void main(String[] args) {
/*
* 函數應用一:將一個整數以二進位輸出(可調二進位位元)
*/
int shuzi;
Scanner s=new Scanner(System.in) ;//擷取控制台輸入
System.out.println("請輸入一個整數");
//尋找並從該控制台返回下一個數字
shuzi=Integer.parseInt(s.next()); //字串轉換為整型

for(int i=7;i>=0;i--){ //i=7,表示8位2進位 i=31 32位二進位
if((shuzi&(1<<i))!=0){
System.out.println("1");
}else{
System.out.println("0");
}

}
s.close();
/*
* 函數應用二:判斷一個數位位元
*/
int q=1234578;
String str=q+""; //先將整型轉換為字串
System.out.println("數字"+str+"是:"+str.length()+"位元");

/*
* 函數應用三:將一個數字倒序輸出
*/
int olddata=12345678;
int count=(olddata+"").length();
int newdata=0;
while(olddata!=0){
for(int i=0;i<count;i++){
newdata=newdata*10+olddata%10;
olddata=olddata/10;
}
System.out.println("newdata="+newdata);


}
/*
* 函數應用四:判斷一個數是否為素數
*/
int num=3;
boolean flag=true; //預設為素數
for(int i=2;i<=num/2;i++){
if(num%i==0){

flag=false; //如果為偶數,跳出迴圈
break;

}
}
if(flag){
System.out.println(num+" is a 素數!");
}else{
System.out.println(num+" is not a 素數!");
}

}}

java學習階段三:運算子和結構學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.