Question

What would be the output if 1, 2, 3, and -1 were entered and please walk...

What would be the output if 1, 2, 3, and -1 were entered and please walk me through the process.

import java.util.Scanner;

public class Practice1

{

public static void main(String[] args)

{

int count = 0;

int sum = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter integers or -1 to quit");

int anInt = keyboard.nextInt();

while(anInt != -1)

{

anInt = keyboard.nextInt();

sum += anInt;

count++;

}

anInt = keyboard.nextInt();

sum += anInt;

count++;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

when you enter 1,2, 3 -1 what happens is first anInt outside while loop becomes 1.

Then anInt is not equal to 1 , execution starts inside while loop

first iteration:

Inside while loop , anInt is again read from keyoboard , now and integer value of 2 is read from keyboard

sum=sum+anInt which is sum=0+2=2

Count is incremented , it becomes 1 , then while loop checks in anItnt value is not equal to -1 , it's not equal hence while loop evaluates to true and hence again execution starts inside while loop .

second iteration:

Here again anInt is read from keyboard , when takes the value of 3 from keyboard

sum+=anInt, here sum has stored 2 from previous execution. hence sum becomes 2+3 = 5

Now again while loop checks if anInt value is -1 , hence this also evaluates to true as anInt value is now 3

Third iteration:

So execution once again start inside the loop

anInt value will be read from keyboard which reads -1 from the keyboard and now sum=sum+abInt = 5-1=4

sum values evaluates to 4.

Now while loop check if anInt value is -1 , this time it's true , hence execution comes out of while loop .

After while loop , again anInt is read from keyboard , so it waits for user input , so final sum value is dependent on the anInt value you enter + previous sum value (we got 4 from previous execution from while loop).

Output will be addition of 4 and the user input .

======================================

if  you want to sum up all the integers till user enters -1 ..above program is not perfect , I am giving you code if you want to sum up all the integers till you hit -1.

import java.util.Scanner;

class Main {

public static void main(String[] args) {

//System.out.println("Hello world!");

int count = 0;

int sum = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter integers or -1 to quit");

int anInt;

do

{

anInt = keyboard.nextInt();

if(anInt == -1)

break;

sum+=anInt;

++count;

}while(true);

System.out.println("Number of reads : "+count+" , sum of integers: "+sum);

}

}

======================

//if you want output of sum of the integers entered until -1 , then use my above program. output is given below

Enter integers or -1 to quit
1 2 3 -1
Number of reads : 3 , sum of integers: 6

=========================

//pls give positive rating , if my solution helped you,thanks!

Add a comment
Know the answer?
Add Answer to:
What would be the output if 1, 2, 3, and -1 were entered and please walk...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

  • 8. What is the output of this program if the input: 1 2 3 4 -5...

    8. What is the output of this program if the input: 1 2 3 4 -5 0 26? importjava.util.Scanner; public class Q_09 Your answer: public static void main(String[] args) int n, neg_count = 0, sum = 0; Scanner input = new Scanner(System.in); System.out.println("Enter n:"); n = input.nextInt(); while(n !=0) neg_count++; sum +=n; System.out.println("sum =" + sum); System.out.println("#of negatives:" + neg_count + "\t" +"Total = "+sum);

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Take the following code and add a joption pane to make it eaiser to read. Can...

    Take the following code and add a joption pane to make it eaiser to read. Can anyone tell me whats wrong with my code? It wont compile JAVA. import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; class que { public static void main(String args[]) { int count=0,min=0,hr=0; String a; Scanner inp=new Scanner(System.in); Queue q=new LinkedList<>(); while(count<10) { System.out.println("Enter your name:"); a=inp.nextLine(); q.add(a); count=count+1; min=min+5; if(min>=60) { hr=hr+1; } System.out.println("You are "+count+" in the queue"); System.out.println("Average wait time is "+hr+" hours and "+min+"...

  • Complete the do-while loop to output 0 to countLimit. Assume the user will only input a...

    Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT