我一直试图输入正确的代码,以便从用户输入中找到最大数字和最小数字。到目前为止我的代码,我能够找到平均数,是否存在我可以使用运营商吗? :
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 :(得分: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;
}
语句,以找出最小值和最大值,例如:
[::-1]
答案 1 :(得分: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));
}
答案 2 :(得分:0)
我跑了但是只有最高的数字才能打印出来。我输入的任何输入都是打印的最低数字0.00。
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);
}
}