Question

How would I edit this Java program to end if the user inputs 0 or less?...

How would I edit this Java program to end if the user inputs 0 or less?

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // to read input
String Message;
int m=0;
int i=1;

System.out.print("Please enter the message you would like displayed: ");

Message=sc.nextLine();

while(true)
{
System.out.print("How many times would you like your message displayed?");

m = sc.nextInt();

break;
}
do
{
System.out.println(Message); //When I enter 0 or a negative number it still runs at least once, how do I get it to end without submitting the message if they enter 0 or less?
i++;
}while(i<=m);

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static void main(String[] args)
{
  Scanner sc = new Scanner(System.in); // to read input
  String Message;
  int m=0;
  int i=1;

  System.out.print("Please enter the message you would like displayed: ");

  Message=sc.nextLine();

  while(true)
  {
    System.out.print("How many times would you like your message displayed?");

    m = sc.nextInt();

    break;
  }
  while (i<=m)
  {
    System.out.println(Message); //When I enter 0 or a negative number it still runs at least once, how do I get it to end without submitting the message if they enter 0 or less?
    i++;
  }
}

Add a comment
Know the answer?
Add Answer to:
How would I edit this Java program to end if the user inputs 0 or less?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • I was wanting to know how would I call my add class through my if statement....

    I was wanting to know how would I call my add class through my if statement. Here is my class: import java.util.Scanner; public class Main {    public static void main(String[] args)    {        int num1 = 0;        int num2 = 0;        int answer = 0;        int userInput = 0;        Scanner sc = new Scanner(System.in);                      System.out.println("Hello, this is a program where you can choose...

  • I am gettting error saying that I need to initialize variable but I want to get...

    I am gettting error saying that I need to initialize variable but I want to get inputs from the user and want to print them out. (using "for" loop) public static void main(String[] args) { String input; int i;    Scanner sc = new Scanner(System.in); for (i=1; i < 5; i++) { System.out.print(i+ ". Please enter a name: "); input = sc.nextLine(); } System.out.println("All names are here:" + input); } }

  • Hi I would like to revise the codes below to bring all text files I have...

    Hi I would like to revise the codes below to bring all text files I have text file 0 from 10 I will give you link here https://drive.google.com/open?id=1LnWqv8ftzARx5Rhf7HtVlVkLVlcA1FtI when tokenizer read all tokens from the 10 files which attached above, I would like to arrange it in alphabetical order and want to make same word tokens being merged. For instance, You are my son? You are my son and good friend! you are the only one whom I loved. The...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT