Question

In: Computer Science

I am trying to figure out how to properly execute a while loop and a do/while...

I am trying to figure out how to properly execute a while loop and a do/while loop in the same program using java. I am stuck at just writing the while loop. I made a condition but I can't figure out how to get it to loop if the condition is true?? Also I have to write a do/while loop in the same program.

here are the details:

  • This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms.
  • You will be using each of these loops to solve the same problem.
  • Please put them both in the same program.
  • When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code.
  • You will want to write a refined algorithm for both to see the logic differences

Here is what I have written in JAVA so far:

import java.util.Scanner;


public class whileloopanddowhile //BEGIN Class Definition (must be same as file name)
{
public static void main (String[] args) //BEGIN Main Method
{
//Declare variables
int number;

//List all Class Objects here
Scanner scan = new Scanner(System.in);
  
//Executable Statement Section, user input
System.out.println ("Enter a grade from 0 to 100 ");
number = scan.nextInt();
  
//This is where the code goes
while (number < 0 && number > 100 )
{   
System.out.println ("ERROR! Must enter a grade between 0 and 100");
}

System.out.println ("You have enetered a valid grade");
  


}//END main method

}

Thank you for any help and clarification!

Solutions

Expert Solution

import java.util.Scanner;

public class Whileloopanddowhile {

    public static void main(String[] args) //BEGIN Main Method
    {
        //Declare variables
        int number;

        //List all Class Objects here
        Scanner scan = new Scanner(System.in);

        //Executable Statement Section, user input
        System.out.println("Enter a grade from 0 to 100 ");
        number = scan.nextInt();

        //This is where the code goes
        while (number < 0 || number > 100) {
            // Incorrect user input 
            System.out.println("ERROR! Must enter a grade between 0 and 100");
            
            // Get input from user again
            System.out.println("Enter a grade from 0 to 100 ");
            number = scan.nextInt();            
        }

        System.out.println("You have entered a valid grade");

        // do while loop
        do 
        {
            // Check if the grade is invalid
            if(number < 0 || number > 100)
               System.out.println("ERROR! Must enter a grade between 0 and 100");
            
            // Get input from user
            System.out.println("Enter a grade from 0 to 100 ");
            number = scan.nextInt();
        }
        while(number < 0 || number > 100);
        
        System.out.println("You have entered a valid grade");
        
    }//END main method
}



*************************************OUTPUT***************************************************


Enter a grade from 0 to 100 
-36
ERROR! Must enter a grade between 0 and 100
Enter a grade from 0 to 100 
150
ERROR! Must enter a grade between 0 and 100
Enter a grade from 0 to 100 
35
You have entered a valid grade
Enter a grade from 0 to 100 
-12
ERROR! Must enter a grade between 0 and 100
Enter a grade from 0 to 100 
150
ERROR! Must enter a grade between 0 and 100
Enter a grade from 0 to 100 
25
You have entered a valid grade

Related Solutions

This is a java program I am trying to figure out how to add a couple...
This is a java program I am trying to figure out how to add a couple methods to a program I am working on. I need to be able to: 1. Remove elements from a binary tree 2. Print them in breadth first order Any help would appreciated. //start BinaryTree class /** * * @author Reilly * @param <E> */ public class BinaryTree { TreeNode root = null; TreeNode current, parent; private int size = 0, counter = 0; public...
I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
I am trying to figure out the social security tax for the employees in this problem,...
I am trying to figure out the social security tax for the employees in this problem, becuase I have to post it all in a chart. I have tried multiplying by the 6.2 tax rate but my program says my answer is wrong. So, is there a different way to calculate social security tax or is there something that I am missing that the problem is asking me to do? I am not looking for answers, but just a clear...
I am trying to figure out the best way to solving a problem in the language...
I am trying to figure out the best way to solving a problem in the language python. I have some but have no clue if I am even going in the right direction. Here are the instructions: Write a program that calculates the shopping list for a birthday party with the minimum amount of leftovers. The program should ask the user for the number of kids attending the party. Assume each kid will cook (but not necessarily eat) 2 hot...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings Entrust, Inc., is a global provider of security software; it operates in one business segment involving the design, production, and sale of software products for securing digital identities and information. The consolidated statements of operations for a three-year period (all values in thousands) follows. On January 1, Year 1, the Entrust common shares traded at $10.40 per share; by year end Year 3, the...
I am trying to figure out the probability, expected value, variance, and standard deviation for a...
I am trying to figure out the probability, expected value, variance, and standard deviation for a series of dice rolls. For example, if I roll a six-sided die in an attempt to roll a 1, and it takes me 7 rolls before a 1 appears, what are those answers? I have figured out the probability equation: P(P-1)^x where x is the number of rolls - 1 so for 7 rolls the probability would be: 1/6(1-1/6)^6 = 0.05581632... Further where I...
I am trying to figure out which test analysis to use for my research questions. I...
I am trying to figure out which test analysis to use for my research questions. I was thinking about think about multivariate because of the number of variable being addressed in the study but there is also the possibility to use univariate to address each question. What are the current levels of police satisfaction in CMPD jurisdictions? What is the public’s perception of crime in CMPD jurisdictions? Does “hot spot” policing reduce crime in CMPD jurisdictions? How does broken windows...
I am having problems trying to figure out what information to pull to prepare budgets
I am having problems trying to figure out what information to pull to prepare budgets
Hi! I am trying to compare 2 files, and figure out which lines from infile2 are...
Hi! I am trying to compare 2 files, and figure out which lines from infile2 are also in infile1, and output the line index, from infile2, of common lines. I would also want to see which lines (actual string line) are common. Below is my c++ program, and I am not sure what I am doing wrong. Can someone tell me what I am doing wrong and how it can be fixed? _________________________________________________________main.cpp #include<iostream> #include<fstream> #include<vector> #include<string> using namespace std;...
How is this done in R-studio? I have been trying to figure it out but I...
How is this done in R-studio? I have been trying to figure it out but I am getting more and more confused. While imprisoned by the Germans during World War II, the English mathematician John Kerrich tossed a coin 10,000 times and obtained 5067 heads. Let p be the probability of a head on a single toss. We wish to check if the data are consistent with the hypothesis that the coin was fair. a) Set up the hypotheses. Why...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT