编写一个程序,要求用户输入数字 N 和选项 C

编写一个程序,要求用户输入数字 N 和选项 C。然后让他在计算 1 ,..., N 的和和乘积之间进行选择。

如果用户输入 C 等于

1 : 打印 1 到 N 个数字的总和

2 : 打印 1 到 N 号的产品

任何其他数字:打印 -1

输入格式:

第 1 行:整数 N

第 2 行:选项 C(1 或 2)

为此我没有得到它的确切结果。这段代码有什么问题?

import java.util.Scanner;


public class Sum_OrProduct {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();

        int choice = sc.nextInt();

        int sum = 0, prod = 1;

        for (int i = 1; i <= num; i++) {

            if (choice == 1) {

                sum = sum + i;

            } else if (choice == 2) {

                prod = prod * i;

            } else {

                System.out.println(-1);

            }

        }

        System.out.println(sum);

        System.out.println(prod);

    }


}

结果


Your Output

55

1

预期产出


55


MMMHUHU
浏览 180回答 3
3回答

绝地无双

而不是 if 和 else 语句来检查它是 for 循环内的总和还是产品没有给出正确的结果,所以首先如果用户想要做总和或产品被检查并基于该计算完成。&nbsp; &nbsp; int result =0;&nbsp; &nbsp; int mult=1;&nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp;&nbsp;&nbsp; &nbsp; System.out.println("Enter a number?");&nbsp; &nbsp; int a = sc.nextInt();&nbsp; &nbsp; System.out.println("Do you want to compute the sum or product?");&nbsp; &nbsp; //1 for sum, 2 for product&nbsp;&nbsp; &nbsp; int b = sc.nextInt();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(b==1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( int i=1; i<=a; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(b==2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=1; i<=a; i++) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mult = mult*i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mult);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Incorrect");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

梦里花落0921

导入 java.util.Scanner;公共课主要{public static void main(String[] args) {&nbsp; &nbsp; // Write your code here&nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp; &nbsp; int n=sc.nextInt();&nbsp; &nbsp; int c=sc.nextInt();&nbsp; &nbsp; if(c==1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int sum=0;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=1;i<=n;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum=sum+i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sum);&nbsp; &nbsp; }&nbsp; &nbsp; else if(c==2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int result=1;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=1;i<=n;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result=result*i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-1");&nbsp; &nbsp; }}}

慕勒3428872

首先,在逻辑(求和或乘积)之上使用 if 语句,在您决定执行哪个操作之后,然后在语句块中对逻辑进行编码,这样您就可以在不同的条件下将结果设置为变量,如下所示;&nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp; &nbsp; int num=sc.nextInt();&nbsp; &nbsp; int choice=sc.nextInt();&nbsp; &nbsp; int result;&nbsp; &nbsp; switch(choice)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; case 1: //sum&nbsp; &nbsp; &nbsp; &nbsp; result = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=1;i<=num;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case 2: //product&nbsp; &nbsp; &nbsp; &nbsp; result = 1;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=1; i<=num; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result *= i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; default: //invalid op&nbsp; &nbsp; &nbsp; &nbsp; result = -1;&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java