Java基础题2从入门到放弃的第五天
Java基礎題2從入門到放棄的第5天
小博主也是初學者,肯定有地方可以用更簡單的方法,希望可以指點出來。謝謝!
1分支語句
1、 讓用戶輸入一個100以內的整數x,再輸入一個100到200之間的整數y,然后程序隨機一個x到y之間的整數并打印。
public class Test1 {public static void main(String[] args) {Scanner sc =new Scanner(System.in);System.out.print("輸入一個100以內的整數x: ");int x=sc.nextInt();System.out.print("輸入一個100到200之間的整數y: ");int y=sc.nextInt();int a=(int) (Math.random()*(y-x))+x;System.out.println("程序隨機一個x到y之間的整數: "+a);} }2.隨機兩個0到100之間的整數,然后使用較大的一個數減去較小的一個數,再判斷這個差的奇偶性。
public class Test2 {public static void main(String[] args) {int a = (int) (Math.random() * 100);int b = (int) (Math.random() * 100);int sub = Math.abs(a - b);if (sub != 0 && sub % 2 == 0) {System.out.println(sub + "為偶數");} else if (sub != 0 && sub % 2 != 0) {System.out.println(sub + "為奇數");} else {System.out.println(sub + "為非奇非偶");}} }? 6.編寫代碼實現如下內容:實現考試成績分等級。[90-100] A等。[80-90) B等。[70-80) C等。 [60-70) D等。[0-60) E等。請根據給定成績,輸出對應的等級。說明:"[“表示包含,”)"表示不包含
public class Test6 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.print("輸入成績:");int grade=sc.nextInt();//for循環if (grade >= 90 && grade <= 100) {System.out.println("A等");} else if (grade >= 80) {System.out.println("B等");} else if (grade >= 70) {System.out.println("C等");} else if (grade >= 60) {System.out.println("D等");} else if (grade >= 0) {System.out.println("E等");} else {System.out.println("沒有記錄");}} }7.分析以下需求,并用代碼實現:
? (1)根據工齡(整數)給員工漲工資(整數),工齡和基本工資通過鍵盤錄入
? (2)漲工資的條件如下:
? [10-15) +5000
? [5-10) +2500
? [3~5) +1000
? [1~3) +500
? [0~1) +200
(3)如果用戶輸入的工齡為10,基本工資為3000,程序運行后打印格式"您目前工作了10年,基本工資為 3000元, 應漲工資 5000元,漲后工資 8000元"
public class Test7 {public static void main(String[] args) {Scanner sc =new Scanner(System.in);System.out.print("請輸入工齡:");int age=sc.nextInt();System.out.print("請輸入底薪: ");int money1=sc.nextInt();if (age>=10&&age<15){System.out.println("您目前工作了"+age+"年,基本工資為"+money1+"元, 應漲工資 5000元,漲后工資"+(money1+5000)+"元");}else if (age>=5){System.out.println("您目前工作了"+age+"年,基本工資為"+money1+"元, 應漲工資 2500元,漲后工資"+(money1+2500)+"元");}else if (age>=3){System.out.println("您目前工作了"+age+"年,基本工資為"+money1+"元, 應漲工資 1000元,漲后工資"+(money1+1000)+"元");}else if (age>=1){System.out.println("您目前工作了"+age+"年,基本工資為"+money1+"元, 應漲工資 500元,漲后工資"+(money1+500)+"元");}else if (age>=0){System.out.println("您目前工作了"+age+"年,基本工資為"+money1+"元, 應漲工資 200元,漲后工資"+(money1+200)+"元");}else{System.out.println("老板給你");}} }2.循環語句
1.編寫程序,生成5個1至10之間的隨機整數,并打印結果到控制臺。
public class Test9 {public static void main(String[] args) {for (int i = 0; i <5 ; i++) {System.out.print((int)(Math.random()*9+1)+"\t");}} }2.判斷101-200之間有多少個素數,并輸出所有素數。
public class Test10 {public static void main(String[] args) {int a = 0;for (int i = 101; i <= 200; i++) {for (int j = 2; j <= i; j++) {if (i % j == 0) {break;} else if (j == i - 1) {a += 1;System.out.print(i + "為素數\t");}}}System.out.println();System.out.print("總共有"+a+"個素數");} }3.編寫程序,打印1到100之內的整數,但數字中包含7或者7的倍數要跳過。(逢7過)
public class Test10 {public static void main(String[] args) {for(int i=1;i<=100;i++){if(i%7==0||i/10==7||i%10==7){System.out.print("");}else{System.out.print(i+"\t");}}}4.求s=a+aa+aaa+aaaa+aa…a的值,其中a是一個數字。
public class Test4 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入一個1~9的數:");int a = sc.nextInt();//a的值System.out.print("請輸入一個數:");int b = sc.nextInt();//a的位數int r = 0;//r是中間結果int s = 0;//結果for (int j = 0; j < b; j++) {for (int i = 0; i < b - j; i++) {int num = (int) Math.pow(10, i);r += a * num;}System.out.print(r + " ");s += r;r = 0;}System.out.println();System.out.println("相加結果:" + s);} }5.求1+2+3+…+1000的和,把和輸出,計算每步結果中有多少個8結尾的數。
public class Test12 {public static void main(String[] args) {int num=0;int add=0;for (int i = 1; i <=1000 ; i++) {add+=i;if(add%10==8){num+=1;}}System.out.println("總和:"+add+" 位數為8的有"+num+"個");} }6.輸出下圖案
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1… 7
1 …100
public class Test14 {public static void main(String[] args) {for (int i = 1; i <= 100; i++) {for (int j = 1; j < i + 1; j++) {System.out.print(j + "\t");}System.out.println();}} }7.求水仙花數(一個三位數,個十百位每位數的立方和等于該數本身)
public class Test2 {public static void main(String[] args) {for (int i = 100; i <= 999; i++) {int a = i / 100; //百位int b = i % 100 / 10; //十位int c = i % 10; //個位if (i == a * a * a + b * b * b + c * c * c) {System.out.print(+i + "\t");}}} }8.使用 *號在分別在控制臺輸出等腰三角形、菱形
public class Test18 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.print("隨機輸入一個數:");int line = sc.nextInt();//等腰三角形System.out.println("打印一個等腰三角形:");for (int i = 1; i <= line; i++) {for (int j = line -i; j >= 0; j--) {System.out.print(" ");}for (int k = 0; k < 2 * i - 1; k++) {System.out.print("*");}System.out.println();}//菱形System.out.println("打印一個菱形:");for (int i = 1; i <= line/2+1; i++) {for (int j = line/2-i; j >=0; j--) {System.out.print(" ");}for (int k = 0; k < 2 * i - 1; k++) {System.out.print("*");}System.out.println();}for (int i =1; i <=line/2; i++) {for (int j = 1; j <=i; j++) {System.out.print(" ");}for (int k=line-2*i;k>0;k--){System.out.print("*");}System.out.println();}} }總結
以上是生活随笔為你收集整理的Java基础题2从入门到放弃的第五天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: icloud 照片导出_我的照片流和iC
- 下一篇: WEB网络渗透的基础知识