简体   繁体   English

为什么我的循环运行的时间比我想要的少?

[英]Why is my loop running one less time than I'd like?

For class I am supposed to write a number scanner that prints user input, the sum and product of these numbers. 在课堂上,我应该写一个数字扫描仪来打印用户输入,这些数字的总和和乘积。 We can assume the user enters a space after every number and the user may enter as many numbers as they'd like. 我们可以假设用户在每个数字之后输入一个空格,并且用户可以输入任意数量的数字。 Ex: 3 10 2 Your numbers are: 3 10 2 Sum: 15 Product: 60 例如:3 10 2您的电话号码是:3 10 2总和:15产品:60

public static void substringGrabber () {
    String recentInt;
    int lastDigit = 0;
    int parsedInt = 0;
    for (int i = 0; i < numbers.length(); i++) {
        if (! Character.isDigit(numbers.charAt(i))) {
            char ignoredCharacter = numbers.charAt(i);
            if (ignoredCharacter != ' ') {
                System.out.println("Your input of:" + ignoredCharacter + " has been ignored");
            }
            recentInt = numbers.substring(lastDigit, i);
            parsedInt = Integer.parseInt(recentInt);
            sum += parsedInt;
            product *= parsedInt;
            lastDigit = i + 1;
            finalString = finalString + parsedInt + " ";

        }

    }

    System.out.println("Your numbers are: " + finalString);
    System.out.println("Sum: " + sum);
    System.out.println("Product: " + product);
}

nextInt() which resides in Scanner by default uses white-space as a token separator. 默认情况下,位于ScannernextInt()使用空格作为标记分隔符。 Therefore, your code can look like this (ask them in advance how many numbers they're going to type in): 因此,您的代码可能看起来像这样(事先询问他们要输入多少个数字):

System.out.println("How many numbers?");
int numbers = scanner.nextInt();
int sum = 0;
int product = 0;
int[] arr = new arr[numbers];
for (int i = 0; i < numbers; i++){
    System.out.println("Type in a number");
    int input = scanner.nextInt();
    sum += input;
    arr[i] = input;
    product *= input;
}

System.out.println("Your numbers are: " + Arrays.deepToString(arr));
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);

If you add a space at the end of your test string it will work. 如果在测试字符串的末尾添加空格,它将起作用。

this check !Character.isDigit(numbers.charAt(i)) ignores the last digit. 此检查!Character.isDigit(numbers.charAt(i))忽略最后一位。

so change the logic to account for that that case : you have a digit and you are at the end of your string. 因此请更改逻辑以解决该情况:您有一个数字,并且位于字符串的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的循环再运行一遍,我看不出为什么 - My loop is running one extra time and I can't see why 为什么我的程序读取的行数比实际少一行? 为什么我的数组只接收一个? - Why is my program reading one less line than there actually is? And why is my array taking in only ones? 为什么运行时间越少,数量越大 - why running time less when it number bigger 为什么模拟器的随机性比我的设备低? - Why is the emulator less random than my device? 循环比我预期的多运行一次 - Loop runs one more time than I expected 循环的大小打印的数字比我想在我的 Set JAVA 中要少 - Size of the loop is printing less numbers than i want to be in my Set JAVA android-为什么当前时间小于Calendar中的回溯日期时间 - android - why is current time less than a back date time in Calendar Java:为什么我的for循环又循环一次,然后在我第一次尝试中断之后又中断呢? - Java: Why does my for loop cycle through one more time and then break after I try to break out of it the first time? 为什么我的hasNextLine()while循环只能工作一次? - Why is my hasNextLine() while loop only working one time? 为什么我很简单的while循环重复一个额外的时间? - Why is my very simple while loop repeating one extra time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM