Java 方法,循环和随机,哦,天哪
我在学校有一项任务,要创建一个至少有三种不同方法的程序,就像一个“魔法八球”。我已经创建了一个方法,将10个“responses”设置为一个“switch”语句,基于一个单独的方法生成一个介于0和9之间的随机数。我需要的是一个“for”、“while”或“do-while”循环方法,它应该继续提问,直到“exit”一词作为问题输入。然后我需要一种方法将所有这些方法结合在一起,这样它们才能正常工作。按照教授的指示,我使用了一个名为BlueJ的程序 到目前为止,我得到的是:Java 方法,循环和随机,哦,天哪,java,loops,methods,Java,Loops,Methods,我在学校有一项任务,要创建一个至少有三种不同方法的程序,就像一个“魔法八球”。我已经创建了一个方法,将10个“responses”设置为一个“switch”语句,基于一个单独的方法生成一个介于0和9之间的随机数。我需要的是一个“for”、“while”或“do-while”循环方法,它应该继续提问,直到“exit”一词作为问题输入。然后我需要一种方法将所有这些方法结合在一起,这样它们才能正常工作。按照教授的指示,我使用了一个名为BlueJ的程序 到目前为止,我得到的是: import java.
import java.util.*;
public class MagicEightBall
{
//Input method
public static void main(String[] args)
{
startAsking();
}
//Loop method
public static void startAsking()
{
do
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Question: ");
System.out.println("Entering 'exit' ends program ");
if(input.nextLine().equalsIgnoreCase("exit"))
break;
System.out.println(getResponse());
}
while(true);
//input.close();
}
//Output method
public static String getResponse()
{
int numberOfResponses = 10;
int response = (int)(Math.random() * numberOfResponses);
String responseString;
switch (response)
{
case 1: System.out.println("Of course! H-A-L said so");
case 2: System.out.println("Yes, my young Padawan!");
case 3: System.out.println("V-ger has informed me that your answer is 'Yes'");
case 4: System.out.println("Mr. Spock says 'Not a chance, Captain'");
case 5: System.out.println("Only when Pi = apple would that be a 'Yes'");
case 6: System.out.println("There is no try, only do, or do not");
case 7: System.out.println("You know 'Big Brother' heard you ask that question?");
case 8: System.out.println("SyStEm MaLfUnCtIoN! pLeAsE tRy l8r");
case 9: System.out.println("No. That would cause a food fight");
default: System.out.println("I'm sorry, it's time for my oil bath");
}
return responseString;
}
}
对于如何完成此任务的任何帮助,我们将不胜感激。我将构建您的代码,然后运行它; 阅读关于你要问什么的内容,似乎什么都做,但不是100%正确,以下步骤: 我问你一个问题 2随机回答你 3如果被要求退出,他将停止程序 你能更好地解释一下你的计划中遗漏了什么吗?现在不行吗 现在,我可以说,您使用变量来追加问题,而不是使用变量来从循环中执行go put,在getresponse方法中也不需要某些东西
import java.util.*;
public class NewClass
{
//Input method
public static void main(String[] args)
{
startAsking();
}
//Loop method
public static void startAsking()
{
do
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Question: ");
System.out.println("Entering 'exit' ends program ");
if(input.nextLine().equalsIgnoreCase("exit"))
break;
getResponse();
}
while(true);
//input.close();
}
//Output method
public static void getResponse()
{
int numberOfResponses = 10;
int response = (int)(Math.random() * numberOfResponses);
System.out.println(response);
switch (response)
{
case 1: System.out.println("Of course! H-A-L said so");break;
case 2: System.out.println("Yes, my young Padawan!");break;
case 3: System.out.println("V-ger has informed me that your answer is 'Yes'");break;
case 4: System.out.println("Mr. Spock says 'Not a chance, Captain'");break;
case 5: System.out.println("Only when Pi = apple would that be a 'Yes'");break;
case 6: System.out.println("There is no try, only do, or do not");break;
case 7: System.out.println("You know 'Big Brother' heard you ask that question?");break;
case 8: System.out.println("SyStEm MaLfUnCtIoN! pLeAsE tRy l8r");break;
case 9: System.out.println("No. That would cause a food fight");break;
default: System.out.println("I'm sorry, it's time for my oil bath");
}
}
}
不关心我为控制循环添加的一些输出您介绍了阵列了吗?这不是一个很好的解决方案。我们有,但能力有限。我现在所拥有的还远没有完成,主要是我把东西放在里面,希望我能从中得到启示,知道如何完成它。我不反对使用数组,但我对它们知之甚少。如果你仔细考虑一下这个开关,你会发现它可以很容易地映射到一个响应字符串数组,这样你就可以用生成的数字立即检索响应。虽然我很欣赏这些重复,但恐怕它们还是有点让我难以理解。我们阅读了有关数组的内容,但不允许将它们实现到我们的程序中,因此我对它们没有任何接触,只知道它们可以包含一些稍后可以调用的内容。我也在关注一篇关于这里的文章——似乎使用本页上的帮助创建了一个无限循环。即使尝试更改他的方法名称以匹配我的方法,它仍然不起作用。看看扰流器,如果你自己无法识别错误