想要在另一个地方输出

时间:2013-11-06 11:17:52

标签: java bluej

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

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 个答案:

答案 0 :(得分: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]);

答案 1 :(得分: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]);
        }
    }
}

答案 2 :(得分:0)

如果您想首先从标准输入读取10个数字,然后将它们一起打印出来,那么您必须先使用例如数字存储数字。 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 + " ");
    }
}

答案 3 :(得分: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 */
}