導航:首頁 > 編程語言 > java從鍵盤輸入幾個數字

java從鍵盤輸入幾個數字

發布時間:2023-11-08 20:10:45

java語言循環從鍵盤輸入10個整數,計算奇數的和

importjava.util.Scanner;

publicclassTestAdd{

publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
intsum=0;
for(inti=1;i<=10;i++){
System.out.println("請輸入第"+i+"個整數:");
inttemp=scan.nextInt();
if(temp%2==1){
sum+=temp;
}
}
System.out.println("所有奇數的和為:"+sum);
}
}

② 編寫一個JAVA小程序,從鍵盤輸入3個數,求這三個數的最大值。

可以使用數組,來存儲輸入的3個數字,然後排序,得到最大值

也可以直接一邊輸出一邊判斷,參考代碼如下

importjava.util.Scanner;

publicclassExam{
publicstaticvoidmain(String[]args){
intlen=3;
System.out.println("請輸入"+len+"個數字");
Scannersc=newScanner(System.in);
intmax=sc.nextInt();//假設第一次輸入的數字是最大,存在這里
for(inti=0;i<len-1;i++){
intx=sc.nextInt();
if(x>max){//如果比max還要大.那麼就替換掉max的值
max=x;
}
}
System.out.println("最大的數字是"+max);
}
}

測試

請輸入3個數字
2
1
6
最大的數字是6

③ java從鍵盤輸入N個數,並輸出其最大值和最小值。

importjava.util.Scanner;
publicclassMain
{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
System.out.print("數字個數:");
intn=sc.nextInt();
intmin=Integer.MAX_VALUE;
intmax=Integer.MIN_VALUE;
int[]b=newint[n];
for(inti=0;i<n;i++)
{System.out.print("第"+(i+1)+"個數字:");
b[i]=sc.nextInt();
}
for(inti=0;i<n;i++)
{
if(min>b[i]){min=b[i];}
if(max<b[i]){max=b[i];}
}
System.out.println("最小值:"+min+"最大值:"+max);
}
}

運行結果:

數字個數:5

第1個數字:12

第2個數字:23

第3個數字:44

第4個數字:-43

第5個數字:5

最小值:-43 最大值:44

④ 用Java寫一個程序,鍵盤輸入5個整數 並按大小的次序輸出

import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入5個整數:");
int[] number=new int[5];
for(int i=0;i<number.length;i++){
number[i]=scanner.nextInt();
}
Arrays.sort(number);
System.out.println("排序後輸出:");
for (int num:number) {
System.out.print(num+"\t");
}
}
}

控制台:
請輸入5個整數:
43
12
87
11
0
排序後輸出:
0 11 12 43 87

閱讀全文

與java從鍵盤輸入幾個數字相關的資料

熱點內容
文件領導簽字在哪裡 瀏覽:239
電腦保存文件是點哪裡 瀏覽:689
word07圖片框 瀏覽:359
跨境付app現在支持哪些國家 瀏覽:647
手機mid文件 瀏覽:929
華碩筆記本升級bios找不到文件 瀏覽:399
編程和電腦哪個更適合孩子 瀏覽:718
xls壓縮文件怎麼設置 瀏覽:423
微信眨眼小女孩 瀏覽:207
cad2010安裝文件 瀏覽:379
編程上什麼學 瀏覽:364
美版s7升級70移動4g 瀏覽:112
金融數據包含哪些 瀏覽:240
尼爾應用程序錯誤 瀏覽:157
淘特app如何助力 瀏覽:472
cad文件有木馬 瀏覽:459
多頁文件怎樣改變字體 瀏覽:145
16燈搖搖棒的程序 瀏覽:705
作業成本相關數據哪裡找 瀏覽:826
買鞋有什麼推薦的app 瀏覽:239

友情鏈接