【问题标题】:can I print something inside of the round brackets in while loop with the condition?我可以使用条件在while循环中的圆括号内打印一些东西吗?
【发布时间】:2018-03-17 07:07:20
【问题描述】:

我在玩这个代码...

import java.util.Scanner;

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

        do{
            System.out.print("Youe Value: ");
            long tobe = scan.nextLong();

            int total = 0;

            String parsed = String.valueOf(tobe);

            for(int i = 0; i < parsed.length(); i++) {
                total += Character.getNumericValue(parsed.charAt(i));
            }

            System.out.println("Your Result: "+ total);
        }
        while(scan.hasNextLong());
    }
}

输出应提示输入一个数字,然后对其进行处理并打印为结果。

如果是integer(long),则再次输入另一个输入

但是while循环scan.hasnextLong中的条件自己接受输入并处理它,输出是这样的-

您的价值:您的结果:n

但预期的输出是这样的-

你的价值:米

您的结果:n

就像第一次一样。 tobe 在第一次之后似乎没有接受输入。它只是使用循环条件中的给定来检查那是否是integer。在打印 Your Value: 之前更清楚程序提示,并在 tobe 中使用此值。

所以我想在 while 循环条件内打印

在这一行的某处,圆括号内的条件

while(scan.hasNextLong())

所以它会像我想要的那样打印和提示;

【问题讨论】:

    标签: java loops while-loop conditional-statements


    【解决方案1】:

    我更改了您的代码,以便扫描仪获得String 输入。
    while 条件然后检查它是否只包含数字。它通过在输入上调用.matches() 来做到这一点,它尝试将输入字符串与正则表达式\d+ 匹配。此表达式匹配一系列一个或多个数字。 \d 是一个数字,+ 是一个量词。您可以在RegExr 等网站上了解和使用正则表达式。

        Scanner scan = new Scanner(System.in);
        String input;
    
        System.out.print("Your Value: ");
    
        while ((input = scan.nextLine()).matches("\\d+")){
    
            int total = 0;
    
            for(int i = 0; i < input.length(); i++) {
                total += Character.getNumericValue(input.charAt(i));
            }
    
            System.out.println("Your Result: "+ total);
            System.out.println("\nYour Value: ");
        }
    
        System.out.println("\nTHE END.");
        scan.close();
    

    【讨论】:

    • so .matches("\\d+") 检查字符串是否只包含数字?如果是这样,我明白了。但我也想在没有条件的情况下打印出来。你能解释一下matches方法是如何工作的吗?清楚地。为什么 \\d+ 等等等等。
    • @Rabin 我添加了对.matches() 方法的解释。不幸的是,我不明白你为什么要在条件内打印一些东西。我仍然希望这会有所帮助。
    【解决方案2】:

    您正在尝试在初始化之前打印一个值。如果您使用print 而不是println,则下一个输出将在同一行。

    public static void main(String[] args)
            {
    
              Scanner scan = new Scanner(System.in);
    
                do{
    
                    long tobe = scan.nextLong();
                    System.out.println("Your Value: " + tobe);
    
                    int total = 0;
    
                    String parsed = String.valueOf(tobe);
    
                    for(int i = 0; i < parsed.length(); i++) {
                        total += Character.getNumericValue(parsed.charAt(i));
                    }
    
                    System.out.println("Your Result: "+ total);
                }
                while(scan.hasNextLong());
            }
    

    【讨论】:

      【解决方案3】:

      while(scan.hasNextLong()); 替换为while(scan.hasNextLine());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 2023-04-03
        • 2015-09-08
        相关资源
        最近更新 更多