Question

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;

public class DoWhileDavidMungomaLab12 {

public static void main(String[] args) {

int aScore = -1;

do {

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

} while (aScore < 0 || aScore > 100);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//WhileDavidMungomaLab12.java
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();

        }


        aScore = -1;

        do {

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

        } while (aScore < 0 || aScore > 100);

    }

}

Output

Please enter a valid number that is within the specified range (0 and 100): Please enter a valid number that is within the sp

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!
Add a comment
Know the answer?
Add Answer to:
Please put both of this loops into one program. Set it up so that there is...
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
  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • What would be the output if 1, 2, 3, and -1 were entered and please walk...

    What would be the output if 1, 2, 3, and -1 were entered and please walk me through the process. import java.util.Scanner; public class Practice1 { public static void main(String[] args) { int count = 0; int sum = 0; Scanner keyboard = new Scanner(System.in); System.out.println("Enter integers or -1 to quit"); int anInt = keyboard.nextInt(); while(anInt != -1) { anInt = keyboard.nextInt(); sum += anInt; count++; } anInt = keyboard.nextInt(); sum += anInt; count++; } }

  • Please edit my following JAVA code so that there is no main function. I would like...

    Please edit my following JAVA code so that there is no main function. I would like one function in this class that completes what the current program is trying to do so that I can call this class in my main class which will be separate. import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class enterwklyincme { private static DecimalFormat df = new DecimalFormat("0.00"); public static float readFloat(Scanner reader) { float num; while (true) { try { num = Float.parseFloat(reader.nextLine()); return...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • Write a while loop that prints all positive numbers that are divisible by 10 and less...

    Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90. import java.util.Scanner; public class PositiveLess {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        System.out.print("n: ");        int n = in.nextInt();        ...        while (...)        {           ...

  • My program will not compile. Recieving an error message: ShoppingBagDemo.java:9: error: cannot find symbol ShoppingBag myBag...

    My program will not compile. Recieving an error message: ShoppingBagDemo.java:9: error: cannot find symbol ShoppingBag myBag = new ShoppingBag(6); ^ symbol: class ShoppingBag location: class ShoppingBagDemo ShoppingBagDemo.java:9: error: cannot find symbol ShoppingBag myBag = new ShoppingBag(6); ^ symbol: class ShoppingBag location: class ShoppingBagDemo 2 errors Here is my code. (Using jGrasp) import java.util.*; class ShoppingBagDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); float cost; ShoppingBag myBag = new ShoppingBag(6); System.out.print("Enter count (use 0 to stop):...

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
ADVERTISEMENT