英文:
Java program to find the second smallest number in console input
问题
我真的在努力理解这个问题,但没有帮助的话就无法继续下去。
我的逻辑是正确的,但出于某些原因,它无法正确执行。
在执行时,我想输入一行数字,例如10 12 5 9 3,程序应该返回第二小的数字。
因为我想先控制基本情况,所以我不打算使用除了已使用的两个之外的任何其他导入的类。
如果有人能够解释一下为什么这不起作用,我将非常感谢。
package secondSmallest;
import java.util.Scanner;
import java.io.PrintStream;
public class secondSmallest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PrintStream out = new PrintStream(System.out);
int smallest = 1;
int secsmallest = 0;
int hold = 0;
while(scan.hasNext()) {
hold = scan.nextInt();
if (hold < smallest) {
secsmallest = smallest;
smallest = hold;
} else {
hold = scan.nextInt();
}
out.printf(" %d", secsmallest);
}
}
}
英文:
I am really trying to grasp this problem but could not continue further without help.
My logic is in order but for some reason it will not execute properly.
On execution I want to enter a line of numbers for example 10 12 5 9 3 and the program should return me the second smallest number.
As I want to control the basics first I am refraining from using any other imported classes except for the two used.
If someone could shed some light on why this does not work I would be very grateful.
package secondSmallest;
import java.util.Scanner;
import java.io.PrintStream;
public class secondSmallest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PrintStream out = new PrintStream(System.out);
int smallest = 1;
int secsmallest = 0;
int hold = 0;
while(scan.hasNext()); {
hold = scan.nextInt();
if (hold < smallest) {
smallest = secsmallest;
smallest = hold;
} else {
hold = scan.nextInt();
}
out.printf(" %d", secsmallest);
}
}
}
答案1
得分: 2
首先:
我的逻辑是正确的,但由于某种原因它不能正确执行。
意味着你的逻辑不正确(除非有拼写错误、语法错误或其他阻止完美结果的错误);
其次:
Scanner#hasNext()
,你将其用作 while
循环的条件:
如果此扫描仪在其输入中有另一个标记,则返回 true。此方法在等待输入进行扫描时可能会阻塞。扫描仪不会超过任何输入。
而你应该在某个地方明确指示何时希望结束 while
循环。在你的示例中,你的循环会无限地继续,因为它没有任何基本情况。即使“Enter”键也是一种数据,按下它将不断输入新行控制字符;
第三:
你将最小值初始化为 1
,这不是一种清晰的设计,将常量静态分配给当前最小值。考虑一种可能性,当你的输入不同时;
第四:
你正在循环内部打印 secsmallest
,我猜这不是你想做的;
第五:
通过在你的 else
块中读取 hold = scan.nextInt();
,你实际上省略了一个输入,因为当你的 while
循环向前迭代一步时,你会有另一个 hold = scan.nextInt();
,然后你跳过一次迭代;
第六:
有许多设计“查找第二小”算法的方法(首先对其进行排序,然后取第二个元素;引入两个指针;等等),但如果你坚持要遵循与你的方式相似的方法,这将按预期工作:
public class Main {
public static void main(String[] args) {
int[] arr = {10, 12, 5, 9, 32, 5, 123, 4, -34, 12, -534, -53, -1, 432, 53};
int res = secondSmallest(arr);
System.out.println(res);
}
public static int secondSmallest(int[] arr) {
int smallest = arr[0];
int secsmallest = arr[1];
int i = 2;
while (i < arr.length-1) {
int current = arr[i];
if (current < smallest) {
secsmallest = smallest;
smallest = current;
}
else if(current < secsmallest) {
secsmallest = current;
}
i++;
}
return secsmallest;
}
}
输出结果:
-53
英文:
First of all:
> My logic is in order but for some reason it will not execute properly.
means that your logic is not in order (unless there is just a typo, or other syntax error, blocking flawless result);
Second:
Scanner#hasNext()
, which you have as a while
condition:
> Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
and you should, somewhere, somehow, indicate when you want your while
loop to end. In your example, your loop is going on infinitely, as it does not have any base case. Even the "Enter" keystroke is a data, and pressing it will keep entering new-line control character;
Third:
You have your smallest initialized as 1
, which is not really a clean design to statically assign the constant to the current minimum. Think of a possibility when your input is different;
Fourth:
You are printing the secsmallest
inside your while loop, which, I suppose, is not what you meant to do;
Fifth:
By reading in your else
block hold = scan.nextInt();
you are effectively ommiting one input, as the moment your while iterates one step forward, you have another hold = scan.nextInt(); and you jump one iteration
;
Sixth:
There are many ways to design "find-second-smallest" algorithm (sorting it first and then taking the second element; introducing two pointers; etc.), but if you insist to follow something close to your way, this works as expected:
public class Main {
public static void main(String[] args) {
int[] arr = {10, 12, 5, 9, 32, 5, 123, 4, -34, 12, -534, -53, -1, 432, 53};
int res = secondSmallest(arr);
System.out.println(res);
}
public static int secondSmallest(int[] arr) {
int smallest = arr[0];
int secsmallest = arr[1];
int i = 2;
while (i < arr.length-1) {
int current = arr[i];
if (current < smallest) {
secsmallest = smallest;
smallest = current;
}
else if(current < secsmallest) {
secsmallest = current;
}
i++;
}
return secsmallest;
}
}
Outputting:
-53
答案2
得分: -1
你的程序应该像这样:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PrintStream out = new PrintStream(System.out);
int smallest = Integer.MAX_VALUE;
int secSmallest = smallest;
int hold = 0;
while (scan.hasNextInt()) {
hold = scan.nextInt();
if (hold < smallest) {
secSmallest = smallest;
smallest = hold;
} else if (hold < secSmallest) secSmallest = hold;
}
out.printf(" %d", secSmallest);
}
英文:
Your program should look like this
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PrintStream out = new PrintStream(System.out);
int smallest = Integer.MAX_VALUE;
int secSmallest = smallest;
int hold = 0;
while (scan.hasNextInt()) {
hold = scan.nextInt();
if (hold < smallest) {
secSmallest = smallest;
smallest = hold;
} else if (hold < secSmallest) secSmallest = hold;
}
out.printf(" %d", secSmallest);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论