输入前两个数字的总和

时间:2014-11-16 11:06:08

标签: java loops addition

我正在使用一个简单的初学者循环程序,我试图从用户那里获得一个整数输入并计算总和。我有一个简单的菜单,用户可以选择其中一个选项,第一个输入数字,第二个显示输入的最后两个数字的总和。所以我需要程序将前两个输入的数字加在一起。因此,如果用户选择选项1,他们可以输入一个数字然后返回到菜单,他们必须再次选择选项1以输入另一个。然后选项2应该计算总和并返回值。现在让我们说用户在此之后输入一个数字,最后两个数字应该相加。例如。

输入输出第二输入(仅适用于菜单选项1)
  1请输入0到20之间的数字:12   1请输入0到20之间的数字:16   2最后两个数字的总和是28.
  1请输入0到20之间的数字:15   2最后两个数字的总和是31.

但是当我添加数字时,程序会将用户编号添加到总和中。 我正在努力解决这个问题。我还认为我需要使用循环。

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);
    }
}

4 个答案:

答案 0 :(得分:0)

sum+=no;将累计所有输入数字的总和。您应该将输入的最后两个数字存储在两个变量中,并且只对这两个变量求和。

     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);

答案 1 :(得分:0)

你应该做出没有'一个array,其中包含2个数字。当总和添加这两个数字时,设置时数字会检查该数组中的数字数量,并取决于存储数字的位置。

另外,请记住注意到的秘密'选项5退出menu()

中的程序

这样的事情应该有效:

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);

让我知道它是否有效。
快乐编码:) -Charlie

答案 2 :(得分:0)

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.

答案 3 :(得分:0)

此代码应该有效:

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);
}

}