例題:編寫程序,生成5個1至10之間的隨機整數,並打印結果到控制台
import java.util.Random;class demo09 { public static void main(String[] args) { //編寫程序,生成5個1至10之間的隨機整數,並打印結果到控制台 for(int i=0;i<5;i++){ Random r =new Random(); int s = r.nextInt(10)+1; System.out.println(s); } } }
顯示結果:
打印1到100之內的整數,但數字中包含7的要跳過
java代碼:
for(int i=0;i<=100;i++){ //十位數 if(i/10 == 7){ continue; }else if(i%10==7){ continue;//個位數 } System.out.println(i); }
我國最高山峰是珠穆朗瑪峰,8848米。現在我有一張足夠大的紙,它的厚度是0.01米。
請問,我折疊多少次,可以折成珠穆朗瑪峰的高度。
Java代碼:
double a=0.01;//對折高度 int i=0;//對折次數 for( i=1; a<8848;i++){ a=a*2;//對折后高度 if(a>8848){ System.out.println("對折"+i+"次"); } }
結果顯示:
需求: 鍵盤錄入x的值,計算出y的並輸出。 x>=3 y = 2 * x + 1; -1<x<3 y = 2 * x; x<=-1 y = 2 * x - 1;
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y =0; if(x>=3){ y = 2*x + 1; }else if(-1<x & x<3){ y = 2*x; }else{ y =2*x-1; } System.out.println(y);
鍵盤錄入三個整數,並將三個數據中的最大值打印在控制台。(Math.max)
Java代碼:
Scanner sc = new Scanner(System.in);//創建錄入對象 int x=0; int[] arr = new int[3];//定義長度為三的數組 //遍歷,把錄入的三個數存進數組 for(int i=0;i<arr.length;i++){ x = sc.nextInt(); arr[i]=x; } //打印輸出 // 方法一 System.out.println("最大值:"+Math.max(Math.max(arr[0],arr[1]),arr[2])); //方法二 if(arr[0] >=arr[1]) { if(arr[0] <= arr[2]){ System.out.println("最大值:"+arr[2]); }else if(arr[0] > arr[2]){ System.out.println("最大值:"+arr[0]); } } else if(arr[0] < arr[1]) { if(arr[1] >= arr[2]) { System.out.println("最大值:"+arr[1]); }else if(arr[1] < arr[2]){ System.out.println("最大值:"+arr[2]); } }
結果例子:
分析以下需求,並用代碼實現:
(1)根據工齡(整數)給員工漲工資(整數),工齡和基本工資通過鍵盤錄入
(2)漲工資的條件如下:
[10-15) +5000
[5-10) +2500
[3~5) +1000
[1~3) +500
[0~1) +200
(3)如果用戶輸入的工齡為10,基本工資為3000,程序運行后打印格式"您目前工作了10年,基本工資為 3000元, 應漲工資 5000元,漲后工資 8000元"
Java代碼:
import java.util.Scanner; class demo01 { public static void main(String[] args) { int gl;//工齡 do { Scanner sc = new Scanner(System.in); System.out.println("請輸入工齡:"); gl = sc.nextInt(); } while (aaa(gl));//如果工齡輸入有誤,循環輸入,正確則結束循環 } //循環輸入方法 public static boolean aaa(int gla) //定義形參gla去接收實參gl { int jza=0; Scanner sc = new Scanner(System.in); boolean fail = false;//先給返回值賦值false //通過參數傳值,輸入工齡判斷 switch(gla){ case 0: System.out.println("請輸入基本工資:"); jza = sc.nextInt(); System.out.println("您目前工作了"+gla+"年,基本工資為 :"+jza+"元, 應漲工資 "+200+"元,漲后工資:"+(jza+200)+" 元"); break; case 1: case 2: System.out.println("請輸入基本工資:"); jza = sc.nextInt(); System.out.println("您目前工作了"+gla+"年,基本工資為 :"+jza+"元, 應漲工資 "+500+"元,漲后工資:"+(jza+500)+" 元"); break; case 3: case 4: System.out.println("請輸入基本工資:"); jza = sc.nextInt(); System.out.println("您目前工作了"+gla+"年,基本工資為 :"+jza+"元, 應漲工資 "+1000+"元,漲后工資:"+(jza+1000)+" 元"); break ; case 5: case 6: case 7: case 8: case 9: System.out.println("請輸入基本工資:"); jza = sc.nextInt(); System.out.println("您目前工作了"+gla+"年,基本工資為 :"+jza+"元, 應漲工資 "+2500+"元,漲后工資:"+(jza+2500)+" 元"); break; case 10: case 11: case 12: case 13: case 14: System.out.println("請輸入基本工資:"); jza = sc.nextInt(); System.out.println("您目前工作了"+gla+"年,基本工資為 :"+jza+"元, 應漲工資 "+5000+"元,漲后工資:"+(jza+5000)+" 元"); break; default: fail = true;//輸入錯誤,則賦值,結束循環 System.out.println("請重新輸入"); } return fail;//返回布爾值 } }
結果演示: