Java While 循环问题(新)
Java While Loops Issue(new)
我的程序有问题。
我想在 while 循环中存储看到的鸟的数量,然后在程序终止时打印看到最多的鸟。
我对 if 语句有疑问。
任何帮助将不胜感激。
import java.util.*;
class gardenbird
{
public static void main(String[] main)
{
askbird();
System.exit(0);
}// END MAIN METHOD
public static void askbird()
{
final int sentinel = -1;
int mostseen = 0;
int howmany = 0;
print("When you want to end the program type in "+sentinel);
while(howmany != sentinel)
{ Scanner scanner = new Scanner(System.in);
print("Which bird have you seen?");
String bird = scanner.nextLine();
howmany = input("How many where in your garden at once?");
if(howmany>mostseen)
{
howmany = mostseen;
}
print("You saw " + howmany+ " " + bird +"\n It was the most common bird in your garden.");
}
}
public static String print(String message)
{
System.out.println(message);
return message;
}
public static int input(String count)
{
Scanner scanner = new Scanner(System.in);
print(count);
String number1=scanner.nextLine();
int number = Integer.parseInt(number1);
return number;
}
}
你的 if 语句的内容是反的,试试这个:
if(howmany > mostseen)
{
mostseen = howmany;
}
此外,
print("You saw " + mostseen + " " + bird +"\n It was the most common bird in your garden.");
大概应该到外面去吧?这样,您只会在用户终止时通知用户,而不是每次他们进行新输入时。您实际上并没有允许您跳出循环的设计,但这就是您的问题所述......或者,您可以将它放在 if 语句中,以便仅在条件为真时打印出来
正如其他人指出的那样,您的 if 块替换是倒退的。
创建实用程序方法来执行 System.out.println() 是过度封装。
一遍又一遍地创建对象会浪费系统资源并降低代码的可读性,但是您是在正确的轨道上。
对比一下这个
import java.util.Scanner;
public class GardenBird
{
public static void main(String[] main)
{
askbird();
System.exit(0);
}// END MAIN METHOD
public static void askbird()
{
Scanner scanner = new Scanner(System.in);
final int sentinel = -1;
int mostseen = 0;
int howmany = 0;
String mostSeenBird = "";
String currentBird = "";
System.out.println("When you want to end the program type in " + sentinel);
while (howmany != sentinel)
{
System.out.println("Which bird have you seen?");
currentBird = scanner.nextLine();
System.out.println("How many where in your garden at once?");
howmany = Integer.parseInt(scanner.nextLine());
if (howmany > mostseen)
{
mostseen = howmany;
mostSeenBird = currentBird;
}
}
System.out.println("You saw " + howmany + " " + mostSeenBird
+ "\n It was the most common bird in your garden.");
scanner.close();
}
}
我的程序有问题。
我想在 while 循环中存储看到的鸟的数量,然后在程序终止时打印看到最多的鸟。
我对 if 语句有疑问。
任何帮助将不胜感激。
import java.util.*;
class gardenbird
{
public static void main(String[] main)
{
askbird();
System.exit(0);
}// END MAIN METHOD
public static void askbird()
{
final int sentinel = -1;
int mostseen = 0;
int howmany = 0;
print("When you want to end the program type in "+sentinel);
while(howmany != sentinel)
{ Scanner scanner = new Scanner(System.in);
print("Which bird have you seen?");
String bird = scanner.nextLine();
howmany = input("How many where in your garden at once?");
if(howmany>mostseen)
{
howmany = mostseen;
}
print("You saw " + howmany+ " " + bird +"\n It was the most common bird in your garden.");
}
}
public static String print(String message)
{
System.out.println(message);
return message;
}
public static int input(String count)
{
Scanner scanner = new Scanner(System.in);
print(count);
String number1=scanner.nextLine();
int number = Integer.parseInt(number1);
return number;
}
}
你的 if 语句的内容是反的,试试这个:
if(howmany > mostseen)
{
mostseen = howmany;
}
此外,
print("You saw " + mostseen + " " + bird +"\n It was the most common bird in your garden.");
大概应该到外面去吧?这样,您只会在用户终止时通知用户,而不是每次他们进行新输入时。您实际上并没有允许您跳出循环的设计,但这就是您的问题所述......或者,您可以将它放在 if 语句中,以便仅在条件为真时打印出来
正如其他人指出的那样,您的 if 块替换是倒退的。
创建实用程序方法来执行 System.out.println() 是过度封装。
一遍又一遍地创建对象会浪费系统资源并降低代码的可读性,但是您是在正确的轨道上。
对比一下这个
import java.util.Scanner;
public class GardenBird
{
public static void main(String[] main)
{
askbird();
System.exit(0);
}// END MAIN METHOD
public static void askbird()
{
Scanner scanner = new Scanner(System.in);
final int sentinel = -1;
int mostseen = 0;
int howmany = 0;
String mostSeenBird = "";
String currentBird = "";
System.out.println("When you want to end the program type in " + sentinel);
while (howmany != sentinel)
{
System.out.println("Which bird have you seen?");
currentBird = scanner.nextLine();
System.out.println("How many where in your garden at once?");
howmany = Integer.parseInt(scanner.nextLine());
if (howmany > mostseen)
{
mostseen = howmany;
mostSeenBird = currentBird;
}
}
System.out.println("You saw " + howmany + " " + mostSeenBird
+ "\n It was the most common bird in your garden.");
scanner.close();
}
}