简体   繁体   中英

can I print something inside of the round brackets in while loop with the condition?

I was playing with this code...

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());
    }
}

The output should prompt for a number then process it and print as Result.

and then again with another input if that's a integer(long)

but the condition in while loop scan.hasnextLong takes input itself and process it and the output is like this-

Your Value: Your Result: n

but the expected output is like this-

Your Value: m

Your Result: n

as like the first time. tobe does not seems taking input after first time. its just using what give in loop condition to check if thats a integer . more clearly the program prompt before printing Your Value: and use this value in tobe .

So i wanted to print inside of while loop condion

somewhere in this line with condition inside the round brackets

while(scan.hasNextLong())

so it print and prompt like I wanted;

You're triyng to print a value before initializing it. If you use print instead of println the next output will be on the same line.

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());
        }

I changed your code so the scanner gets a String input.
The while condition then checks if it contains only numbers. It does that by calling .matches() on the input, which tries to match the input string with the Regular Expression \\d+ . This expression matches a series of one or more digits. \\d is for a digit, + is a quantifier. You can learn about and play with Regular Expressions on sites like 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();

Replace while(scan.hasNextLong()); with while(scan.hasNextLine());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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