Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/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 需要在另一个地方输出_Java_Bluej - Fatal编程技术网

Java 需要在另一个地方输出

Java 需要在另一个地方输出,java,bluej,Java,Bluej,我最终想要我的输出,但我的程序在每次输入后都会给出它。请告诉我我应该做什么改变!我的程序将用户输入的所有负数从最多十个数字中进行排序。 我的计划是: import java.io.*; public class Task_3 { public static void main(String args[])throws IOException { double a, b; InputStreamReader read= new InputStream

我最终想要我的输出,但我的程序在每次输入后都会给出它。请告诉我我应该做什么改变!我的程序将用户输入的所有负数从最多十个数字中进行排序。 我的计划是:

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);
            }
        }
    }
}

如果要在末尾显示所有小于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]);
double[]arr=新的双精度[10];
整数计数=-1;
对于(a=1;a

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]);
        }
    }
}
import java.io.*;
公开课任务3
{
公共静态void main(字符串args[])引发IOException
{
InputStreamReader read=新的InputStreamReader(System.in);
BufferedReader in=新的BufferedReader(读取);
double[]numMass=新的double[10];
整数计数=0;
对于(int a=0;a<10;a++)
{
System.out.println(“输入您的号码”);
double b=double.parseDouble(in.readLine());
if(b<0)
{
numMass[count]=b;
计数++;
}
}
for(int i=0;i
如果您想先从标准输入中读取10个数字,然后将它们一起打印,则必须先使用
数组列表等存储数字,然后分别打印出来

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 + " ");
    }
}
publicstaticvoidmain(字符串args[])引发IOException
{
双a,b;
列表编号=新的ArrayList();
InputStreamReader read=新的InputStreamReader(System.in);
BufferedReader in=新的BufferedReader(读取);

对于(a=1;a如果只想输出过滤后的数字,可以使用StringBuilder合成输出,并在输入模式结束后在屏幕上打印:

StringBuilder result = new StringBuilder();
for (/* your for loop) {
    /* your input logic */
    if (b < ) {
        result.append(b + "\n");
    }
}
System.out.println(result.toString());
StringBuilder结果=新建StringBuilder();
for(/*您的for循环){
/*你的输入逻辑*/
如果(b<){
结果。追加(b+“\n”);
}
}
System.out.println(result.toString());
如果必须对结果执行其他处理(并且需要双重格式),请使用以下截取的格式:

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

for (Double d : result) {
     /* do your processing of the result here */
}
List result=newlinkedlist();
/*与上面相同,但是*/
结果.添加(b);
/* ... */
for(双d:结果){
/*请在此处对结果进行处理*/
}

你的程序没有对数字进行排序你想看到所有小于0的数字吗?ankit rustagi:“sorts out”=删除,而不是排序你想在其他地方报告正数吗?如果是这样,就把它作为参数传递给函数“report(b);”在else语句中。这不会编译,因为
numMass
没有维度。数组也不会打印。修复了)我忘记打印了)你不应该使用
LinkedList
来处理像这样琐碎的事情,使用
ArrayList
@Snps我想这取决于我们之后对结果列表的处理。
LinkedList
对于插入的复杂性为
O(1)
ArrayList
在大多数情况下具有
O(1)
但是
O(n)
在最坏的情况下。在这种情况下,您将添加到列表的末尾,并迭代所有元素,这些元素是
O(1)
各自的
O(n)
用于两种列表类型。
ArrayList
可能会优于
LinkedList
,即使是在
LinkedList
的最佳条件下,这是因为更高的内存位置(更紧凑的数据结构)。
LinkedList
也将占用至少4倍的空间。在一般情况下,不应使用
LinkedList
List<Double> result = new LinkedList<>();
/* same as above instead, BUT */
      result.add(b);
/* ... */

for (Double d : result) {
     /* do your processing of the result here */
}