Java 输出将不会打印正确的信息
我正在尝试制作一个程序,询问用户一只特定的鸟,然后询问他们在那个时候见过多少只鸟。如果在任何时候使用输入“结束”这个词,那么系统应该打印出看到最多的鸟和看到的数字。但是,在运行程序时,如果在随机点输入“END”,则返回的结果是看到的次数最多的是END,看到的次数为0。我不知道该怎么做。我尝试过不同的方法,但效果不太好。另外,我已经将最大数组限制设置为10个positions,但它在10个positions之后继续,如果我输入一个值,系统将崩溃。我把限制部分写对了吗?还是我错过了一些重要的事情Java 输出将不会打印正确的信息,java,Java,我正在尝试制作一个程序,询问用户一只特定的鸟,然后询问他们在那个时候见过多少只鸟。如果在任何时候使用输入“结束”这个词,那么系统应该打印出看到最多的鸟和看到的数字。但是,在运行程序时,如果在随机点输入“END”,则返回的结果是看到的次数最多的是END,看到的次数为0。我不知道该怎么做。我尝试过不同的方法,但效果不太好。另外,我已经将最大数组限制设置为10个positions,但它在10个positions之后继续,如果我输入一个值,系统将崩溃。我把限制部分写对了吗?还是我错过了一些重要的事情 i
import java.util.Scanner;
public class testing
{
public static void main (String[] param)
{
birdInput();
most();
System.exit(0);
}
public static void birdInput()
{
int i = 0;
String birdInput;
int numberInput;
Scanner scanner = new Scanner(System.in);
int maxVal = Integer.MIN_VALUE;
int maxValIndex = -1;
while (true)
{
System.out.println("What bird did you see?");
birdInput = scanner.nextLine();
if (birdInput.equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.exit(0);
}
else
{
String[] birds = new String[10];
int[] numbers = new int[10];
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
if (birds[i].equals("END"))
{
maxVal = numbers[i];
maxValIndex = i;
System.out.print("\nThe most common bird that you saw was the " + birds[maxValIndex] + " with " + maxVal + " being seen in total\n");
System.exit(0);
}
}
}
}
public static void most()
{
System.out.println("fdff");
}
}
这是我编辑的Till Hemmerich对我问题的回答。我试图删除全局变量,因此将整个代码合并到一个方法中。然而,我仍然有一些问题。我一直在努力,但真的很困惑
import java.util.Scanner;
public class birds2
{
public static void main(String[] param)
{
birdInput();
System.exit(0);
}
public static void birdInput()
{
Scanner scanner = new Scanner(System.in);
String[] birds = new String[99999999];
int[] numbers = new int[99999999];
int i = 0;
int maxIndex;
while (i <= birds.length)
{
System.out.println("What bird did you see?");
birds[i] = scanner.nextLine();
System.out.println("How many did you see?");
numbers[i] = scanner.nextInt();
i++;
}
int newnumber = numbers[i];
if ((newnumber > numbers.length))
{
maxIndex = i;
i++;
}
if (birds[i].toUpperCase().equals("END"))
{
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
}
}
import java.util.Scanner;
公共级鸟类2
{
公共静态void main(字符串[]参数)
{
birdInput();
系统出口(0);
}
公共静态无效birdInput()
{
扫描仪=新的扫描仪(System.in);
String[]birds=新字符串[9999999];
int[]数字=新int[9999999];
int i=0;
int-maxIndex;
while(i数字、长度))
{
maxIndex=i;
i++;
}
if(birds[i].toUpperCase().equals(“END”))
{
System.out.print(“\n好吧……我想谢谢您使用这个程序?\n”);
System.out.print(“\n您看到的最常见的鸟是“+birds[maxIndex]+”,其中“+numbers[maxIndex]+”总共被看到\n”);
系统出口(0);
}
}
}
您正在循环的每个迭代中声明鸟类
和数字
数组。它们应该在循环之前只声明和初始化一次
我改变了很多,所以我将在这里解释我的全部改变
首先,我必须将数组定义从上面提到的while循环中移出,因为从其他方面来说,每次都会覆盖这些数组。
我还使他们可以在全球范围内以其他方式与他们合作
总的来说,我重新构造了整个代码,使其更具可读性,更面向对象。
例如,我创建了一个名为inputCheck()的方法,它以字符串形式返回输入,并检查它是否等于END,这样就不必为此编写两次逻辑。(它还考虑在检查输入“if(input.toUpperCase().equals(“end”)”)之前,只需将输入的大小写为end lower或uppercase)
现在,每次需要这样的输入时都可以调用此方法:
但是如果你想从中得到一个整数,你需要小心,你首先必须像这样解析它:integer.parseInt(inputCheck())
之后,我编写了一个方法来搜索数字数组中的最大值并获取其索引:
为了解决您的上一个问题(超过10次输入后崩溃),我更改了您的while循环。因为您的阵列只有10个位置可以放置东西,所以如果您尝试将信息放置在第11个位置,它将崩溃。看起来不是这样的:
while(我不太担心第二种方法。这一部分还没有完成。我正在尝试首先完成第一种方法。你正在每个循环上重新创建数组,为什么?看,我不确定它是否正确,但我正在尝试将用户输入的内容输入到我以前的数组中。因此,他们输入的鸟和看到的数字将被放入birds和numbers数组中。此外,由于您在birds[i]END中写入END,因此您使用同一单元格来读取最常见的数据……当然,您将获得此输出。首先您需要搜索数组中的最大值(索引介于0和i-1之间)。然后打印此索引处的值(更改数组声明后)这是个好主意,但您需要了解这意味着什么`=newstring[]`。你每次都重写数组,所以你丢失了数据。我不确定它是否正确,但我尝试将用户输入的内容输入到我以前的数组中。因此,他们输入的bird和看到的数字将被放入bird和numbers数组中。您好。我尝试过处理此代码,但重新格式化的方式没有成功真的很有效。你有没有可能用我能管理的方式来更新它?我现在检查一下并测试一下。我昨天没有IDE,因为我有一个外部会议,不得不使用笔记本电脑。这一切看起来很棒,只是尝试了一下,效果很好。最后一点帮助。有没有一种方法可以在没有全局变量的情况下做到这一点?我确实设法做到了把它修改一下,这样就没有了,但是现在我正在努力发送变量。如果你想知道我首先做了什么,请告诉我,如果你能帮忙的话。再次感谢你。是的,如果你不介意的话,就把它发到这里。我可以在半小时内查看它。我已经发布了我编辑过的内容。我试着解决没有全局变量的问题把整个代码放在一个地方,但仍然不起作用。请指出我哪里出错了。
public static int maxIndex;
public static String[] birds = new String[10];
public static int[] numbers = new int[10];
static String inputCheck() {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END")) {
end();
}
return input;
}
birds[i] = inputCheck();
public static int getMaxIndex(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
int newnumber = numbers[i];
if ((newnumber > numbers.length)) {
maxIndex = i;
}
}
return maxIndex;
}
public static void end() {
maxIndex = getMaxIndex(numbers);
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
public static void birdInput() {
int i = 0;
while (i <= birds.length) {
System.out.println("What bird did you see?");
birds[i] = inputCheck();
System.out.println("How many did you see?");
numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash
i++;
}
}
import java.util.Scanner;
public static int maxIndex;
public static String[] birds = new String[10];
public static int[] numbers = new int[10];
public static void main(String[] param) {
birdInput();
most();
System.exit(0);
}
public static void birdInput() {
int i = 0;
while (i <= birds.length) {
System.out.println("What bird did you see?");
birds[i] = inputCheck();
System.out.println("How many did you see?");
numbers[i] = Integer.parseInt(inputCheck()); //you should check here if its actuall a number otherwiese your programm will crash
i++;
}
}
static String inputCheck() {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.toUpperCase().equals("END")) {
end();
}
return input;
}
public static int getMaxIndex(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
int newnumber = numbers[i];
if ((newnumber > numbers.length)) {
maxIndex = i;
}
}
return maxIndex;
}
public static void end() {
maxIndex = getMaxIndex(numbers);
System.out.print("\nWell....I guess thanks for using this program?\n");
System.out.print("\nThe most common bird that you saw was the " + birds[maxIndex] + " with " + numbers[maxIndex] + " being seen in total\n");
System.exit(0);
}
public static void most() {
System.out.println("fdff");
}
}