0

我被困在下面代码 3 的解决方案上。我需要插入一个简单的数学问题,在翻阅我的书和课堂上的示例视频后,我终生无法弄清楚这一点。我想让程序问一个问题,“8 的 2 次幂的答案是什么”,答案是“64”。有人愿意帮助我吗?如果有人能让我开始,我可以提出另外两个问题!非常感谢!!金

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
4

2 回答 2

2

当您运行程序时,mainresponseE将始终将其设置为"Definite"。因为:

查看您的代码流程:

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

第一个if总是满足的,所以它总是会被执行。

此外,即使您responseE在阅读分数后进行评估,您也需要再次考虑如何编写您的条件。请注意,如果score <= 85那么score <= 70...。

你应该有这样的东西:

切换前:

responseE = getResponse(score);

这是方法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";
}

阅读输入要评估的其他字段也是如此。

于 2013-03-11T18:16:27.020 回答
0

我需要更多信息才能给你答案。看起来代码需要一个随机数生成器,但是在您的问题中您要求 8^2 或 8*8。你想要哪个?我问是因为随机数生成与硬编码数字变量有很大不同

于 2013-03-11T18:14:15.763 回答