亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

編寫一個程序,要求用戶輸入數字 N 和選項 C

編寫一個程序,要求用戶輸入數字 N 和選項 C

MMMHUHU 2023-06-08 20:17:40
編寫一個程序,要求用戶輸入數字 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 Output551預期產出55
查看完整描述

3 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

而不是 if 和 else 語句來檢查它是 for 循環內的總和還是產品沒有給出正確的結果,所以首先如果用戶想要做總和或產品被檢查并基于該計算完成。


    int result =0;

    int mult=1;

    Scanner sc=new Scanner(System.in);  

    System.out.println("Enter a number?");

    int a = sc.nextInt();

    System.out.println("Do you want to compute the sum or product?");

    //1 for sum, 2 for product 

    int b = sc.nextInt();

    

            if(b==1){

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

                result += i;

                }

                System.out.println(result);

            }

            else if(b==2){

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

                    mult = mult*i;

                    }

                System.out.println(mult);

            }

            else {

                System.out.println("Incorrect");

            }


查看完整回答
反對 回復 2023-06-08
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

導入 java.util.Scanner;公共課主要{


public static void main(String[] args) {

    // Write your code here

    Scanner sc=new Scanner(System.in);

    int n=sc.nextInt();

    int c=sc.nextInt();

    if(c==1)

    {

        int sum=0;

        for(int i=1;i<=n;i++)

        {

            sum=sum+i;

        }

        System.out.println(sum);

    }

    else if(c==2)

    {

        int result=1;

        for(int i=1;i<=n;i++)

        {

            result=result*i;

        }

        System.out.println(result);            

    }

    else

    {

        System.out.println("-1");

    }


}

}


查看完整回答
反對 回復 2023-06-08
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

首先,在邏輯(求和或乘積)之上使用 if 語句,在您決定執行哪個操作之后,然后在語句塊中對邏輯進行編碼,這樣您就可以在不同的條件下將結果設置為變量,如下所示;


    Scanner sc=new Scanner(System.in);

    int num=sc.nextInt();

    int choice=sc.nextInt();

    int result;


    switch(choice) 

    {

      case 1: //sum

        result = 0;

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

          result += i;

        }

      break;


      case 2: //product

        result = 1;

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

          result *= i;

        }

      break;


      default: //invalid op

        result = -1;

      break;

    }

    System.out.println(result);


查看完整回答
反對 回復 2023-06-08
  • 3 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號