Java exercises and solutions: while loop


while loop

Exercise 1: Write Java program to prompt the user to choose the correct answer from a list of answer choices of a question.

The user can choose to continue answering the question or stop answering it. See the example below:

What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit

Enter your choice: b

Incorrect!

Again? press y to continue:

Solution:


import java.io.*;

public class JavaExercises
{
public static void main(String[] args)
{
selectChoice();
}

static void selectChoice(){

String choice;
String con="y";
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the command keyword to exit a loop in Java?");
System.out.println("a.quit");
System.out.println("b.continue");
System.out.println("c.break");
System.out.println("d.exit");


while (con.compareTo("y")==0)
{

System.out.print("Enter your choice:");
choice =br.readLine();

if (choice.compareTo("c")==0)
{
System.out.println("Congratulation!");
}
else if (choice.compareTo("q")==0 || choice.compareTo("e")==0) {  System.out.println("Exiting...!"); break; }
else System.out.println("Incorrect!");

System.out.print("Again? press y to continue:");
con =br.readLine();
}

}catch(IOException e){}


  }

}

Exercise 2:Write Java program to print the table of characters that are equivalent to the Ascii codes from 1 to 122.

The program will print the 10 characters per line.

Solution:


public class JavaExercises
{
public static void main(String[] args)
{
selectCharacters();
}

static void printCharacters(){

int i =1;
while (i <=122)
{
System.out.print((char)i+"\t");
if (i % 10 == 0)
System.out.print("");
i++;
}


 }

}




Comments

Abeer comment

 Abeer

Help me please to do my homework please ....

Write a Java program to find the average of the students grades knowing that the number of the students and the number of student courses shall be given by the user .
(4 Points)

Notes: The Number of students should be more than zero , and the student grades should be between 0 - 100

2. Assign letter grades for the student average based on the distribution below:
(2 Points)

A​: 90 – 100
B​: 80 – 89
C​: 70 – 79
D​: 60 – 69
F​: 0 – 59

3. Count and print the number of the students who got an A. (2 Points)
4. Print on a file “out.txt” the class average and the number of students who got A.
(2 Points)


2017-04-18
Abeer comment

 Abeer

Very good it was nice


2017-04-18
maya comment

 maya

ex2
don't you mean 'static void selectCharacters()'

selectCharacters();
}

static void printCharacters(){


2015-12-06
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.