Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int number, sum= 0, count;
for (count = 0; count < 5; count++){
number= input.nextInt();
sum += number;
}
System.out.println("sum is "+ sum);
System.out.println("count is " + count);
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Computer Science: An Overview (12th Edition)
- Please help me fix the code Make it looks like the expected import java.util.Scanner; public class NumberLoops { public static void main(String[] args) { double num; double result = 1; int i; Scanner sc = new Scanner(System.in); // Create a Scanner object // Take user input for the Positive Number System.out.print("Enter a positive integer: "); num = sc.nextDouble(); // Read user input // if the input number is not an integer if(num % 1 != 0) { // print the message System.out.println("Not an Integer: " + num); System.exit(0); } // if the number is not a positive number if(numarrow_forwardThe following code segment has an error. Find the error and suggest a correction.arrow_forwardcomplete the code belowarrow_forward
- import java.util.Scanner; public class CharMatch { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userString; char charToFind; int strIndex; userString = scnr.nextLine(); charToFind = scnr.next().charAt(0); strIndex = scnr.nextInt(); /* Your code goes here */ }}arrow_forwardimport java.util.Scanner; public class DebugSix3 { public static final int TIME_WASTER = 500; public static void main(String[] args) throws InterruptedException { Scanner input = new Scanner(System.in); System.out.print("Enter a number between 1 and 20 >> "); int num = input.nextInt(); if (num < 1 || num > 20) { System.out.println("Number out of range!"); return; } for (int i = num; i > 0; i--) { System.out.print(i + " "); Thread.sleep(TIME_WASTER); } System.out.println("Blastoff!"); } } The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. An example of the program is shown below: Enter a number between 1 and 20 >> 5 5 4 3 2 1 Blastoff! Depending on the value assigned to the…arrow_forwardStringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forward
- import java.util.Scanner; public class OutputWithVars { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userNum; int userA; int userB; System.out.println("Enter integer:"); userNum = scnr.nextInt(); userA = userNum * userNum; userB = userNum * userNum * userNum; System.out.printf("You have entered %d", userNum); System.out.printf("\n%d squared is %d", userNum, userA); System.out.printf("\nAnd %d cubed is %d", userNum, userB); System.out.println("\nEnter another integer:"); int user = scnr.nextInt(); System.out.printf("%d", user) Purpose: I am trying to stop and let the user input another value but my result is Enter integer: You have entered 2 2 squared is 4 And 2 cubed is 8 Enter another integer: meaning my first part worked but... Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at…arrow_forwardimport java.util.Scanner; public class CircleAndSphereWhileLoop{ public static final double MAX_RADIUS = 500.0; public static void main(String[] args) { Scanner in = new Scanner(System.in); // Step 2: Read a double value as radius using prompt // "Enter the radius (between 0.0 and 500.0, exclusive): " // Step 3: While the input radius is not in the ragne (0.0, 500.0) // Display a message on one line (ssuming input value -1) // "The input number -1.00 is out of range." // Read a double value as radius using the same promt double circumference = 2 * Math.PI * radius; double area = Math.PI * radius * radius; double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); double volume = (4 / 3.0) * Math.PI * Math.pow(radius, 3); // Step 4: Display the radius, circle circumference, circle area, // sphere surface area, and…arrow_forwardimport java.util.Scanner; public class leapYearLab { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userYear; boolean LeapYear; System.out.println("Enter the year"); userYear = scnr.nextInt(); isLeapYear(userYear); //if leap year if (isLeapYear){ System.out.println(userYear + " - leap year"); } else{ System.out.println(userYear + " - not a leap year"); } scnr.close(); } public static boolean isLeapYear(int userYear){ boolean LeapYear; /* Type your code here. */ if ( userYear % 4 == 0) { // checking if year is divisible by 100 if ( userYear % 100 == 0) { // checking if year is divisible by 400 // then it is a leap year if ( userYear % 400 == 0) LeapYear = true; else LeapYear = false; } // if the year is not divisible by 100 else…arrow_forward
- import java.util.Scanner; public class RunBloodData { public static void main(String[] args) { Scanner SC = new Scanner(System.in); String b_Type,r_Factor; System.out.print("Enter blood type of patient: "); b_Type=SC.nextLine(); System.out.print("Enter the Rhesus factor (+ or -): "); r_Factor=SC.nextLine(); BloodData b_data; if(b_Type.equals("")&& r_Factor.equals("")) { b_data=new BloodData(); b_data.display(); } else { b_data=new BloodData(b_Type,r_Factor); b_data.display(); } } } class BloodData{ static String bloodType; static String rhFactor; public BloodData() { bloodType="O"; rhFactor="+"; } public BloodData(String b_Type,String r_Factor) { bloodType=b_Type; rhFactor=r_Factor; } public void display(){ System.out.println(bloodType+rhFactor+" is added to the bloodbank.\n"); } }arrow_forwardFirst, you will debug the Paint1 class so that no errors remain and all calculations work correctly by completing the following: o Find and fix the three errors in the given code. • Use the following sample input values to test your program. If you have found and fixed all the errors, your output should exactly match the sample. Input: 30 25 Output: Wall area: 750.0 square feet Paint needed: 2.142857142857143 gallons Tip: When you run your program in Eclipse, it will prompt you to enter input in the Console window. If you cannot find this window, go to Window, then Show View, then Console. Next, you will add loops to validate all user input and handle exceptions so that code passes all test cases by completing the following: • Review the code, looking for the two code blocks where user input is required. • Implement a do-while loop in both blocks of code to ensure that input is valid and any exceptions are handled. • Use the two following sample sets of input to test your program.…arrow_forwardPlease determine errors and correct it. package test; import java.util.Scanner; public class Test { public static void main(String[] args) {String fname,lname; int hrsworked; Double payrate,income,fnfp,paye,fiji,medical=6.50,total_deduction,netpay; Scanner sc = new Scanner(System.in); System.out.print("\nEnter Employee First Name: "); fname = sc.nextLine(); System.out.print("Enter Employee Last Name: "); lname = sc.nextLine(); System.out.println("Enter Employee Hours Worked: "); hrsworked = sc.nextInt(); System.out.print("Enter Employee Pay Rate: "); payrate = sc.nextDouble(); income = hrsworked * payrate; fnfp = income*0.085; paye = income*0.065; fiji = income*0.0285; total_deduction = hrsworked + fnfp + paye + fiji +medical; netpay = income-total_deduction; System.out.printf("\n*-------------* Pay Slip for: %s *-------------*\n",(fname+" "+lname)); System.out.printf("Fortnightly Income: $ %.2f\n",income); System.out.printf("Hours Worked: \n", hrsworked); System.out.printf("FNPF: $…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education