Close
Close

Trying to learn Java using Codesdope. I am stuck on this question.:Take integer inputs from user until he/she presses q ( Ask to press q to quit after

   Amanda Davison

This is my code: 

I tried a while loop first, but now trying a do while loop.. I am so lost. please help, if you can give me the answer please explain, I am not looking for just an answer… I am looking to understand… Thank you...(I am very new to Java).

 

Scanner input = new Scanner(System.in);

String choice = "";
do{
    System.out.println("Please enter a number: ");
     int num = input.nextInt();
    System.out.println("Please enter another number or q to quit. ");
    choice = input.next();
    
int result = Integer.parseInt(choice);
    
int sum;

sum = num + result;


while(!choice.equals("q")) //to compare a STRING YOU NEED( "STRING NAME.EQUALS ("STRING"))// To get NOT is !(stringname).equals("string").
{
   System.out.println("You have quit. The sum of your numbers is : "+sum);

}
}
 

 

I was unable to get the sum to keep adding the new numbers so I changed it to a do while loop and now ot just crashes when I have no errors in netbeans..


Answers

  •   

    You want to get sum and product of all the integers and also the number of integers that you have for getting the average.

    So, first let’s define three variables:

    sum = 0

    product = 1

    count = 0

    After we get some number, we will add it to the sum and the sum will then become that number and we will continue to do so.

    e.g., number = 4 → we will add 4 to sum (0+4) and then the sum will become 4, again the number given is 2 → we will add 2 to sum (4+6) and then it will become 6 (it is 4 with previous addition).

    Similarly, we will get the product

    e.g., number = 4 → we will multiply 4 to the product (1*4) and then product will become 4, again the number given is 2 → we will multiply 2 to the product (4*2) and then it will become 8 (it is 4 with previous multiplication).

    So, we have a structure like this:

    int sum = 0;
    int product = 0;
    int count = 0;
    Scanner input = new Scanner(System.in);
    
    while(condition){
        if(!choice.equals("q")){
            int number = Integer.parseInt(choice);
    
            sum = sum+number;
            product = product*number;
            count++;
        }
    }
    

    Now, we want to do the handling part with q.

    So, it will look something like this:

    String choice = "";
    while(!choice.equals("q")){
    
        System.out.println("Enter q to quit or any number to continue");
        choice = input.next();
    }
    

    So, this will take input from the user and it it is q, then the value of the choice will become q and while loop will stop.

    So, the whole code is something like this:

    import java.util.*;
    
    class Test {
    public static void main(String[] args) {
    
    String choice = "";
    
    int sum = 0;
    int product = 1;
    int count = 0;
    Scanner input = new Scanner(System.in);
    
    while(!choice.equals("q")){
        System.out.println("Enter a number or q to quit");
        choice = input.next();
    
        if(!choice.equals("q")){
            int number = Integer.parseInt(choice);
            sum = sum+number;
            product = product*number;
            count++;
        }
    } 
    
    System.out.println("Product is: "+product+"\nAverage is: "+((float)sum/count));
    
    }
    }
    

     



  •    seoexpertim

    A lot of my friends have been visiting this site to get content for essays. I just got to know about teamcanadawomenslacrosse.com and am already loving the concept of the site. It is very attractive.



  •    lunaterra

    Thanks for the great tutorials you shared on the forum  bitlife 



  •    bilon83361

    Internet Download Manager Crack https://free4pc.cc/ is a reliable and very useful tool with safe multipart downloading technology to accelerate from internet your downloads ... 



  •    seoexpertim

    I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work rolvi.mysignup.biz



  •    fareed123a

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. assume world



  •    theodoreevans

    Your point of view is as clear as a blind spot on an awning. It's a lot to take in. I want to see what you write next five nights at freddy's



  •    thiagogo

    Excellent piece. Reading your blog postings has been a delightful surprise for me, and I wanted to let you know that. For what it's worth, I've subscribed to your feed and will be checking back for future posts. Sincerely appreciate the helpful details. If free time play game Fnaf and Fnaf 8 with me



  •    among1

    Playing games is fun. With the challenge that it brings, fall guys 3 is a game that people of all ages can enjoy. Try to beat the other people. To win, you have to compete in crowded areas and get through chapters with obstacles.



  •    bettyking

    I'm glad to see that you have a unique way of writing the post. Now it's easy for me to understand the idea and put it into practice  io games



Ask Yours
Post Yours
Write your answer