Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在do while循环中查找最大值和小写/大写_Java - Fatal编程技术网

Java 在do while循环中查找最大值和小写/大写

Java 在do while循环中查找最大值和小写/大写,java,Java,我必须编写一个程序,要求用户输入一个整数值。在每个值之后,如果用户想要继续程序,则必须用“y”或“n”进行响应,并且用户输入的每个数字都表示为奇数或偶数,并找到用户输入的数字的平均值。我已经完成了这一切,但我对两件事感到困惑: 我该如何使用户可以同时输入大写和小写字母y,并且程序仍将运行?到目前为止,它只适用于小写的y 此外,你会如何编程,以便在用户输入完所有数字后,它会给你用户输入的最高数字? 例如:用户输入数字10、12、13,程序声明“输入的最大值为13。” 以下是我目前的代码: impo

我必须编写一个程序,要求用户输入一个整数值。在每个值之后,如果用户想要继续程序,则必须用“y”或“n”进行响应,并且用户输入的每个数字都表示为奇数或偶数,并找到用户输入的数字的平均值。我已经完成了这一切,但我对两件事感到困惑:

  • 我该如何使用户可以同时输入大写和小写字母y,并且程序仍将运行?到目前为止,它只适用于小写的y
  • 此外,你会如何编程,以便在用户输入完所有数字后,它会给你用户输入的最高数字? 例如:用户输入数字10、12、13,程序声明“输入的最大值为13。”
  • 以下是我目前的代码:

    import java.util.Scanner;
    class ProgramTest {
        public static void main(String[] args) {
            String answer = "";
            double sum = 0;
            int count = 0;
            do {
    
                int num;
                Scanner scan = new Scanner(System.in);
                System.out.print("Enter any number : ");
                num = scan.nextInt();
    
                if ((num % 2) == 0) System.out.println(num + " is an even number.");
                else System.out.println(num + " is an odd number");
                sum += num;
                System.out.println(("Would you like to enter another value(y/n)?:"));
                answer = scan.next();
                count++;
    
            } while (answer.equals("y"));
    
            System.out.println("Average: " + (sum/count));
        }
    }
    

    查看Java文档中的
    String.equalsIgnoreCase(String-anotherString)
    ,了解小写和大写的
    y

    对于最高/最低数字,创建2个变量,如果它们为空或低于/高于当前数字,则重新分配它们。在课程结束时,您将获得适当的价值


    这太简单了,无法向您提供代码,因此,到目前为止,上面的解释已经足够了。

    要获得不区分大小写的比较,请尝试

    import java.util.Scanner;
    class ProgramTest {
        public static void main(String[] args) {
        String answer = "";
        double sum = 0;
        int count = 0;
        **int max = 0;**
        do {
    
            int num;
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter any number : ");
    
            num = scan.nextInt();
    
            if ((num % 2) == 0) System.out.println(num + " is an even number.");
            else System.out.println(num + " is an odd number");
            sum += num;
            System.out.println(("Would you like to enter another value(y/n)?:"));
    
            **max = (num > max) ? num : max ;** 
    
            answer = scan.next();
            count++;
    
        } while (answer.equals("y"));
    
          System.out.println("Average: " + (sum/count));
          System.out.println("Highest : " + max);   
      }
    
    while (answer.equalsIgnoreCase ("y"));
    
    对于最高和最低,在
    do
    循环之前创建两个新变量

    int lowest = Integer.MAX_VALUE;
    int highest = Integer.MIN_VALUE;
    
    在你的循环中做什么

    lowest = Math.min (lowest, num);
    highest = Math.min (higest, num);
    

    谢谢你的小写和大写,但我不明白你所说的变量是什么意思。我将把变量放在哪里?如果一个数大于另一个数,我如何将它们重新分配到另一个数?请原谅,如果这对你来说真的很基本,这是我做Java和计算机编程的第一年,一般来说,我仍在习惯它。非常感谢您的帮助。