文章目錄
- Java基礎入門訓練
- 一、根據年齡數值輸出年齡段
- 二、列印1-100素數
- 三、判斷素數
- 四、輸出閏年
- 五、輸出乘法口訣表
- 六、求兩個正整數的最大公約數
- 七、計算運算式的值
- 八、數字9 出現的次數
- 九、求水仙花數
- 十、撰寫代碼模擬三次密碼輸入的場景
- 十一、求二進制位中1的個數
- 十二、求二進制奇偶序列
- 十三、猜數字游戲
- 完!
Java基礎入門訓練
一、根據年齡數值輸出年齡段
題目內容:
??根據年齡, 來列印出當前年齡的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
Java代碼實作
import java.util.Scanner;
public static void main0(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("請輸入這個人的年齡大小:");
int age = scan.nextInt();
if(age>=0 && age<=18){
System.out.println("少年");
}
else if(age>=19 && age<=28){
System.out.println("青年");
}
else if(age>=29 && age<=55){
System.out.println("中年");
}
else if(age>=56){
System.out.println("老年");
}
}
編譯效果:

??我們輸入一個數值的大小,程式會列印出對應的年齡段.
二、列印1-100素數
題目內容:
??列印1-100之間存在的素數
Java代碼實作
public static void main(String[] args) {
int i=1;
int j=2;
int count=0;
for(i=1;i<=100;i++){
for(j=2;j<i;j++){
if (i%j==0){
break;
}
}
if(i==j){
System.out.println(i);
count++;
}
}
System.out.println(count);
}
編譯效果:

注意點:列印出1-100之間的素數,我們用的是較為簡單的做法,還可以更加方便地求解,比如i++ --> i+=2,偶數不可能是素數,還可以將j的范圍縮小到 i/2 或者 i開平方.
三、判斷素數
題目內容:
??輸入一個數字判斷是否是素數
Java代碼實作:
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.printf("請輸入要判斷的數字:");
int num=scan.nextInt();
int i=0;
for( i=2;i<num;i++){
if(num%i==0){
System.out.println(num+"不是素數");
break;
}
}
if(i==num){
System.out.println(num+"是素數");
}
}
編譯效果:

注意點:同上
四、輸出閏年
題目內容:
??輸出 1000 - 2000 之間所有的閏年
Java代碼實作:
public static void main(String[] args) {
int count = 0;
for(int year=1000;year<=2000;year++){
if((year%4==0) && (year %100!=0) ||(year %400 == 0) ){
System.out.println(year);
count++;
}
}
System.out.println(count);
}
編譯效果:

五、輸出乘法口訣表
題目內容:
??輸出9*9乘法口訣表
Java代碼實作:
public static void main(String[] args) {
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
System.out.printf("%d*%d=%-2d ",j,i,i*j);
}
System.out.println();
}
}
編譯效果:

注意點:格式化輸出,我們可以采用C語言中printf函式的格式.
六、求兩個正整數的最大公約數
題目內容:
??輸入兩個正整數,輸出他們的最大公約數
Java代碼實作:
import java.util.Scanner;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c = 0;
if(a<b){
int tmp = a;
a = b;
b = tmp;
}
while(a%b!=0) {
c = a % b ;
a = b;//
b = c;
}
System.out.println(b);
}
編譯效果:

注意點:我們做題時用到了輾轉相除法,不了解規則的同學可以來到我的往期博客C語言編程筆試題(二)了解.
七、計算運算式的值
題目內容:
??計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,
Java代碼實作:
public static void main(String[] args) {
int flag = 1;
double sum = 0.0;
for(int i=1;i<=100;i++){
sum += (flag)*(1.0/i) ;
flag=-flag;
}
System.out.println(sum);
}
編譯效果:

注意點: sum += ( flag ) * ( 1.0 / i) ;這里一定要明確 是1.0 / i ,不是1 / i.另外sum要定義成double型別.
八、數字9 出現的次數
題目內容:
??撰寫程式數一下 1到 100 的所有整數中出現多少個數字9
// 9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99
public static void main(String[] args) {
int count = 0;
for( int i = 0;i<=100;i++){
if(i%10==9){
count++;
}
if(i/10==9){
count++;
}
}
System.out.println(count);
}
編譯效果:

注意點:99中9出現了兩次,所以我們用兩個if陳述句,分別對含有9的數字中9的個數進行計數.
九、求水仙花數
題目內容:
??求出0~999之間的所有“水仙花數”并輸出,
??水仙花數”是指一個三位數,其各位數字的立方和確好等于該數本身,如:153=1+5+3?,則153是一個“水仙花數.
Java代碼實作:
public static void main(String[] args) {
for(int n=1;n<999;n++){
int tmp=n;
int count = 0;
//1.算出該數字有多少位
while(tmp!=0){
count++;
tmp = tmp/10;
}//得到的count 即為該數字的位數
//2.將該數字的每一位數字得到,算出每一位數字的次方的和
tmp=n;
int sum = 0;
while(tmp!=0){
sum += Math.pow(tmp%10,count);
tmp = tmp/10;
}
//3.比較結果與原數字是否相等
if(sum==n){
System.out.println(sum+"是水仙花數");
}
}
}
編譯效果:

思考步驟:
1.算出該數字有多少位
2.將該數字的每一位數字得到,算出每一位數字的次方的和
3.比較結果與原數字是否相等
十、撰寫代碼模擬三次密碼輸入的場景
題目內容:
??最多能輸入三次密碼,密碼正確,提示“登錄成功”,密碼錯誤可以重新輸入.
??最多輸入三次,三次均錯,則提示退出程式
Java代碼實作:
import java.util.Scanner;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 3;
while(count>0){
System.out.println("請輸入密碼:");
String password = sc.nextLine();
if(password.equals("123456")){
System.out.println("輸入正確 , 登陸成功 !!");
}
else{
count--;
System.out.println("輸入錯誤 ,"+"你還有"+count+"次機會!!");
}
}
}
編譯效果:

注意點:equals() 比較字串的功能.
十一、求二進制位中1的個數
題目內容:
??寫一個函式回傳引數二進制中 1 的個數 比如: 15 0000 1111 4 個 1
Java代碼實作:
import java.util.Scanner;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入數字:");
int num = sc.nextInt();
int count = 0;
for(int i=0;i<32;i++){
if(((num>>i) & 1 )== 1){
count++;
}
}
System.out.println(count);
}
編譯效果:

注意點: 明確位運算子& 的作用,二進制的每一位 &1,都可以得到這一位上的數字
十二、求二進制奇偶序列
題目內容:
??獲取一個數二進制序列中所有的偶數位和奇數位, 分別輸出二進制序列,
Java實作代碼:
import java.util.Scanner;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("請輸入一個數字:");
int num = s.nextInt();
// 取到二進制數列中的偶數位
for(int i=31;i>=0;i-=2){
System.out.print(((num>>i)&1)+" ");
}
System.out.printf("\n");//換行列印
// 取到二進制序列中的奇數位
for(int i=30;i>=0;i-=2){
System.out.print(((num>>i)&1)+" ");
}
}
編譯效果:

注意點:這是練習十二的拓展,我們根據二進制數列的奇偶位進行取位.
十三、猜數字游戲
??我們實作簡單的猜數字游戲,由電腦隨機生成100以內的數字,我們進行猜測,直到猜對為止,程式退出,
Java代碼實作:
import java.util.Random;
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
Random random = new Random();
int randNum = random.nextInt(100);
//System.out.println(randNum);
while(true){
System.out.println("請輸入數字:");
int num=scan.nextInt();
if(num<randNum){
System.out.println("你猜小了");
}
else if(num==randNum){
System.out.println("你猜對了");
break;
}
else if(num>randNum){
System.out.println("你猜大了");
}
}
}
編譯效果:

??好了,我們Java基礎題目的分享就到這里結束了,希望大家多多練習,
??謝謝大家的欣賞與關注!!!
完!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/272463.html
標籤:java
上一篇:多執行緒總結(四)萬字長文,一篇徹底看懂ReentrantLock,AQS的原始碼
下一篇:Java物件創建的程序具體分析
