简体   繁体   English

Java:如何为一个int读取多个扫描仪值

[英]Java: How to read multiple scanner values for one int

I have been trying to figure out how calculate the area and volume based on the input obtain from the Scanner Class. 我一直试图弄清楚如何根据从Scanner类获得的输入来计算面积和体积。 The exercise consist of receiving multiple pair of radius and height at once. 练习包括一次接收多对半径和高度。

I have written the methods and tested them, so those should be working. 我已经编写了方法并对其进行了测试,因此它们应该可以工作。 The problem I am having is when I want to use inputs from 'Scanner' and use them to make the calculations. 我遇到的问题是当我想使用“扫描仪”中的输入并使用它们进行计算时。

Here is my code (I did not include the methods): 这是我的代码(我没有包括方法):

    Scanner keyboard = new Scanner(System.in); 

    int radius = 0;
    int height = 0;

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    int[] numbers = new int[items.length];

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

    for (int i = 0; i < numbers.length/2; i++)
    {
        numbers[i] = radius;
        numbers[i + 1] = height;

        double result = area(radius);
        double result1 = area (radius,height);
        double result2 = volume (radius,height);

        System.out.println("");
        System.out.print("r = " + radius + "    ");
        System.out.print("h = " + height);
        System.out.println("");
        System.out.print("Base area:    ");
        System.out.printf("%.2f", result);
        System.out.println("");
        System.out.print("Surface area: ");
        System.out.printf("%.2f", result1);
        System.out.println("");
        System.out.print("Volym:            ");
        System.out.printf("%.2f", result2);
        System.out.println("");
    }
    keyboard.nextLine(); 

Those are the Results: 这些是结果:

Input: 2 4 5 1

Output: 


r = 0    h = 0

Base area:        0.00

Surface area:     0.00

Volym:            0.00



r = 0    h = 0

Base area:        0.00

Surface area:     0.00

Volym:            0.00

These two lines are wrong: 这两行是错误的:

numbers[i] = radius;
numbers[i + 1] = height;

radius and height should be on the left hand side: radiusheight应在左侧:

radius = numbers[i];
height = numbers[i + 1];

Also, if you want to "group" the inputs into pairs, this won't work: 同样,如果您要将输入“分组”成对,则将无法使用:

radius = numbers[i];
height = numbers[i + 1];

This will just group the input like "(0, 1), (1, 2)". 这只会将输入分组为“((0,1),(1,2)””。 Use this instead; 改用它;

radius = numbers[i * 2];
height = numbers[i * 2 + 1];

you must do this: 您必须这样做:

for (int i = 0; i < numbers.length; i+=2)
{
    radius = numbers[i];
    height = numbers[i + 1];
    .
    .
    .
}   
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int radius = 0;
    int height = 0;

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    int[] numbers;

    if(items.length % 2 == 0){ //Making sure you are having even number of inputs
        numbers = new int[items.length];

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

        for(int i = 0,j = -1; i < numbers.length / 2; i++){
            radius = numbers[++j];
            height = numbers[++j];
            System.out.println("radius: " + radius + " height: " + height);

            //Write Your Area Volume Code Here and use the radius and height which is there in the loop for calculation
            //Your code goes here
            //Your code ends here
        }
    }else{
        System.out.println("Incorrect input provided");
    }
    keyboard.close();
}}

Give this piece a try. 尝试一下。 The sample output is provided below: 下面提供了示例输出:

Enter values 1 2 3 4 5 6 7 8 9 10 radius: 1 height: 2 radius: 3 height: 4 radius: 5 height: 6 radius: 7 height: 8 radius: 9 height: 10 输入值1 2 3 4 5 6 7 8 9 10半径:1高度:2半径:3高度:4半径:5高度:6半径:7高度:8半径:9高度:10

Thanks 谢谢

请注意:首次初始化后,半径和高度不会获得新值

try some as: 尝试一些作为:

public class NewMain
{
class Crate
{
    public int radius;
    public int height;
}

public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter values ");
    String input = keyboard.nextLine();

    String[] items = input.split(" ");
    Crate[] numbers = new Crate[items.length];

    for (int i = 0; i < items.length; i ++)
    {
        numbers[i].radius = Integer.parseInt(items[i * 2]);
        numbers[i].height = Integer.parseInt(items[i * 2 + 1]);
    }
}
}

I think you can try this solution. 我认为您可以尝试此解决方案。

The method processItems() cleans all the white-spaces between numbers and this line of code if (integers.length % 2 == 0) makes sure we have even number of inputs. if (integers.length % 2 == 0)确保我们有偶数个输入,则processItems()方法将清除数字和该行代码之间的所有white-spaces So we have always a pair of numbers ( Radius and Height ). 因此,我们总是有一对数字RadiusHeight )。

I also created a class InputPair and an array of InputPair to print the pairs an maintain modularity. 我还创建了一个类InputPair和阵列InputPair打印对一个维持模块性。

You can test with this for even number of inputs: 您可以用它测试偶数输入:

2 9 4 7 5 6 7 1 3 8 12 5 8 2 3 9 2 9 2 7 2 9 4 7 5 6 7 1 3 8 12 5 8 2 3 9 2 9 2 7

And this for odd number of inputs: 对于奇数输入,这是:

2 9 4 5 6 7 1 3 8 12 5 8 6 3 9 2 9 2 7 2 9 4 5 6 7 1 3 8 12 5 8 6 3 9 2 9 2 7

You can find here the full Java code 您可以在此处找到完整的Java代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static class InputPair {
        public int radius;
        public int height;

        public InputPair() {}

        public void printValues() {
            System.out.println("Radius: " + radius + " Height: " + height);
        }
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter values ");
        String input = keyboard.nextLine();

        String[] items = input.split(" ");
        Integer[] integers = processItems(items);
        InputPair[] values = new InputPair[integers.length];

        System.out.println(Arrays.toString(items));
        System.out.println("items length:  " + items.length);

        System.out.println(Arrays.toString(integers));
        System.out.println("items length:  " + integers.length);

        if (integers.length % 2 == 0) { 
            for (int i = 0; i < integers.length / 2; i++) {
                System.out.print("Index:  " + i + " ");
                try {
                    values[i] = new InputPair();
                    values[i].radius = integers[i * 2];
                    values[i].height = integers[i * 2 + 1];
                    values[i].printValues();
                } catch (NullPointerException ne) {
                    System.out.println("Error NullPointerException");
                }

            }
        } else {
            System.out.println("The number of values bust be an even number. ");
        }
    }

    private static Integer[] processItems(String[] items) {
        List<Integer> numbers = new ArrayList<>();

        for (String s : items) {
            //Do your stuff here
            s = s.replaceAll("[^\\d]", "");
            if (!s.equals("")) {
                numbers.add(Integer.parseInt(s));
                System.out.println(Integer.parseInt(s));
            }
        }

        Integer[] stockArr = new Integer[numbers.size()];
        return numbers.toArray(stockArr);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM