如何让Java接受一些用户输入并将信息传递给一些方法?
我试图用Java编写一个main方法,它接受一些用户输入并将其传递给一些方法 到目前为止,我的代码是:如何让Java接受一些用户输入并将信息传递给一些方法?,java,Java,我试图用Java编写一个main方法,它接受一些用户输入并将其传递给一些方法 到目前为止,我的代码是: //this method prints a menu to the console public static void menu(){ System.out.println("Select one of the following:\n"); System.out.println("Enter Observatory Data[1]"); System.out.pr
//this method prints a menu to the console
public static void menu(){
System.out.println("Select one of the following:\n");
System.out.println("Enter Observatory Data[1]");
System.out.println("Enter Earthquake Data[2]");
System.out.println("Get Largest Ever Earthquake[3]");
System.out.println("Get All Earthquakes Greater Than X[4]");
System.out.println("Exit[5]");
}
public static void main(String[] args){
Scanner reader= new Scanner(System.in);
menu(); //print the menu to screen
String input1=reader.next(); //user should select 1,2,3,4 or 5
boolean cheese=false; //keep the program running
while (cheese=false){
if (input1.matches("[a-zA-Z67890]*")){ //if anything but 1,2,3,4,or 5 is entered return this string and reprint the menu
//int inputNum1=Integer.parseInt(input1);
System.out.println("no work!");
menu();
String input2=reader.next();
}else if(input1.matches("1")){ //if user types 1 accept some information and pass it to the Observatory method
System.out.println("What is the Observatory name?");
String observatoryName = reader.next();
System.out.println("What country is the observatory in?");
String country = reader.next();
System.out.println("What year did the observatory open??");
String year = reader.next();
System.out.println("Observatory added. Waht next?");
System.out.println("What area does the observatory cover?");
String area = reader.next();
Observatory newObservatory = new Observatory(observatoryName,country,Integer.parseInt(year),Double.parseDouble(area));
也有一些其他的选择,但这里只粘贴一个就足够了。当前代码运行、打印菜单并接受一些用户输入,但一旦执行,程序即终止,尽管存在
布尔奶酪
剩余false
。有人建议我如何让Java运行直到输入选项5,并在选择1时检索一些信息吗 在while循环条件中,将cheese设置为false,而不是将cheese与false进行比较。换成
while (cheese == false) {
或
您还可以在上次实际输入后等待更多用户输入。因此,在最后一行之后,添加以下内容:
reader.next();
以便程序在退出之前等待进一步的用户输入。如果我正确理解了您的程序,您可以这样做,删除while循环,并达到预期效果。在while循环条件下,您将cheese设置为false,而不是将cheese与false进行比较。换成
while (cheese == false) {
或
您还可以在上次实际输入后等待更多用户输入。因此,在最后一行之后,添加以下内容:
reader.next();
以便程序在退出之前等待进一步的用户输入。如果我正确理解了您的程序,您可以这样做,删除while循环,并达到预期效果。您的while条件不正确。。。 应该是:
boolean cheese = true;
while (cheese)
{
// Do stuff.
}
或者,如果你真的想让你的cheese布尔值为false,只需执行以下操作:
boolean cheese = false;
while (!cheese)
{
// Do stuff.
}
您的while条件不正确。。。 应该是:
boolean cheese = true;
while (cheese)
{
// Do stuff.
}
或者,如果你真的想让你的cheese布尔值为false,只需执行以下操作:
boolean cheese = false;
while (!cheese)
{
// Do stuff.
}
虽然(cheese=false)应该是while(cheese==false)并不是你问题的真正答案,但使用switch语句可能比使用一堆“if/else”更容易,我觉得默认情况可能非常有用。事实上,它应该是
而(!cheese)
正如前面所说的,问题可能在(cheese=false)附近但是,如果不看你在哪里下奶酪,就很难知道其余的是否正确。啊,当然。我和我的方式阻碍了我。谢谢你们!虽然(cheese=false)应该是while(cheese==false)并不是你问题的真正答案,但使用switch语句可能比使用一堆“if/else”更容易,我觉得默认情况可能非常有用。事实上,它应该是而(!cheese)
正如前面所说的,问题可能在(cheese=false)附近但是,如果不看你在哪里下奶酪,就很难知道其余的是否正确。啊,当然。我和我的方式阻碍了我。谢谢你们!如果没有看到更多的代码,我不能投票认为你已经回答了这个问题。啊,当然。我和我的方式阻碍了我。谢谢你们!如果没有看到更多的代码,我不能投票认为你已经回答了这个问题。啊,当然。我和我的方式阻碍了我。谢谢你们!