检查用户是否立即输入负数?

时间:2015-02-20 16:45:32

标签: java negative-number

我正在尝试创建一个用户输入数字的程序,程序会计算最大值和最小值,并一直询问它是否会遇到负数。

详细说明: -

但是,如果用户在启动时输入负数,则应打印“最大值和最小值未定义!”并结束。

但是如果输入一个正数,程序会输出最大值和最小值,仍会继续询问更多数字,直到遇到负数,看到负数仍会打印最大值和最小值然后结束。

有办法做到这一点吗?

我试过的内容如下: -

import java.util.Scanner;

public class NegativeNum {
    public static void main(String []args) {

        Scanner keys = new Scanner(System.in);
        System.out.println("Enter a number: ");

        double num = keys.nextInt();
        double Max = num+0.5;
        double Min = num-0.5;

        if(num<0) {
            System.out.println("Max and Min undefined");
        }

        while(num>0) {
            System.out.println("Max = " + Max);
            System.out.println("Min = " + Min);
            System.out.println("\nEnter another: ");
            num = keys.nextInt();

        }

        {
            num = num*-1;
            System.out.println("Max = " + Max);
            System.out.println("Min = " + Min);
            System.out.println("Number is Negative! System Shutdown!");
            System.exit(1);

        }

    }
}

4 个答案:

答案 0 :(得分:0)

import java.util.Scanner;


public class NegativeNum {
    public static void main(String []args) {

        Scanner keys = new Scanner(System.in);
        System.out.println("Enter a number: ");

        double num = keys.nextInt();
        double Max = num+0.5;
        double Min = num-0.5;

        if(num<0) {
            System.out.println("Max and Min undefined");
            System.exit(1);
        }

        while(true) {     
            double temp_num = num;         

            num = Math.abs(num);
            Max = num+0.5;
            Min = num-0.5;

            System.out.println("Max = " + Max);
            System.out.println("Min = " + Min);

            if ( temp_num < 0 )
                break;

            System.out.println("\nEnter another: ");
            num = keys.nextInt();
        }

        System.out.println("Number is Negative! System Shutdown!");

    }
}

答案 1 :(得分:0)

只需计算迭代期间的最大值和最小值

while(num>0) {
    System.out.println("Max = " + Max);
    System.out.println("Min = " + Min);
    System.out.println("\nEnter another: ");
    num = keys.nextInt();
    if(Math.abs(num) > max) {
        max = Math.abs(num);
    } 
    if(Math.abs(num) < min) {
        min = Math.abs(num);
    }  
}

System.out.println("Max = " + Max);
System.out.println("Min = " + Min);
System.out.println("Number is Negative! System Shutdown!");
System.exit(1);

答案 2 :(得分:0)

import java.util.Scanner;

public class NegativeNum {
    private static int entryCount = 0;  // Count the Number of Entries

    public static void main(String []args) {

        Scanner keys = new Scanner(System.in);
        System.out.println("Enter a number: ");

        double num = keys.nextInt();
        double Max = num+0.5;
        double Min = num-0.5;


        while(true) {
            if( num < 0 && entryCount == 0) { // Make sure if it's first entry and it's negative too
                System.out.println("Number is Negative! System Shutdown!");
                System.exit(1);
            }
            System.out.println("Max = " + Max);
            System.out.println("Min = " + Min);
            System.out.println("\nEnter another: ");
            num = keys.nextInt();
            entryCount++;
        }

    }
}

答案 3 :(得分:0)

此代码成功运行所有情况: -

import java.util.Scanner;

public class class2 
{


        public void positive(double num)
        {
         double Max = num+0.5;
         double Min = num-0.5;

             System.out.println("Max = " + Max);
             System.out.println("Min = " + Min);
             System.out.println("\nEnter another: ");


        }

        public void negative(double num,String k)
            {

            double Max = num+0.5;
            double Min = num-0.5;
            System.out.println("Max = " + Max);
            System.out.println("Min = " + Min);


               if(k=="terminate")
               {
                 System.out.println("System is shutting down");
                 System.exit(1);
               }
            System.out.println("\nEnter another: ");

       }

public static void main(String []args) 
{

    class2 obj=new class2();
    Scanner keys = new Scanner(System.in);
    System.out.println("Enter a number: ");
    boolean bol=true;
    String k="";
    double num = keys.nextInt();
    int count=1;
    if(num<0 && count ==1) 
    {
       k="terminate";
       count=count+1;

       System.out.println("Max and Min undefined");
       System.exit(1);
       System.out.println("\nEnter another: ");
       num = keys.nextInt();

    }

    while(bol==true)
    {
    while(num>0) 
    {
           count=count+1;
           obj.positive(num);
           num = keys.nextInt();


    }
    while(num<0 && count!=2)
    {
         k="terminate";
         obj.negative(num,k);
         num = keys.nextInt();
    }
    count=count+1;
    }

}
}