简体   繁体   English

Java代码简单数学

[英]Java Code Simple Math

I am stuck on the solution for code 3 below. 我坚持下面的代码3的解决方案。 I need to insert a simple math problem and I cannot for the life figure this out after going over my book and sample video from class. 我需要插入一个简单的数学问题,在翻阅我的书并从课堂上观看视频之后我无法想到这一点。 I would like the program to ask the question, "What is the answer to 8 raised to the power of 2" with the answer "64". 我希望程序能够提出这样一个问题:“8的强大之处是什么?”答案为“64”。 Anyone willing to help me out? 有谁愿意帮帮我吗? I can come up with my other two questions if someone can just get me started! 如果有人能让我开始,我可以提出另外两个问题! Thank you very much!! 非常感谢你!! Kim

import java.util.Scanner;  //allows for input

public class ASG03 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); //allows for input

        //Step 1 - Declare and initialize variables
        String candidateName = "";
        String responseE = "";

        int option = 0;
        double score = 0;


        if (score <=85)
            responseE = "Definite";
        else if (score <=70)
            responseE = "Likely";
        else if (score <=60)
            responseE = "Maybe";
        else
            responseE = "No";

        String responseI = "";

        if (score <=85)
            responseI = "Yes";
        else if (score <=70)
            responseI = "Yes";
        else if (score <=60)
            responseI = "Yes";
        else
            responseI = "No";

        //Step 2 -  Process input

        System.out.println("Enter candidate name: ");
        candidateName = input.nextLine();
        System.out.println("Enter score 0 -100: ");
        score = input.nextDouble();
        System.out.println();



        System.out.println("Enter 1 to set employment category ");
        System.out.println("Enter 2 to set interview possibility ");
        System.out.println("Enter 3 to view a sample test question ");
        System.out.println("Enter option now -> ");
        option = input.nextInt();





        //Step 3 and 4 - Process calculations and output
        switch(option)
        {
        case 1:
            System.out.println("You are now setting the employment category...");
            //can use nested if else
            System.out.println("Employment category =  " + responseE);

            break;


        case 2:
            System.out.println("You are now setting the interview possibilities...");
            System.out.println("Interview possibilites = " + responseI);



            break;

        case 3:
            System.out.println("You are now viewing a sample test question...");
            //use random and power from Math library


        default:


        }//end of switch

    }//end of main

}//end of class

When you run your program, in the main you'll have that responseE will always be set to "Definite" . 当您运行程序时,在main您将始终将responseE设置为“Definite” Because: 因为:

Look at the flow of your code: 查看代码流:

double score = 0;
if (score <=85)
  responseE = "Definite";
else if (score <=70)
...
...

the first if is always satisfied, so it'll always be executed. 第一个if总是满足,所以它总是被执行。

Furthermore, even when you'll evaluate responseE after you read the score, you need to consider again how you write your conditions.. note that if score <= 85 then score <= 70 .... 此外,即使您在阅读得分后评估responseE score <= 85 ,您还需要再次考虑如何编写条件。请注意,如果score <= 85score <= 70 ....

You should have something like this: 你应该有这样的东西:

Before the switch : 切换之前:

responseE = getResponse(score);

And here is the method getResponse : 这是方法getResponse

private static String getResponse(double score) {
  if (score <=85 && score >70)
    return "Definite";
  else if (score <=70 && score > 60)
    return "Likely";
  else if (score <=60 && score > 40) //For example..
    return "Maybe";
  return "No";
}

The same for the other fields you want to evaluate after you read the input. 读取输入要评估的其他字段也是如此。

I need a little more information before I can give you an answer. 在我给你答案之前,我需要更多的信息。 It looks like the code wants a random number generator, however in your question you asked for 8^2, or 8*8. 看起来代码想要一个随机数生成器,但是在你的问题中你要求8 ^ 2或8 * 8。 Which would you like? 你想要哪个? I ask because random number generation is a lot different than hard coding number variables 我问,因为随机数生成与硬编码数变量有很大不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM