要求用户使用简单的while循环再次运行程序

时间:2014-03-21 04:33:12

标签: java loops while-loop

我正在尝试运行此程序。用户输入值后,程序将输出值。我的问题是该程序应该要求用户输入任何数字再次运行程序或-1退出,但我无法确定将while循环放在代码中的位置。我需要知道在哪里完全放置while循环。它应该在Java代码的开头还是在最后。代码如下。

import java.util.Scanner;
public class Program4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System. in );

        //Declare variables
        double numb;
        double absolute, meanNum = 0, medianNum = 0;
        double num1, num2, num3;
        double m;
        double me;
        double ab;

        System.out.println("Enter a number"); //prompting the user to enter information
        numb = input.nextDouble();

        while (numb != -1) {

            ab = absolute(numb);
            System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

            System.out.println("Enter three numbers"); //prompting the user to enter information
            num1 = input.nextDouble();
            num2 = input.nextDouble();
            num3 = input.nextDouble();

            m = meanNum(num1, num2, num3); // Calling the method Mean Number
            System.out.printf("The Mean Number is: %f\n", m);

            me = medianNum(num1, num2, num3); // Calling the method Median Number
            System.out.printf("The median Number is %f\n", me);
        }
    }
    //// Creating another method for finding Mean Number
    public static double meanNum(double num1, double num2, double num3) {
        double meanNumb = 0;
        meanNumb = (num1 + num2 + num3) / 3; // Calculating the mean number by summing the numbers entered divided by three

        return (meanNumb);
    }
    // Creating another method for finding Median Number
    public static double medianNum(double num1, double num2, double num3) {
        //Using the IF Statement to find the median value of three numbers entered
        double medianNum = 0;
        if (num1 < (num3) && (num1 > num2))
            medianNum = num1;
        if ((num2 < num1) && (num2 > num3))
            medianNum = num2;
        if ((num3 < num2) && (num3 > num1))
            medianNum = num3;
        if ((num1 < num2) && (num1 > num3))
            medianNum = num1;
        if ((num2 < num3) && (num2 > num1))
            medianNum = num2;
        if ((num3 < num1) && (num3 > num2))
            medianNum = num3;

        return (medianNum);
    }
    // Creating a separated method for finding the absolute value of a number
    public static double absolute(double numb) {
        double ab = 0;
        if (numb < 0) {
            ab = numb * -1;
        } else {
            ab = numb;


        }

        return ab;
    }
}

4 个答案:

答案 0 :(得分:2)

do-while循环是您正在寻找的。它会是这样的:

do {
// whatever
} while(numb != 1);

do-while循环确保循环运行至少一次,然后根据您的while语句,它们将继续运行或退出。因此,您可以在do-while循环中放置以下行:

System.out.println("Enter a number"); //prompting the user to enter information
numb = input.nextDouble();

在此之后,只需包含您的while循环已包含的所有内容。每次循环运行时,它都会要求用户输入一个数字,根据输入的数字,您的程序将继续运行或退出。

您可以在此处查找Oracle的教程:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

<强>更新

运行此程序时,您可能会遇到一些异常。我建议你做以下事情以避免任何问题:

do {
     System.out.print("Enter a number (Enter -1 to exit): "); //prompting the user to enter information
     numb = input.nextDouble();


     if (numb != -1) {
         ab = absolute(numb);
         System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

         System.out.println("Enter three numbers"); //prompting the user to enter information
         num1 = input.nextDouble();
         num2 = input.nextDouble();
         num3 = input.nextDouble();

         m = meanNum(num1, num2, num3); // Calling the method Mean Number
         System.out.printf("The Mean Number is: %f\n", m);

         me = medianNum(num1, num2, num3); // Calling the method Median Number
         System.out.printf("The median Number is %f\n", me);
     }
} while (numb != -1);

if-statement确保只有在用户输入的数字不是-1时才会运行其余代码。

答案 1 :(得分:0)

只需在while循环的末尾添加一条消息,您必须要求用户输入另一个输入并再次将该输入读入numb

System.out.println("Enter a number");
numb = input.nextDouble();

答案 2 :(得分:0)

您可以使用DO While循环,它是出口控制的,与入口控制的while循环不同

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System. in );

    //Declare variables
    double numb;
    double absolute, meanNum = 0, medianNum = 0;
    double num1, num2, num3;
    double m;
    double me;
    double ab;



    do {
        System.out.println("Enter a number"); //prompting the user to enter information
        numb = input.nextDouble();
        ab = absolute(numb);
        System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

        System.out.println("Enter three numbers"); //prompting the user to enter information
        num1 = input.nextDouble();
        num2 = input.nextDouble();
        num3 = input.nextDouble();

        m = meanNum(num1, num2, num3); // Calling the method Mean Number
        System.out.printf("The Mean Number is: %f\n", m);

        me = medianNum(num1, num2, num3); // Calling the method Median Number
        System.out.printf("The median Number is %f\n", me);
    } while (numb != -1);
}

答案 3 :(得分:0)

您可以使用do.. while循环!

do {
    System.out.println("Enter a number"); //prompting the user to enter information
    numb = input.nextDouble();

    ab = absolute(numb);
    System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

    System.out.println("Enter three numbers"); //prompting the user to enter information
    num1 = input.nextDouble();
    num2 = input.nextDouble();
    num3 = input.nextDouble();

    m = meanNum(num1, num2, num3); // Calling the method Mean Number
    System.out.printf("The Mean Number is: %f\n", m);

    me = medianNum(num1, num2, num3); // Calling the method Median Number
    System.out.printf("The median Number is %f\n", me);
} while (numb != -1);