Sum of previous two numbers entered

二次信任 提交于 2019-12-06 12:41:29

问题


I am working a simple beginner loop program, where i am trying to get an integer input from the user and calculate the sum. I have a simple menu in which the user can select one of for options, the first being inputting a number and second showing the sum of the last two numbers entered. So i need the program to add the previous two entered numbers together. so if the user selects option 1, they can enter a number and then be returned to the menu where they have to select option 1 again to enter another. option 2 should then calculate the sum and return the value.now lets say the user enters a number after this, the last two numbers should be summed.. Eg.

Input Output 2nd input (only for menu option 1)
1 Please enter a number between 0 and 20: 12
1 Please enter a number between 0 and 20: 16
2 The sum of the last two numbers is 28.
1 Please enter a number between 0 and 20: 15
2 The sum of the last two numbers is 31.

But when i add the numbers, the program adds on the user number to the sum. I am struggling to get around this. I also think i need to use a loop.

import java.lang.*;
import java.util.*;
import java.text.*;

class test {

    public static void menu() {
        System.out.print(" Select one of the option below\n" +
                "   1 -     Enter a new number\n " +
                "   2 - Show the sum of the last two number\n" +
                "   3 - Show the current number as pluses\n" +
                "   4 - Show the current number as centred pluses\n");
    }

    public static void main(String[] args) {
        int no = 0;
        int sum = 0;
        int option;

        Scanner input = new Scanner(System.in);

        do {
            menu();
            option = input.nextInt();
            switch (option) {
                case 1:
                    System.out.print("Please enter a number between 0 and 20 : ");
                    no = input.nextInt();
                    break;
                case 2:
                    sum += no;
                    System.out.println("The Sum of the Numbers is : " + sum);
                default:
                    System.out.print("Invalid option");
            }
        } while (option != 5);
    }
}

回答1:


sum+=no; will accumulate the sum of all the numbers ever entered. You should store the last two numbers entered in two variables, and only sum those two.

     int last = 0;
     int beforeLast = 0;
     do {
         menu();
         option=input.nextInt();

         switch (option) {

         case 1:

            System.out.print("Please enter a number between 0 and 20 : "  ); 
            no=input.nextInt();
            beforeLast = last;
            last = no;
            break;
         case  2:
            System.out.println("The Sum of the Numbers is : " + (last+beforeLast));

         default :
             System.out.print("Invalid option");


         } 
    } while (option !=5);



回答2:


You should make 'no' an array with 2 numbers in it. When making the sum add those 2 numbers, when settings the numbers check how many numbers are in that array and depending on that where to store the number.

Also, remember to notice the 'secret' option 5 to exit the program in menu()

Something like this should work:

int[] no=new int[2];

...

case 1:

    System.out.print("Please enter a number between 0 and 20 : "  ); 
    int inNo=input.nextInt();
    no[0]=no[1];
    no[1]=inNo;
    break;
case  2:
    sum=no[0]+no[1];
    System.out.println("The Sum of the Numbers is : " + sum);

Let me know if it works.
Happy coding :) -Charlie




回答3:


case 2:
    sum += no;
    System.out.println("The Sum of the Numbers is : " + sum);
    sum = no;

assigning the value of second input in the sum variable after printing.



回答4:


This code should work:

import java.util.Scanner;
public class test {
    public static void menu() {
        System.out.print("Select one of the option below\n" +
            "   1 - Enter a new number\n" +
            "   2 - Show the sum of the last two number\n" +
            "   3 - Show the current number as pluses\n" +
            "   4 - Show the current number as centred pluses\n");
            //I cleaned this up a little bit so it looks a little better
}

public static void main(String[] args) {
    int no1 = 0; //since we are only dealing with the last two digits, we don't need an array but two different number variables
    int no2 = 0;
    int sum = 0;
    int count1 = 0; // counts the number of times case 1 is executed
    int option;

    Scanner input = new Scanner(System.in);

    do {
        menu();
        option = input.nextInt();
        switch (option) {
            case 1:
                count1 += 1; //counts the number of case 1 executed
                System.out.print("Please enter a number between 0 and 20 : ");
                if (count1 % 2 == 0) //if count1 is even, the input will be stored in no1...
                    no1 = input.nextInt();
                else
                    no2 = input.nextInt();//...else it will be stored in no2
                sum = no1 + no2; // this is the sum of the last two numbers entered
                break;
            case 2:
                System.out.println("The Sum of the Numbers is : " + sum);
                break; // don;t forget to break the case so the default won't be reached if option = 2
            default:
                System.out.print("Invalid option");
        }
    } while (option != 5);
}

}



来源:https://stackoverflow.com/questions/26956242/sum-of-previous-two-numbers-entered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!