gpt4 book ai didi

Java 代码简单数学

转载 作者:行者123 更新时间:2023-12-02 07:12:24 25 4
gpt4 key购买 nike

我被下面代码 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

最佳答案

当您运行程序时,在main中你将会拥有responseE将始终设置为“确定”。因为:

查看代码流程:

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

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

此外,即使您评估 responseE读完分数后,您需要再次考虑如何编写条件。注意 if 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";
}

对于您读取输入之后想要评估的其他字段也是如此。

关于Java 代码简单数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com