Computer Applications
Write a program that inputs number of runs made by a cricket player on each ball. Entering runs as -1 should display the message that player is out. Finally it displays the number of runs made and balls played by the player.
Java
Java Iterative Stmts
ICSE
10 Likes
Answer
import java.util.Scanner;
public class KboatPlayerScore
{
public void computeScore() {
Scanner in = new Scanner(System.in);
int runs = 0, balls = 0, r = 0;
do {
System.out.print("Enter runs: ");
r = in.nextInt();
if (r == -1)
System.out.println("Player is out");
else
runs += r;
balls++;
}
while(r != -1);
System.out.println("Total Runs: " + runs);
System.out.println("Total Balls: " + balls);
}
}
Output
Answered By
3 Likes