導航:首頁 > 編程語言 > 輸出最大值java

輸出最大值java

發布時間:2023-05-19 22:14:48

⑴ 編寫java程序,從十個數中找出最大值

import java.util.Scanner;

public class Test {

public static void main(String args[]){

int num[]=new int[10];
int max;
Scanner scan=new Scanner(System.in);
System.out.print("請輸入10個數字內:");
for(int i=0; i<10; i++){
num[i]=scan.nextInt();
}
max=num[0];
for(int i=1; i<10; i++){
if(max<num[i]) max=num[i];
}
System.out.println("最大值容是"+max);
}

}

⑵ 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 判斷兩個數大小 並輸出最大值

public class Test {
public static void main(String[] args) {
System.out.println(Math.max(3, 4));
}
}

因為不知道你比的是抄double型還int型,或者其他類型,所以把相關方法全貼這了。

static double max(double a, double b)
返回兩個 double 值中較大的一個。
static float max(float a, float b)
返回兩個 float 值中較大的一個。
static int max(int a, int b)
返回兩個 int 值中較大的一個。
static long max(long a, long b)
返回兩個 long 值中較大的一個。

⑷ 求java源程序代碼:輸入兩個數,輸出最大值

一下代碼僅供參考

packagecom.kidd.test.;

importjava.util.NoSuchElementException;
importjava.util.Scanner;

publicclassTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
inta=0;
intb=0;

booleannext=false;

while(!next){
System.out.print("請輸入兩個整數(用空格分隔):");
try{
a=sc.nextInt();
b=sc.nextInt();
next=true;
}catch(NoSuchElementExceptione){
System.out.println("輸入有誤,請重新輸入.");
sc.nextLine();
continue;
}
}
System.out.printf("最大值為:%d ",a>b?a:b);
}
}

⑸ 用JAVA 輸入三個數字,輸出最大值和最小值

importjava.util.Scanner;

publicclassDemon
{

publicstaticvoidmain(String[]args)
{
int[]input=newint[3]; //定義輸入的數組(3個數)
Scannerin=newScanner(System.in); //定義輸入方法
System.out.println("輸入第3個整數,空格隔開:");
for(inti=1;i<4;i++) //輸入3個數
{

input[i-1]=in.nextInt();
}
System.out.println("最大和最小數數是:"+max(input[2],max(input[0],input[1]))+""+min(input[2],min(input[0],input[1])));

}
privatestaticintmax(intnumber1,intnumber2) //求最大函數
{
returnnumber1>=number2?number1:number2;

}

privatestaticintmin(intnumber1,intnumber2) //求最小函數
{
returnnumber1<=number2?number1:number2;

}

}

閱讀全文

與輸出最大值java相關的資料

熱點內容
產品在網站優化多少錢 瀏覽:992
亂碼文件夾 瀏覽:480
mc編程後怎麼模擬加工 瀏覽:153
如何恢復刷機後的數據 瀏覽:243
重裝系統win81教程 瀏覽:317
nero10安裝教程 瀏覽:182
handJoy游戲大廳安卓版 瀏覽:663
wow的配置文件怎麼重置 瀏覽:921
css代碼在線編輯 瀏覽:383
哪個狼人殺app可以觀戰 瀏覽:797
你懂的免費qq空間 瀏覽:858
電影曲面是在哪裡拍的app 瀏覽:137
ipadwps怎麼改文件名 瀏覽:162
怎麼將結果顯示在jsp 瀏覽:819
word文檔解析度 瀏覽:108
如何在網站主頁中插入圖像 瀏覽:258
特斯拉數據需要多少伺服器 瀏覽:828
手機百度雲無法看種子文件 瀏覽:690
都有哪些街拍網站 瀏覽:482
賣家鄉特產要什麼網站賣呢 瀏覽:102

友情鏈接