Write a program name numbers.java that repeatedly asks the user to input a numbe
ID: 639848 • Letter: W
Question
Write a program name numbers.java that repeatedly asks the user to input a number. After each number is inputted, the program displays how many numbers have been entered so far, the average of all the numbers that have been entered so far, and the largest number that has been entered so far. If the user enters done, the program should end, displaying the final message.
Write a program that you can tell knock-knock jokes to. To be more specific, here's what the program should do:
1. Display a message indicating that the program is ready to hear a joke.
2. Get a line of text from the user.
3. If the text doesn't contain the word knock (in any case), then print a message saying so and ask the user to start over (and go back to step 2).
4. If the message does contain the word knock, then print Who's there?
5. Get a line of text from the user.
6. Display the user's text followed by who?
7. Get a line of text from the user.
8. Laugh
3.
Explanation / Answer
/*
* Java rogram that
* repeatedly asks the user to input a number.
* After each number is inputted, the program displays
* how many numbers have been entered so far, the average
* of all the numbers that have been entered so far, and the
* largest number that has been entered so far. If the user enters done,
* the program should end, displaying the final message.*/
import java.util.Scanner;
public class Numbers
{
public static void main(String[] args)
{
//Scanner to read input
Scanner keyboard=new Scanner(System.in);
int sential=999;//to stop reading input;
int value;
double sum=0;
double average=0;
int count=0;
double largest=0;
System.out.println("Enter an input value or enter 999 to stop");
value=keyboard.nextInt();
//stop reading if the entered value is 999
while(value!=sential)
{
if(value>largest)
largest=value;
sum+=value;
count++;
average=sum/count;
System.out.println("Value :"+value);
System.out.println("Numbers entered :"+count);
System.out.println("Average :"+average);
System.out.println("Larget value: "+largest);
System.out.println("Enter an input value or enter 999 to stop");
value=keyboard.nextInt();
}
System.out.println("Program exited");
}
}
Sample op:
Enter an input value or enter 999 to stop
1
Value :1
Numbers entered :1
Average :1.0
Larget value: 1.0
Enter an input value or enter 999 to stop
2
Value :2
Numbers entered :2
Average :1.5
Larget value: 2.0
Enter an input value or enter 999 to stop
3
Value :3
Numbers entered :3
Average :2.0
Larget value: 3.0
Enter an input value or enter 999 to stop
4
Value :4
Numbers entered :4
Average :2.5
Larget value: 4.0
Enter an input value or enter 999 to stop
------------------------------------------------------------------
//KnockKnock.java
import java.util.Scanner;
public class KnockKnock
{
public static void main(String[] args)
{
//Scanner to read input
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter a line of joke");
//read a joke from the user
String joke=keyboard.nextLine();
//Read till the user enter the text contains knock or KNOCK
while(!(joke.contains("knock") ||joke.contains("KNOCK")))
{
System.out.println("Enter a line of joke");
//read a joke from the user
joke=keyboard.nextLine();
}
System.out.println("Who's is there ?");
//read a line of text from the user
String line=keyboard.nextLine();
//print the line
System.out.println("who ? "+line);
//Read the userText for response
String userText=keyboard.nextLine();
//print the userText followd by "Laugh"
System.out.println("Laugh"+ userText);
}
}
Sample op:
Enter a line of joke
Jack of all master of none
Enter a line of joke
A knock kick all kicks
Who's is there ?
Jhonson
who ? Jhonson
Mighe tell jokes
LaughMighe tell jokes
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.