導航:首頁 > 編程語言 > 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從鍵盤輸入幾個數字相關的資料

熱點內容
創造資料庫的兩種方式是什麼 瀏覽:958
linux使用ss代理伺服器 瀏覽:931
視易魔方機頂盒支持哪些文件 瀏覽:440
壓縮文件付費解壓 瀏覽:352
業務存期代碼 瀏覽:770
把文件下到d盤找不到了 瀏覽:171
蘋果手機有什麼拍攝app軟體推薦 瀏覽:673
網路次爪爪是什麼意思 瀏覽:269
小學教的編程學的什麼 瀏覽:178
cdrX9win10補丁 瀏覽:468
電視安裝提示文件系統未掛載 瀏覽:723
excel讀取文件中的字元串 瀏覽:397
手機文庫下載的文件 瀏覽:700
怎麼查網路的運營商的 瀏覽:127
win101607升級1703 瀏覽:225
人臉識別系統java 瀏覽:391
word公式上標 瀏覽:342
ai文字漸變工具怎麼用 瀏覽:832
mac描述文件急救 瀏覽:92
設備網站模板 瀏覽:172

友情鏈接