Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 如何在用户每次输入数据时增加变量?_Java_Variables_Increment - Fatal编程技术网

Java 如何在用户每次输入数据时增加变量?

Java 如何在用户每次输入数据时增加变量?,java,variables,increment,Java,Variables,Increment,我有一个程序,它会提示用户输入整数,直到他们输入一个值,然后继续执行下一步的程序。每次用户输入整数时,如何增加一个名为count的变量?顺便说一句,我正在使用count++来增加计数量,我只是不知道当用户输入数据时如何使计数增加 迄今为止的代码 //variables int num, count = 0, high, low; Scanner userInput = new Scanner(System.in); //loop

我有一个程序,它会提示用户输入整数,直到他们输入一个值,然后继续执行下一步的程序。每次用户输入整数时,如何增加一个名为count的变量?顺便说一句,我正在使用count++来增加计数量,我只是不知道当用户输入数据时如何使计数增加

迄今为止的代码

//variables
        int num, count = 0, high, low;
        Scanner userInput = new Scanner(System.in);

        //loop

        do {
                    System.out.print("Enter an integer, or -99 to quit: --> ");
                    num = userInput.nextInt();


                    high = num;
                    low = num;

                    //higher or lower
                    if(count > 0 && num > high)
                    {
                       high = num; 
                    }
                    else if(count > 0 && num < low)
                    {
                        low = num;
                    }
                    else
                    {
                       System.out.println("You did not enter any numbers."); 
                    }

                } while (num != -99);

        System.out.println("Largest integer entered: " + high);
        System.out.println("Smallest integer entered: " + low);
//变量
int num,count=0,高,低;
扫描仪用户输入=新扫描仪(System.in);
//环路
做{
System.out.print(“输入整数,或-99退出:->”;
num=userInput.nextInt();
高=num;
低=num;
//高还是低
如果(计数>0&&num>高)
{
高=num;
}
否则如果(计数>0&&num<低)
{
低=num;
}
其他的
{
System.out.println(“您没有输入任何数字。”);
}
}而(num!=-99);
System.out.println(“输入的最大整数:“+高”);
System.out.println(“输入的最小整数:“+low”);

您可以将
计数++在do循环中的任意位置。

这很简单。只需将
count++
放入
while循环中,如下所示

// variables
int num, count = 0, high, low;
Scanner userInput = new Scanner(System.in);

// loop
do {
   System.out.print("Enter an integer, or -99 to quit: --> ");
   num = userInput.nextInt();

   count++; // here it goes

   high = num;
   low = num;

   // higher or lower
   if(count > 0 && num > high)
   {
      high = num; 
   }
   else if(count > 0 && num < low)
   {
      low = num;
   }
   else
   {
       System.out.println("You did not enter any numbers."); 
   }

} while (num != -99);

System.out.println("Largest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);
//变量
int num,count=0,高,低;
扫描仪用户输入=新扫描仪(System.in);
//环路
做{
System.out.print(“输入整数,或-99退出:->”;
num=userInput.nextInt();
count++;//开始了
高=num;
低=num;
//高还是低
如果(计数>0&&num>高)
{
高=num;
}
否则如果(计数>0&&num<低)
{
低=num;
}
其他的
{
System.out.println(“您没有输入任何数字。”);
}
}而(num!=-99);
System.out.println(“输入的最大整数:“+高”);
System.out.println(“输入的最小整数:“+low”);

我明白你为什么要用它,但为什么要用计数器

下面的代码使用不同的技术从用户处获取整数。它还可以确保提供的数字确实是一个整数,且在integer.MIN_值和integer.MAX_值范围内:

Scanner userInput = new Scanner(System.in);
String ls = System.lineSeparator();

int high = 0;
int low = Integer.MAX_VALUE;
int num;
String input = "";
while(input.equals("")) {
    System.out.print("Enter an integer, (q to quit): --> ");
    input = userInput.nextLine().toLowerCase();
    if (input.equals("q")) {
        if (low == Integer.MAX_VALUE) {
            low = 0;
        }
        break;
    }
    if (!input.matches("^-?\\d+$")) {
        System.err.println("Invalid Entry (" + input + ")! "
                         + "You must supply an Integer (int) value!" + ls);
        input = "";
        continue;
    }

    boolean invalidInteger = false;
    long tmpVal=0;
    try {
        tmpVal = Long.parseLong(input);
    } catch(NumberFormatException ex) {
        invalidInteger = true;
    }
    if (invalidInteger || tmpVal < Integer.MIN_VALUE || tmpVal > Integer.MAX_VALUE) {
        System.err.println("Invalid Entry (" + input + ")! " + ls
                         + "Number too large (Minimum Allowable: " + Integer.MIN_VALUE 
                         + "  Maximum Allowable: " + Integer.MAX_VALUE + ")!" + ls
                         + "You must supply an Integer (int) value!" + ls);
        input = "";
        continue;
    }
    num = Integer.parseInt(input);
    if (num > high) {
        high = num;
    }
    if (num < low) {
        low = num;
    }
    input = "";
}
System.out.println("Largest integer entered:  " + high);
System.out.println("Smallest integer entered: " + low);
Scanner userInput=新扫描仪(System.in);
字符串ls=System.lineSeparator();
int高=0;
int low=整数最大值;
int-num;
字符串输入=”;
while(input.equals(“”){
System.out.print(“输入一个整数,(q表示退出):-->”);
input=userInput.nextLine().toLowerCase();
if(输入等于(“q”)){
if(低==整数.MAX_值){
低=0;
}
打破
}
如果(!input.matches(“^-?\\d+$”){
System.err.println(“无效条目(“+input+”)!”
+“必须提供整数(int)值!”+ls);
输入=”;
继续;
}
布尔无效整数=假;
长tmpVal=0;
试一试{
tmpVal=Long.parseLong(输入);
}捕获(NumberFormatException ex){
无效积分=真;
}
if(无效整数| | tmpValInteger.MAX_值){
System.err.println(“无效条目(“+input+”)!+ls
+“数字太大(允许的最小值:”+Integer.MIN_值
+“允许的最大值:“+Integer.MAX_VALUE+”)!+ls
+“必须提供整数(int)值!”+ls);
输入=”;
继续;
}
num=整数.parseInt(输入);
如果(数值>高){
高=num;
}
如果(数值<低){
低=num;
}
输入=”;
}
System.out.println(“输入的最大整数:“+高”);
System.out.println(“输入的最小整数:“+low”);

如果您想记录用户输入的条目数量,那么如果您愿意,您仍然可以应用计数器。

count++或count=count+1如何?谢谢!令人惊讶的是,这么简单的事情竟把我弄糊涂了。有没有办法使某个整数不包含在计数中?例如,用户可以键入-99继续通过提示,但是我怎么会将-99不包括在num或count increment中呢?实际上,我没有得到您所要求的。但是如果您输入-99,它将结束循环的
for
对吗?但是对于您的问题
是否有办法不将某个整数包括在计数中?
是的,您可以。您可以尝试使用一个简单的
if-else
语句。如果你需要更多的帮助,请告诉我。