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

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

需要幫助在主類中添加方法

需要幫助在主類中添加方法

忽然笑 2023-08-23 17:14:59
你好,我正在為我的java類開發一個項目,我應該為代數導師編寫一段代碼,如下所示:編寫一個帶有 a 的程序,顯示一個隨機生成的問題,要求用戶解決 y 變量,接受用戶的輸入,如果用戶回答正確則打印“正確”,否則打印“錯誤”。你的 main 應該給出一個問題然后退出。使用一種或多種方法來產生這種行為。這是關于公式 mx + b 的。這就是我到目前為止所擁有的,并且有效!import java.util.Random;import java.lang.Math;import java.util.Scanner;class Main {    public static void main(String[] arg){        double min_value = -100;        double max_value = 100;        double m_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;        double x_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;        double b_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;        System.out.println("Given: ");        System.out.println("m = " + m_value);        System.out.println("x = " + x_value);        System.out.println("b = " + b_value);        System.out.print("What is the value of y? ");        Scanner user_input = new Scanner(System.in);        String user_answer = "";        user_answer = user_input.next();        int correct_answer = (int)m_value * (int)x_value + (int)b_value;        if (user_answer.equals(correct_answer))            System.out.println("You are correct!");        else            System.out.print("Sorry, that is incorrect. ");        System.out.println("The answer is " + correct_answer);    }}因此,即使輸出是正確的,我也需要將代碼分解為更小的方法,這就是我對如何獲取一段代碼并將其放入另一個方法中感到困惑的地方,一旦它運行,它也會調用該方法并給我相同的輸出。我已經準備好了給出的材料,但我讀得越多,我就越困惑。如果有人有任何想法或建議,請告訴我任何信息,我將非常感激。謝謝
查看完整描述

3 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

請查看此內容,您正在string與進行比較integer,


if (user_answer.equals(correct_answer))

這可能會幫助您:


import java.util.Scanner;


class Main {


    public static void main(String[] arg) {


        double min_value = -100;

        double max_value = 100;


        double m_value = generateRandom(max_value, min_value);

        double x_value = generateRandom(max_value, min_value);

        double b_value = generateRandom(max_value, min_value);


        System.out.println("Given: ");

        System.out.println("m = " + m_value);

        System.out.println("x = " + x_value);

        System.out.println("b = " + b_value);


        checkAnswer(m_value, x_value, b_value);         

    }


    private static void checkAnswer(double m_value, double x_value, double b_value) {


        System.out.print("What is the value of y? ");


        Scanner user_input = new Scanner(System.in);

        String user_answer = "";

        user_answer = user_input.next();


        int correct_answer = (int) m_value * (int) x_value + (int) b_value;


        if (user_answer.equals(String.valueOf(correct_answer))) {

            System.out.println("You are correct!");

        } else {

            System.out.print("Sorry, that is incorrect. ");

            System.out.println("The answer is " + correct_answer);

            user_input.close();

        }

    }

    static int generateRandom(double max_value, double min_value) {


        return (int) ((int) (Math.random() * ((max_value - min_value) 

+ 1)) + min_value);


    }

}


查看完整回答
反對 回復 2023-08-23
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

從技術上講,您已經正確解決了問題,您正在使用一種或多種方法,但也許您嘗試做的是一種稱為提取方法/提取函數重構的常見代碼重構,執行這種類型的重構會產生更具可讀性和可維護性的代碼,并且很容易做到。


作為初學者,請識別重復或看起來相似的代碼,在您的情況下,以下幾行看起來適合 extract 方法:


double m_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;

double x_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;

double b_value = (int)(Math.random()*((max_value-min_value)+1))+min_value;

請注意,每行的 RHS 是相同的,因此我們可以用如下方法調用替換顯式代碼:


double m_value = getRandomDoubleBetween(max_value, min_value);

double x_value = getRandomDoubleBetween(max_value, min_value);

double b_value = getRandomDoubleBetween(max_value, min_value);


private double getRandomDoubleBetween(double max_value, double min_value) {

     return (int)(Math.random()*((max_value-min_value)+1))+min_value;

 }

您可以識別代碼的其他區域,這些區域要么包含重復,要么可能包含一些難以理解的代碼,如果將其提取到一個具有揭示代碼正在做什么的名稱的方法中,這些代碼會更容易理解。


查看完整回答
反對 回復 2023-08-23
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

這是方法的快速概述,因此尚未完全完成。如果您需要更多幫助,請詢問!祝你作業順利,成為野獸開發者之一!


public class Main {


    public static void main(String[] args) {


    int a = 1; // give a value of 1


    methodTwo(a); // sending the int a into another method


    }


    // Here's method number two

    static void methodTwo (int a) { // it gives a's type and value


        System.out.println(a); //Gives out a's value, which is 1

    }


}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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