In: Computer Science
The following Java code is set up to ask how many people are attending a meeting and checks these user generate responses with replies, using the do while setup. To end the loop you type 0, change this to accept the answer "Y" to continue the loop after every response and "N" to end the loop with every case type. (Y,y,N,n)
Meeting.java
------
import java.util.Scanner;
public class Meeting {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
final int ROOM_CAPACITY =
100;
int numPeople, diff;
String name;
System.out.println("****** Meeting
Organizer ******");
System.out.print("Enter your name:
");
name = input.nextLine();
System.out.println("Welcome " +
name);
do{
System.out.print("\nHow many people would attend the meeting? (type
0 to quit): ");
numPeople =
input.nextInt();
if(numPeople
< 0)
System.out.println("Invalid input!");
else
if(numPeople != 0)
{
if(numPeople > ROOM_CAPACITY)
{
diff = numPeople -
ROOM_CAPACITY;
System.out.println("Sorry!
The room can only accommodate " + ROOM_CAPACITY +" people.
");
System.out.println(diff + "
people have to drop off");
}
else if(numPeople < ROOM_CAPACITY)
{
diff = ROOM_CAPACITY -
numPeople;
System.out.println("The
meeting can take place. You may still invite " + diff + "
people");
}
else
{
System.out.println("The
meeting can take place. The room is full");
}
}
}while(numPeople != 0);
System.out.println("Goodbye!"); }
}
Program:
import java.util.Scanner;
public class Meeting {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int ROOM_CAPACITY = 100;
int numPeople, diff;
String name;
char c;
System.out.println("****** Meeting Organizer ******");
System.out.print("Enter your name: ");
name = input.nextLine();
System.out.println("Welcome " + name);
do{
System.out.print("\nHow many people would attend the meeting?:
");
numPeople = input.nextInt();
if(numPeople < 0)
System.out.println("Invalid input!");
else if(numPeople != 0)
{
if(numPeople > ROOM_CAPACITY)
{
diff = numPeople - ROOM_CAPACITY;
System.out.println("Sorry! The room can only accommodate " +
ROOM_CAPACITY +" people. ");
System.out.println(diff + " people have to drop off");
}
else if(numPeople < ROOM_CAPACITY)
{
diff = ROOM_CAPACITY - numPeople;
System.out.println("The meeting can take place. You may still
invite " + diff + " people");
}
else
{
System.out.println("The meeting can take place. The room is
full");
}
}
System.out.print("Are you want to continue? (Y/N): ");
c = input.next().charAt(0);
if(c=='N' || c=='n') break;
}while(c=='y' || c=='Y');
System.out.println("Goodbye!"); }
}
Output:
****** Meeting Organizer ******
Enter your name: abc
Welcome abc
How many people would attend the meeting?: 10
The meeting can take place. You may still invite 90 people
Are you want to continue? (Y/N): y
How many people would attend the meeting?: 20
The meeting can take place. You may still invite 80 people
Are you want to continue? (Y/N): y
How many people would attend the meeting?: 30
The meeting can take place. You may still invite 70 people
Are you want to continue? (Y/N): n
Goodbye!