我有一个程序,我需要用户在每次访问时询问他们是否要继续并增加总金额

时间:2019-02-15 03:13:45

标签: java

编写一个Java程序进行一系列求和。它是 认为项间差异(即共同差异)为 总是1.

示例:如果系列的第一项和最后一项是5和10 那么系列分别是:5、6、7、8、9、10。

要完成此任务,您应该了解以下java 编程主题:

  • 从键盘即Java IO接收输入
  • Java循环。

用户规范:用户将提供两个整数,即第一项和最后一项 学期。

约束:第一学期应介于1到100之间,而最后一项应在 必须大于第一项。

  • 程序将仅显示序列的总和。
  • 最后,将要求用户继续序列和。

最终输出样本

这是一个简单的系列求和问题。用你的名字准备

Enter first term between 1 to 100: 4
Enter last term: 4
Error: Enter an integer greater than 4: 1
Error: Enter an integer greater than 4: 6
SUM = 15
Do you like to do again?
(Press 'y' for 'Yes' and 'n' for 'No') y
Enter first term between 1 to 100: 1
Enter last term: 10
SUM = 55
Do you like to do again?
(Press 'y' for 'Yes' and 'n' for 'No') n

package lab3;
import java.util.Scanner;
public class JavaMain {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String userInput = null;
        int term = 0;
        int lastTerm = 0;
        int sum = 0;
System.out.println("It is a simple series sum problem. Prepared by -------");
        System.out.println("Enter first term between 1 to 100:");
        term = input.nextInt();
        while (term < 0 || term > 100) {
            System.err.print("Error: Enter the first term between 1 to 100");
            term = input.nextInt();


        }
        System.out.println("Enter last term:");
        lastTerm = input.nextInt();
        while (lastTerm <= term) {
            System.err.print("Error: Enter an integer greater than " +  term);
            lastTerm = input.nextInt();
        }
        for (int i = term+1; i<lastTerm; i++) {
            sum += term + lastTerm + i;
            System.out.println("Sum:" + sum);
        }

        while (true) {
            System.out.println("Do you like to do again");
            userInput = input.next();
            if (userInput.equalsIgnoreCase("Y"));

            else if (userInput.equalsIgnoreCase("N"))
            {
                break;
            }
        }
    }
}

Need to be able to repeat the questions add to get that total of 55.

在此感谢您的帮助,到目前为止,它已经可以正常工作了

Enter first term between 1 to 100:
4
Enter last term:
6
Sum:4
Sum:9
Sum:15
Do you like to do again
y
It is a simple series sum problem. Prepared by -------
Enter first term between 1 to 100:
1
Enter last term:
10
Sum:16
Sum:18
Sum:21
Sum:25
Sum:30
Sum:36
Sum:43
Sum:51
Sum:60
Sum:70
Do you like to do again

1 个答案:

答案 0 :(得分:0)

OP的问题是这样。它位于代码下方。由于OP的问题描述包含在代码片段中,因此问题需要格式化。

  

需要能够重复添加的问题,使总数达到55。

String userInput = null;
        int term = 0;
        int lastTerm = 0;
        int sum = 0;
System.out.println("It is a simple series sum problem. Prepared by -------");
        System.out.println("Enter first term between 1 to 100:");
        term = input.nextInt();
        while (term < 0 || term > 100) {
            System.err.print("Error: Enter the first term between 1 to 100");
            term = input.nextInt();


        }
        System.out.println("Enter last term:");
        lastTerm = input.nextInt();
        while (lastTerm <= term) {
            System.err.print("Error: Enter an integer greater than " +  term);
            lastTerm = input.nextInt();
        }
        // Edit start
        for (int i = term; i<=lastTerm; i++) {
            sum += i;
        }
        System.out.println("Sum:" + sum);
        // Edit end

将上述代码放入while循环中。

        while (true) {
            // Place it here
            System.out.println("Do you like to do again");
            Boolean willContiue = false;
            while (true){
                userInput = input.next();
                if (userInput.equalsIgnoreCase("Y"));
                    willContiue = true;
                    break;
                else if (userInput.equalsIgnoreCase("N"))
                {
                    break;
                }
            }
            if (!willContinue) {
                break;
            }
        }

通过进行以上修改,您现在将使用户再次输入他的选择。由于在您的代码中,您只是在询问用户他/她是否愿意再次执行该操作,并且如果用户回答“ Y”,则您的代码不会执行任何操作。因此,这个问题不再重复。