简体   繁体   English

为什么两次输入相同的数字会导致我的程序失败?

[英]Why does inputting the same number twice cause my program to fail?

I am just starting out learning Java and I am in the process of working through a much more efficient program to solve this problem, but I would like to understand why entering the same number twice causes the program to not run all the way through? 我刚开始学习Java,并且正在研究一个效率更高的程序来解决此问题,但是我想了解为什么两次输入相同的数字会导致程序无法完全运行?

I need to write a program where the user enters three numbers and they get sorted in non-descending order. 我需要编写一个程序,其中用户输入三个数字,并且它们以非降序排列。

import java.util.Scanner;

public class ModThreeReorder{
public static void main(String []args){

java.util.Scanner input = new java.util.Scanner(System.in);

int num1;
int num2;
int num3;

System.out.println("Please enter three (3) numbers");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();

System.out.println("Thanks. You entered " + num1 + ", " + num2 + ", " +    num3 + ".");

if (num1 > num2 && num1 > num3){
    if (num2 > num3 || num2 == num3){
        System.out.println("Your numbers in non-descending order are "  + num3 + ", " + num2 + ", " + num1 + ".");
    }
    else if (num3 > num2 || num3 == num2){
        System.out.println("Your numbers in non-descending order are " + num2 + ", " + num3 + ", " + num1 + ".");
    }
}
else if (num2 > num1 && num2 > num3){
    if (num1 > num3 || num1 == num3){
        System.out.println("Your numbers in non-descending order are "  + num3 + ", " + num1 + ", " + num2 + ".");
    }
    else if (num3 > num1 || num3 == num1){
        System.out.println("Your numbers in non-descending order are " + num1 + ", " + num3 + ", " + num2 + ".");


                }   
            }
else if (num3 > num1 && num3 > num2){
    if (num1 > num2 || num1 == num2){
        System.out.println("Your numbers in non-descending order are " + num2 + ", " + num1 + ", " + num3 + ".");
    }
    else if (num2 > num1 || num2 == num1){
        System.out.println("Your numbers in non-descending order are " + num1 + ", " + num2 + ", " + num3 + ".");

        }
      }
     }
   }

You have to clean the buffer, try to place a scanner.nextLine(); 您必须清理缓冲区,尝试放置一个scanner.nextLine(); after each nextInt() . 在每个nextInt() This method returns the rest of the current line, excluding any line separator at the end. 此方法返回当前行的其余部分,但不包括最后的任何行分隔符。 The position is set to the beginning of the next line. 该位置设置为下一行的开始。 Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present. 由于此方法继续在输入中搜索以寻找行分隔符,因此,如果不存在行分隔符,它可以缓冲所有要搜索的输入以跳过该行。

You can't directly take three ints without clearing the buffer in some way. 如果不通过某种方式清除缓冲区,就无法直接取三个整数。 This is a friendly way i like to do it 这是我喜欢的一种友好方式

System.out.println("Please enter number one :: ");
num1 = input.nextInt();
System.out.println("Please enter number two :: ");
num2 = input.nextInt();
System.out.println("Please enter number three :: ");
num3 = input.nextInt(); 

This will give you the results you expected. 这将为您提供预期的结果。

Another flaw in your program is the logic in determining which number is the smallest. 程序中的另一个缺陷是确定哪个数字最小的逻辑。 You should have something similar to this : 您应该有类似以下内容:

if(num1 >= num2 && num1 >= num3){
    if(num2 >= num3)
        System.out.println(num3 +" "+ num2 + " "+ num1);
    else
        System.out.println(num2+" "+num3+" "+num1);
}else if(num2 >= num1 && num2 >= num3){
    if(num1 >= num3)
        System.out.println(num3 +" "+ num1 + " "+ num2);
    else
        System.out.println(num1 +" "+ num3 + " "+ num1);
}else{
    if(num1 >= num2)
        System.out.println(num2 +" "+ num1 + " "+ num3);
    else
        System.out.println(num2 +" "+ num1 + " "+ num3);
}

So putting this all together will allow you to take your three int inputs correctly, then sort and display them in descending order. 因此,将所有这些放在一起可以使您正确地输入三个int输入,然后以降序对其进行排序和显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM