Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/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_String - Fatal编程技术网

Java 我如何计算我有多少个字符串并将它们连接在一起?

Java 我如何计算我有多少个字符串并将它们连接在一起?,java,string,Java,String,我的程序要求用户提供输入列表,这些输入可以是字符串、整数或双精度。一旦用户完成输入列表,他们输入“退出”完成。(即用户输入:1 2.3退出) 一旦他们输入“quit”,我的程序就会把整数和double相加,打印出这些值,它还会告诉你列表中有多少整数/double 但现在我还想解释字符串输入(除了int或double作为字符串计数之外的任何内容)。如果我得到一些字符串值,我想计算有多少个字符串,并将它们连接起来,最后也打印出来 我在考虑使用“else”语句,因为它可以解释除int和double之外

我的程序要求用户提供输入列表,这些输入可以是字符串、整数或双精度。一旦用户完成输入列表,他们输入“退出”完成。(即用户输入:1 2.3退出)

一旦他们输入“quit”,我的程序就会把整数和double相加,打印出这些值,它还会告诉你列表中有多少整数/double

但现在我还想解释字符串输入(除了int或double作为字符串计数之外的任何内容)。如果我得到一些字符串值,我想计算有多少个字符串,并将它们连接起来,最后也打印出来

我在考虑使用“else”语句,因为它可以解释除int和double之外的所有内容,但是我现在的代码如何做到这一点呢?有没有办法让用户不必输入“退出”来完成列表?我能让它在用户按下“回车”键后继续运行吗?提前谢谢

import java.util.Scanner;

public class InputParser
{
   public static void main(String[] args)
   {
      Scanner scanner = new Scanner(System.in);
      System.out.println("How many values do you want to input?: ");
      int numValues = scanner.nextInt();
      System.out.println("Enter " + numValues + " values: ");
      int sumIntegers = 0;
      int countIntegers = 0;
      double sumDoubles = 0.0;
      int countDoubles = 0;

      while (scanner.hasNextLine())
      {
         if (scanner.hasNextInt())
         {
            countIntegers++;
            sumIntegers += scanner.nextInt();
         }
         else if (scanner.hasNextDouble())
         {
            countDoubles++;
            sumDoubles += scanner.nextDouble();
         }
         else
         {
            String str = scanner.next();
            if (str.equalsIgnoreCase("quit"))
            {
               break;
            }
         }
      }
      System.out.println("Number of integers: " + countIntegers);
      System.out.println("Sum of integers: " + sumIntegers);
      System.out.println("Number of doubles: " + countDoubles);
      System.out.println("Sum of doubles: " + sumDoubles);
   }
}

因为在下面的代码中,您从用户处读取字符串类型

  else
     {
        String str = scanner.next();
        if (str.equalsIgnoreCase("quit"))
        {
           break;
        }
     }
您需要有一个计数器来计算用户迄今为止输入了多少字符串

 int numberOfString = 0;

如果不想保存它们,请将它们连接在一起

 String result = "";

  else
     {
        String str = scanner.next();
        numberOfString++;
        result += str+" "; <-- to make a blank so you can separate between each String
        if (str.equalsIgnoreCase("quit"))
        {
           break;
        }
     }

,因此,无需检查用户键入的值是否正确。:)

好的,您不需要将值的数量作为输入。因为有一个输入结束标记“退出”

无论如何,修复代码以处理所有字符串并生成串联结果非常简单

import java.util.Scanner;

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

        System.out.println("Enter  values: ");
        int sumIntegers = 0;
        int countIntegers = 0;
        double sumDoubles = 0.0;
        int countDoubles = 0;
        StringBuilder sumString = new StringBuilder();
        int countStrings = 0;

        while (scanner.hasNextLine())
        {
            if (scanner.hasNextInt())
            {
                countIntegers++;
                sumIntegers += scanner.nextInt();
            }
            else if (scanner.hasNextDouble())
            {
                countDoubles++;
                sumDoubles += scanner.nextDouble();
            }
            else
            {
                String str = scanner.next();
                if (str.equalsIgnoreCase("quit"))
                {
                    break;
                }
                else {
                    ++countStrings;
                    sumString.append( str );
                }
            }
        }
        System.out.println("Number of integers: " + countIntegers);
        System.out.println("Sum of integers: " + sumIntegers);
        System.out.println("Number of doubles: " + countDoubles);
        System.out.println("Sum of doubles: " + sumDoubles);
        System.out.println("Number of strings: " + countStrings);
        System.out.println("concatenated string: " + sumString.toString());

        scanner.close();
    }
}

请不要忘记关闭扫描仪:否则,将出现警告

最后,你的程序结束了,这行吗。。。。我相信这会带来一个例外。在使用nextDouble和next之后,您可能会遇到问题。你会遇到一个问题。@Karen如果你不想完全打字,我建议你一种方法approach@KickButtowski:如果我使用nextDouble和NextAfter,为什么会出现问题?谢谢您的解释!
 int i = 0;
 while( i < numValues){

  i++;
  }
  System.out.println("How many values do you want to input?: ");
  int numValues = scanner.nextInt();
import java.util.Scanner;

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

        System.out.println("Enter  values: ");
        int sumIntegers = 0;
        int countIntegers = 0;
        double sumDoubles = 0.0;
        int countDoubles = 0;
        StringBuilder sumString = new StringBuilder();
        int countStrings = 0;

        while (scanner.hasNextLine())
        {
            if (scanner.hasNextInt())
            {
                countIntegers++;
                sumIntegers += scanner.nextInt();
            }
            else if (scanner.hasNextDouble())
            {
                countDoubles++;
                sumDoubles += scanner.nextDouble();
            }
            else
            {
                String str = scanner.next();
                if (str.equalsIgnoreCase("quit"))
                {
                    break;
                }
                else {
                    ++countStrings;
                    sumString.append( str );
                }
            }
        }
        System.out.println("Number of integers: " + countIntegers);
        System.out.println("Sum of integers: " + sumIntegers);
        System.out.println("Number of doubles: " + countDoubles);
        System.out.println("Sum of doubles: " + sumDoubles);
        System.out.println("Number of strings: " + countStrings);
        System.out.println("concatenated string: " + sumString.toString());

        scanner.close();
    }
}