课堂测试
思路:
因為給的程序中出現了越界的情況,我們就涉及一個程序使得最大值為最后一個數來進行比較這樣就不會越界,②我們對輸入的值進行判斷
代碼
package pkg; import java.util.*; public class Test {public static int Largest(int list[],int length){int i,max=list[length-1];for(i=0;i<=(length-1);i++){if(list[i]>max){max=list[i];}}return max;} public static void main(String[] args) {// TODO Auto-generated method stubint number,max,length;try{Scanner in=new Scanner(System.in);System.out.print("請輸入整數的個數length=:");length=in.nextInt(); //個數的長度System.out.println("請輸入length個整數");int sz[]=new int[length]; //定義長度for(int i=0;i<sz.length;i++){sz[i]=in.nextInt(); //數組的輸入 }System.out.println("最大值為:");Test s=new Test();System.out.println(s.Largest(sz, length)); //輸出最大值 }catch(InputMismatchException e){//接住錯誤System.out.println("輸入格式不正確!");}} }1、正序
2、有字符
3、亂序
4、倒序
5、混合
6、混合正序
轉載于:https://www.cnblogs.com/syhn/p/5302838.html
總結
- 上一篇: scala的传名参数
- 下一篇: 8-4:C++继承之子类的默认构造函数如