Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/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 parseInt_Java_Parseint - Fatal编程技术网

使用输出的java parseInt

使用输出的java parseInt,java,parseint,Java,Parseint,下面的代码接受用户输入的10个整数,在空格中使用String.split(“”),将它们解析为整数并打印出来 public class variableGrowth { public main variableGrowth() { System.out.print('\u000c'); //scanner and variables Scanner scan = new Scanner(System.in);

下面的代码接受用户输入的10个整数,在空格中使用
String.split(“”
),将它们解析为整数并打印出来

public class variableGrowth
{
    public main variableGrowth()
    {
        System.out.print('\u000c');
            //scanner and variables 
        Scanner scan = new Scanner(System.in);
        String inputInt = scan.nextLine();
        System.out.println("These are the integers you entered: "+inputInt);

        String[] items = inputInt.split(" ");
        int[] results = new int[items.length];

        for (int i = 0; i < items.length; i++) {
            try {
                int stuff = Integer.parseInt(items[i]);
                System.out.println(stuff);
                } 
            catch (NumberFormatException nfe) {
                System.out.println(results);
                System.out.println(errorMessage);   
                }
            }
        }
    }    
}

除非声明变量errorMessage,否则代码将无法编译

您只需初始化一个数组来存储结果,但从不使用它。在循环中,应该存储每个解析的值

results[i] = stuff;
然后在循环外部,您可以访问所有整数来执行某些操作

但是对于简单的求和操作,可以定义一个变量,然后对循环中的每个新int求和

int sum;
for ...
    // get your stuff here
    sum += stuff;
而且我一开始没有注意到你的方法签名。但是主方法的签名是

public static void main(String args[])
在Java中,您的方法签名应遵循:

[accessor] [static] 'returnType' yourMethodName('yourParams')

您所需要做的就是创建一个额外的变量,该变量将包含您的跑步总数

int total = 0;
for (...) {
    System.out.println(value);
    total += value;
    System.out.println("Running Total: " + total)
}

有几种方法可以做到这一点。如果要寻找所有数字的直接和,只需在循环外保留一个sum变量,并在所有迭代中保持一个工作和

int sum = 0;
for (int i = 0; i < items.length; i++) {
  try {
    int stuff = Integer.parseInt(items[i]);
    sum += stuff;
    ...
}
int和=0;
对于(int i=0;i
另一种方法是将所有数字(特别是如果需要对它们执行其他操作)存储在列表或数组中,并在事后执行操作

int[] numbers = new int[items.length];
for (int i = 0; i < items.length; i++) {
  try {
    int stuff = Integer.parseInt(items[i]);
    numbers[i] = stuff;
    ...
}

// Perform operations here
int[]number=新的int[items.length];
对于(int i=0;i
使用流(Java 8):


那么Arrays.stream(items).mapToInt(Integer::parseInt).sum();)呢?非常感谢。我想第二部分正是我要找的。当我把:double c=numbers[I]/100;放在你的代码后面,然后是System.out.println(c);,c总是等于零。有什么想法吗?我假设你把那一行放进去(double c=numbers[I]/100)在for循环内部?(i变量在for循环外部不存在)。您的数字是否太小?如果数字[i]小于100,您将得到0,因为您正在进行整数除法。您可能希望除以100.0或除以(双精度)100我如何单独调用每个元素?再次澄清,我不是在数组中寻找整数的和,我想用数组中的每个整数作为变量求和。也就是说,在循环迭代0时,我想做类似结果[0]*3的事情。在循环迭代1时,我想做结果[1]*3,然后打印所有结果。
int[] numbers = new int[items.length];
for (int i = 0; i < items.length; i++) {
  try {
    int stuff = Integer.parseInt(items[i]);
    numbers[i] = stuff;
    ...
}

// Perform operations here
import java.util.*;
import java.lang.*;

class Main
{
    public static void main(String[] args) {
        System.out.print('\u000c');
        //scanner and variables
        Scanner scan = new Scanner(System.in);
        String inputInt = scan.nextLine();
        System.out.println("These are the integers you entered: "+inputInt);

        String[] items = inputInt.split(" ");

        int sum = Arrays.stream(items)
                .mapToInt(x -> Integer.parseInt(x))
                .sum();

        System.out.println(String.format("Sum is: %s", sum));
    }
}