从扫描仪输入输出多个字符串

时间:2014-10-30 01:42:16

标签: java string java.util.scanner

我的程序的目的是接受来自用户的整数,双精度和字符串,当程序通过输入单词" quit"终止时,程序平均整数和双精度,并输出提交字符串。以下是我到目前为止的情况:

import java.util.*;

public class Lab09 {
    public static void main(String [] args) {
        Scanner console = new Scanner(System.in);

      double sumI = 0;
      double sumD = 0;
      String words = "";
      int numInputInt = 0;
      int numInputDoub = 0;
        do {
            System.out.print("Enter something: ");
            if (console.hasNextInt()) {
               int numI = console.nextInt();

            if (numI >= -100 && numI <= 100) {
               sumI += numI;
               numInputInt++;
            }
            else {
               System.out.println("Integer out of range!(-100 .. 100)");
            }

            }
         else if (console.hasNextDouble()) {
            double numD = console.nextDouble();
            if (numD >= -10.0 && numD <= 10.0) {
               sumD += numD;
               numInputDoub++;
            }
            else {
               System.out.println("Double out of range!(-10.0 .. 10.0)");
            }   

         }
         else {
            words = console.next();
         }

        } while (!words.equalsIgnoreCase("quit")); 
        System.out.println("Program terminated...");
      double avgInt = sumI / numInputInt;
      double avgDoub = sumD / numInputDoub;

      if (numInputInt > 0) {
         System.out.println("\tAveragae of Integers: " + avgInt);
      }
      else {
         System.out.println("\tNo intergers submitted");
      }
      if (numInputDoub > 0) {
         System.out.println("\tAverage of Doubles: " + avgDoub);
      }
      else {
         System.out.println("\tNo doubles submitted");
      }
      System.out.println(words);


   }

}

整数和双打得到很好的处理,但是我陷入了困境。关于如何去做的任何想法? 提前谢谢!

3 个答案:

答案 0 :(得分:0)

您可以使用

String input = "";
do {
    //...
    }
    else {
        input = console.next();            
        words += input + " ";
    }
    console.nextLine(); // Read the carriage return
} while (!input.equalsIgnoreCase("quit")); 
//...
System.out.println(words.trim());

连接文本,但在循环中完成时效率相当低。

更好的解决方案是使用StringBuilder ...

StringBuilder work = new StringBuilder(128);
String input = "";
do {
    //...
    }
    else {
        input = console.next();            
        words.append(" ").append(input);
    }
    console.nextLine(); // Read the carriage return
} while (!input.equalsIgnoreCase("quit")); 
//...
System.out.println(words);

答案 1 :(得分:0)

虽然您可以构建List<String>个单词,但您似乎只想将每个新单词连接到String words

if (words.length() > 0) words += " "; // <-- add a space.
words += console.next(); // <-- += not =

然后将循环测试更改为

while (!words.trim().toLowerCase().endsWith("quit"));

它应该像你期望的那样工作。

答案 2 :(得分:-1)

我刚看了你的评论,你不能使用任何其他课程,试试这个:

import java.util.*;

public class Lab09 {
    public static void main(String [] args) {
        Scanner console = new Scanner(System.in);

      double sumI = 0;
      double sumD = 0;
      String words = "";
      int numInputInt = 0;
      int numInputDoub = 0;
        do {
            System.out.print("Enter something: ");
            if (console.hasNextInt()) {
               int numI = console.nextInt();

            if (numI >= -100 && numI <= 100) {
               sumI += numI;
               numInputInt++;
            }
            else {
               System.out.println("Integer out of range!(-100 .. 100)");
            }

            }
         else if (console.hasNextDouble()) {
            double numD = console.nextDouble();
            if (numD >= -10.0 && numD <= 10.0) {
               sumD += numD;
               numInputDoub++;
            }
            else {
               System.out.println("Double out of range!(-10.0 .. 10.0)");
            }   

         }
         else {
            words = words.concat(" ").concat(console.next());

         }

        } while (!words.contains("quit")); 
        System.out.println("Program terminated...");
      double avgInt = sumI / numInputInt;
      double avgDoub = sumD / numInputDoub;

      if (numInputInt > 0) {
         System.out.println("\tAveragae of Integers: " + avgInt);
      }
      else {
         System.out.println("\tNo intergers submitted");
      }
      if (numInputDoub > 0) {
         System.out.println("\tAverage of Doubles: " + avgDoub);
      }
      else {
         System.out.println("\tNo doubles submitted");
      }
      System.out.println(words);


   }

}