简体   繁体   English

扫描仪提示用户输入数字

[英]Scanner to prompt user for numbers

Here are the instructions I'm trying to follow: 以下是我要遵循的说明:

Write a method called negative sum that accepts a scanner reading input from a file containing a series of integers., and a print message to the console indicating whether the sum starting from the first number is ever negative.You should alse return true if a negative sum can be reached and false if not. 编写一个称为负数总和的方法,该方法接受扫描仪从包含一系列整数的文件中读取输入,并向控制台显示一条打印消息,指示从第一个数字开始的总和是否为负数。如果为负数,还应返回true可以达到总和,否则不可以。 for example, suppose the file contains the 38 4 19 -27 -15 -3 4 19 38 your method would consider the sum of just number (38) , the first two numbers ( 38 + 4) the firs three number ( 38 + 4 + 19) and so on to the asnd . 例如,假设文件包含38 4 19 -27 -15 -3 4 19 38您的方法将考虑正数(38),前两个数字(38 + 4)和第三个数字(38 + 4 + 19),以此类推。 none of thes sume is negative so the method would produce the following output and return false : No negative numbers. 它们的sume都不为负,因此该方法将产生以下输出并返回false:没有负数。

If the file instead contains 14 7 -10 9 -18 -10 17 42 98 the method finds that a negative sum of -8 is reached after adding the first six numbers. 如果该文件包含14 7 -10 9 -18 -10 17 42 98,则该方法发现在加上前六个数字后达到负-8。 it should output the following to the console and return true: sum of -8 after 6 steps. 它应将以下内容输出到控制台并返回true:6个步骤后的总和为-8。

This is what I have so far. 到目前为止,这就是我所拥有的。 I'm just having trouble adding the scanner to prompt the user for the numbers. 我只是在添加扫描仪以提示用户输入数字时遇到麻烦。

import java.io.*;
import java.util.*;

public class NegativeSum{
    public static void main (String [] args )
    throws FileNotFoundException{


        negativesum();


        }//end of amin

public static boolean negativesum()
throws FileNotFoundException{


     File file = new File ("negativeSum.txt");
    Scanner input =  new Scanner (file);

    int sum=0;
    int count = 0;

    while ( input.hasNextInt()){
        int next =input.nextInt();
        sum+=next;
        count++;

        if ( sum<0){
            System.out.println("sum of " + sum + " after " + count + "steps" );
            return true;
            }

        }///end of while
    System.out.println("no negative sum ");
    return false;




    }//end of metho d

}//end of main

The only serious mistake I see in your implementation (vs. your problem statement) is that your method should receive a Scanner as input (eg accepts a scanner ) - 在您的实现中(相对于您的问题陈述),我看到的唯一严重错误是您的方法应接收“ Scanner作为输入(例如, 接受扫描仪 )-

public static boolean negativeSum(Scanner input) {
  if (input == null) {
    // Handle null - e.g. no value
    return false;
  }
  int sum = 0;
  int count = 0;

  while (input.hasNextInt()) {
    int next = input.nextInt();
    sum += next;
    count++;

    if (sum < 0) {
      System.out.println("sum of " + sum + " after "
          + count + " steps");
      return true;
    }
  }// /end of while
  System.out.println("no negative sum");
  return false;
}

I don't see anything in your problem description that asks you to prompt the user for numbers. 我在问题描述中没有看到任何要求您提示用户输入数字的内容。 It appears that your code already meets the stated requirements of the assignment. 您的代码似乎已经满足指定的分配要求。

If you did want to do that, though: 但是,如果您确实想这样做:

  • System.in is an InputStream representing standard input. System.in是一个InputStream代表标准输入。
  • The Scanner(InputStream) constructor creates a Scanner that reads from the specified InputStream . Scanner(InputStream)构造函数创建一个Scanner ,该Scanner从指定的InputStream读取。

I'll leave it as an exercise to you to figure out how to put the two together. 我将把它留给您作为练习,以弄清楚如何将两者结合在一起。 :) :)

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

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