• Register
1 vote
242 views

Problem:

Can anyone provide me an expression that executes the loop body as long as the user enters a non-negative number.

Thanks in advance.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Answer :

The program that executes the loop body as long as the user enters a non-negative number is:

import java.util.Scanner;

public class NonNegativeLooper

     public static void main (String [] args)

    

          Scanner scnr = new Scanner(System.in);

          //initialize the userNum with 8

          int userNum = 8;

          //Repeat the loop until the userNum is not negative

          while (userNum>=0)

         

              System.out.println(Body);

              //Prompt the user to enter the userNum again

              userNum = scnr.nextInt();

         

          System.out.println(Done);

          return;

Hope this helps you the most.

Happy coding

11 6 4
34,950 points

Related questions

0 votes
1 answer 53 views
53 views
Problem: Hi there! I am working with While loop in C++ and I want to write a program that continues to bid unless the user enters ‘n’ or ‘N’. I understand the while loop but unable to make the logic. Please provide me the code with detailed comments. Thank You!
asked Feb 25 Code Learner 9.9k points
0 votes
1 answer 3 views
3 views
Problem: I am stuck in this program that is string method, my issue is that I cannot get the loop to stop and the program to print the output that is currently stored after the keyword has been entered. I am not trying to compare strings, I am trying to input multiple strings and add a word, in ... nextLine(); s = s + "not "; if(s == "stop"){ System.out.println(s); break; } else{ continue; } } } }
asked May 1 ochena 23.3k points
0 votes
1 answer 219 views
219 views
Solutions: I am new in programming and my logic is not good, can anyone give the right solution? Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"?
asked Feb 14, 2020 maddi86 5.4k points
0 votes
1 answer 50 views
50 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 9.9k points
0 votes
1 answer 119 views
119 views
Problem: Can anyone help it was actually asked in exam as following and I need to know whether my answer was right or not? Write an expression that whose value is the fifth character of the String name?
asked Feb 24, 2020 maddi86 5.4k points
0 votes
1 answer 69 views
69 views
Problem: I am new to programming, I need help, can anyone help me? Write an expression that evaluates to True if and only if s refers to the str "end"?
asked Feb 18, 2020 maddi86 5.4k points
0 votes
1 answer 229 views
229 views
Problem: I am new in programming and stuck in a query, can anyone guide me? Write an expression that evaluates to True if the str associated with s starts with "p"?
asked Feb 17, 2020 maddi86 5.4k points
0 votes
1 answer 207 views
207 views
Problem: Write an expression whose value is the str that consists of the third to last character of the str associated with s I tried s[2:] didn't work. Can you please guide me with right solution?
asked Feb 19, 2020 maddi86 5.4k points
0 votes
1 answer 7 views
7 views
Problem: Have you any suggestions how i can find prime number with javascript language?
asked Apr 18 Supti 13k points