我如何检测nextInt的内容,或者它是否在最后?

时间:2015-09-02 13:40:33

标签: java

以下问题来自Daniel Liang的 Java简介一书的第5章:

  

(计算正数和负数并计算数字的平均值)写   读取未指定数量的整数的程序确定了多少   已读取正值和负值,并计算总值和平均值   输入值(不包括零)。您的程序以输入0结束。显示   平均值为浮点数   
  这是一个示例运行:
  输入一个整数,输入结束,如果它是0:1 2 -1 3 0
  积极的数量是3
  否定数量为1
  总数是5.0
  平均值为1.25
  
输入一个整数,输入结束,如果它是0:0
  除0

外,不输入任何数字

我的代码目前满足了问题的要求,但我想涵盖我的所有基础。有了Java的基础知识,有没有办法可以检查我的while语句中的def IndexPage(strUrl, strServer): ... strPage = "<!DOCTYPE html><html>"; strPage = strPage + "<head><title>Match report comments.</title></head>"; strPage = strPage + "<body><form action=\"" + strServer + "\">"; strPage = strPage + "Group:<br><input type=\"text\" name=\"group\" value=\"" + arrGroupValue[1] + "\">"; strPage = strPage + "<br>Unit:<br><input type=\"text\" name=\"unit\" value=\"" + strUnit + "\">"; strPage = strPage + "<br>Information:<br><textarea rows=\"8\" cols=\"30\" name=\"info\">" + strInfo + "</textarea>"; strPage = strPage + "<br>Resp:<br><input type=\"text\" name=\"responsible\" value=\"" + strResp + "\">"; strPage = strPage + "<br><br><input type=\"submit\" value=\"Submit\"></form></body></html>"; return strPage; ,看看是否:

  1. 数字序列不以0结尾。在我的测试中,程序似乎停止了(请注意我的While语句中的调试输出)。我猜测没有一种简单的方法可以做到这一点,因此我们将0用作哨兵,但我想我会问。

  2. 如果仅按下Enter键。当我在NetBeans中执行此操作时,提示移动到下一行,我想包含一条消息,通知用户没有输入任何内容并输入整数。

  3. 我的代码:

    nextInt

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

item2 :=  reflect.ValueOf(i)
fmt.Printf("Hello, item2 : %v\n", item2)

使用您的扫描仪,您可以使用package positivenegative; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class PositiveNegative { public static void main(final String[] args) { // Welcome message System.out.println("Welcome to the Number Averagererer."); // Yes I'm aware "Averagererer" isn't a word System.out.println("Be sure to enter a 0 at the end"); System.out.println("or the program will not work."); System.out.print("Enter an integer, the input ends if it is 0: "); // Initialize variables int number = -1; int totalPositive = 0; int totalNegative = 0; int counter = 0; double average = 0; String line = ""; while (line.isEmpty()) { line = readLine(); if (line.isEmpty()) { System.out.println("Line is empty. Enter number!"); } } try (final Scanner input = new Scanner(line)) { // Process the entered line and sort/calculate them while (input.hasNextInt()) { number = input.nextInt(); if (number == 0) { // stop if number is 0 break; } System.out.println("number: " + number);// TODO: remove // Adds to positive if (number > 0) { totalPositive++; } // Adds to negative else { totalNegative++; } counter++; average = (average + number); } // Output message if the user only enters "0" if (counter == 0) { System.out.println("No numbers were entered except 0"); } // Output message with calculated data from input else { final double total = average; average = (average / counter); System.out.println("The number of positives is: " + totalPositive); System.out.println("The number of negatives is: " + totalNegative); System.out.println("The total is: " + total); System.out.println("The average is: " + average); } } } private static String readLine() { try { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final String s = br.readLine(); return s; } catch (final IOException e) { e.printStackTrace(); return ""; } } } 来测试是否有更多数字,但它会阻止,直到有。有关阻止的更多详细信息,请参见here