0

我最后想要我的输出,但我的程序在每次输入后都会给出它。请告诉我应该做哪些改变!我的程序从最多十个数字中整理出用户输入的所有负数。我的程序是:

import java.io.*;
public class Task_3
{
    public static void main(String args[])throws IOException
    {
        double a, b;
        InputStreamReader read= new InputStreamReader(System.in);
        BufferedReader in= new BufferedReader(read);
        for(a=1;a<=10;a++)
        {
            System.out.println("Enter your number");
            b=Double.parseDouble(in.readLine());
            if(b<0)
            {
                System.out.println(b);
            }
        }
    }
}

结果显示在输出屏幕上:

Enter your number
5
Enter your number
6
Enter your number
1
Enter your number
45
Enter your number
874
Enter your number
-547
-547.0
Enter your number
-8
-8.0
Enter your number
-1
-1.0
Enter your number
-5
-5.0
Enter your number
6
4

4 回答 4

0

如果您希望所有小于 0 的数字都显示在末尾,请将它们存储在数组中

    double[] arr = new double[10];
    int count=-1;
    for(a=1;a<=10;a++)
    {
        System.out.println("Enter your number");
        b=Double.parseDouble(in.readLine());
        if(b<0)
        {
            arr[++count] = b;
        }
    }
    for(a=1;a<=count;a++) System.out.println(arr[a]);
于 2013-11-06T11:23:22.497 回答
0

这样做:

import java.io.*;
public class Task_3
{
    public static void main(String args[])throws IOException
    {

        InputStreamReader read= new InputStreamReader(System.in);
        BufferedReader in= new BufferedReader(read);
        double[] numMass = new double[10];
        int count = 0;
        for(int a = 0; a < 10; a ++)
        {
            System.out.println("Enter your number");
            double b=Double.parseDouble(in.readLine());
            if(b < 0)
            {
                numMass[count] = b;
                count++;    
            }
        }
        for (int i = 0; i < count; i++) {
            System.out.println(numMass[i]);
        }
    }
}
于 2013-11-06T11:26:36.617 回答
0

如果您想首先从标准输入中读取 10 个数字,然后将它们一起打印,那么您必须首先使用例如 an 存储这些数字ArrayList,然后将它们分别打印出来。

public static void main(String args[])throws IOException
{
    double a, b;
    List<Double> numbers = new ArrayList<>();
    InputStreamReader read= new InputStreamReader(System.in);
    BufferedReader in= new BufferedReader(read);
    for(a=1;a<=10;a++)
    {
        System.out.println("Enter your number");
        b=Double.parseDouble(in.readLine());
        if(b<0)
        {
            numbers.add(b); // Store number in list.
        }
    }
    for (Double d : numbers) { // Print out the numbers.
        System.out.println(d + " ");
    }
}
于 2013-11-06T11:27:00.667 回答
0

如果您只想输出过滤后的数字,您可以使用 StringBuilder 组合输出并在输入模式结束后将其打印在屏幕上:

StringBuilder result = new StringBuilder();
for (/* your for loop) {
    /* your input logic */
    if (b < ) {
        result.append(b + "\n");
    }
}
System.out.println(result.toString());

如果您必须对结果执行额外的处理(并且需要 Double 格式),请使用以下截图:

List<Double> result = new LinkedList<>();
/* same as above instead, BUT */
      result.add(b);
/* ... */

for (Double d : result) {
     /* do your processing of the result here */
}
于 2013-11-06T11:27:08.370 回答