2016-03-07 63 views
1

我一直在试图输入正确的代码来查找用户输入中的最大号码和最小号码这是我的代码迄今为止,我能找到平均值,是否有我可以使用的操作员? :找到用户输入的最大号码和最小号码(方法)

package labs; 

import java.util.Scanner; 

public class Lab7method { 

public static double readNumber(Scanner input, String prompt, String errorMessage, boolean toBePositive, boolean askForInteger) { 
boolean hasError = true; // assume to have error 
double number = 0; //initialize number 


while (hasError) { 
     // Prompt the user to input 
     System.out.print(prompt); 

     // Check if it is non-numeric data 
     if (!input.hasNextDouble()) { 
      // If it is, report the error 
      System.out.println(errorMessage); 
      input.next(); // throw the input away by reading it 
     } else if (askForInteger && !input.hasNextInt()) { 
      // report the non-integer error 
      System.out.println(errorMessage); 
      input.next(); // throw the input away by reading it 
     } 
     else { 
      // read the number 
      number = input.nextDouble(); 

      // if the caller wants only positive number, check for negative or zero 
      if (toBePositive && number <= 0) 
       System.out.println(errorMessage); // report the error 
      else 
       // end the error checking loop 
       hasError = false; 
     } 
    } 

    // return the number to the caller 
    return number; 
} 

// the really used method 
public static int readInteger(Scanner input, String prompt, String errorMessage) { 
    // invoke the readNumber() to an integer 
    int anInteger = (int)readNumber(input, prompt, errorMessage, false, true); 

    // return to the caller 
    return anInteger; 
} 


// the really used method 
public static double readDouble(Scanner input, String prompt, String errorMessage) { 
    // invoke the readNumber() to do the actual work 
    double aNumber = readNumber(input, prompt, errorMessage, false, false); 

    // return to the caller 
    return aNumber; 
} 

// the really used read positive double method 
public static double readPositiveDouble(Scanner input, String prompt, String errorMessage) { 
    // invoke the readNumber() to get a positive floating point number 
    double aNumber = readNumber(input, prompt, errorMessage, true, false); 

    // return to the caller 
    return aNumber; 
} 

public static int readPositiveInt(Scanner input, String prompt, String errorMessage) { 
    int anposInteger = (int) readNumber(input, prompt, errorMessage, true, false); 
    return anposInteger; 
} 

public static void main(String[] args) { 
    // Create a Scanner object 
    Scanner input = new Scanner(System.in); 

    //initial account balance// 

    int noofgrades = readPositiveInt(input, "Enter number of grades: ", "Error you did not enter a positive integer:"); 
    double lowestGrade = 0; 
    double highestGrade = 0;  
    double sumOfGrade = 0; 
    double grade = 0; 



    for (int i = 0; i < noofgrades; i++){   

     double Grade = 0; 
    // monthly interest rate// 

    double mograde = readPositiveDouble(input, "Please enter grade " + (i +1) + ": ", "Errorrrrrrrr"); 


    sumOfGrade += mograde; 
    } 

    double averagegrade = sumOfGrade/noofgrades; 

    // Do I use these -> Double.MAX_VALUE; 
    // Do I use these -> Double.MIN_VALUE;  


     System.out.printf("The average of the grades entered are: %4.2f\n" 
          + "The highest grade: %4.2f\n" + "The lowest grade is: %4.2f\n" 
          , averagegrade, highestGrade, lowestGrade);               

回答

0

你只需要添加一对夫妇的if语句转换为for循环main方法,找出最大值和最小值,如:

for (int i = 0; i < noofgrades; i++) { 

    double Grade = 0; 
    // monthly interest rate// 

    double mograde = readPositiveDouble(input, "Please enter grade " + (i + 1) + ": ", "Errorrrrrrrr"); 

    if (i == 0) { 
     lowestGrade = mograde; 
     highestGrade = mograde; 
    } else { 
     if (mograde < lowestGrade) { 
      lowestGrade = mograde; 
     } 
     if (mograde > highestGrade) { 
      highestGrade = mograde; 
     } 
    } 

    sumOfGrade += mograde; 
} 
+0

我把你的建议,但在运行代码后,我只能够打印的最高等级,但我的最低等级为0.00不管我输入了什么号码。 – Coco

+0

我编辑了我的答案。你现在可以检查吗? –

+0

谢谢你了! :) – Coco

0

试试这个例子,打印最小最大右后输入:

Scanner scanner = new Scanner(System.in); 
    int n; 
    int max = Integer.MIN_VALUE; 
    int min = Integer.MAX_VALUE; 

    while((n = scanner.nextInt()) != Integer.MIN_VALUE){ 
     max = n>max?n:max; 
     min = n<min?n:min; 
     System.out.println(String.format("Min: %d. Max %d", min, max)); 
    } 
0

我跑了它,但只有最高的号码才能够打印。我输入的任何输入都是最低的数字。

public static void main(String[] args) { 
    // Create a Scanner object 
    Scanner input = new Scanner(System.in); 

    //initial account balance// 

    int noofgrades = readPositiveInt(input, "Enter number of grades: ", "Error you did not enter a positive integer:"); 
    double lowestGrade = 0; 
    double highestGrade = 0;  
    double sumOfGrade = 0; 
    double grade = 0; 



    for (int i = 0; i < noofgrades; i++){   



     double Grade = 0; 
    // monthly interest rate// 

    double mograde = readPositiveDouble(input, "Please enter grade " + (i +1) + ": ", "Errorrrrrrrr"); 



    if(mograde < lowestGrade){ 
     lowestGrade = mograde; 
    } 


    if(mograde > highestGrade){ 
     highestGrade = mograde; 
    } 

    sumOfGrade += mograde; 
    } 

    double averagegrade = sumOfGrade/noofgrades; 

    // Do I use these -> Double.MAX_VALUE; 
    // Do I use these -> Double.MIN_VALUE;  


     System.out.printf("The average of the grades entered are: %4.2f\n" 
          + "The highest grade: %4.2f\n" + "The lowest grade is: %4.2f\n" 
          , averagegrade, highestGrade, lowestGrade);               






} 

}