我正在尝试为我的几个朋友构建一个排序算法,并且我现在正在为其进行测试构建。最终我会让它变得更加动态,但目前我只是将它接受到用户输入姓名和输入的任何数字的位置。但是,当我运行代码时,它要求输入名称,但随后它会按照输入数字值的指示正确终止。我已经处理这个问题半个小时了,说实话,我有点困惑。
我的代码如下:
//Sorter.java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Sorter {
public static void dict() {
// Create a dictionary
Map<String, Range> dictionary = new HashMap<>();
// Add groups and their corresponding value ranges
dictionary.put("Family A", new Range(0, 10));
dictionary.put("Family B", new Range(11, 20));
dictionary.put("Family C", new Range(21, 30));
// Retrieve the value range for a specific group
Range rangea = dictionary.get("Family A");
System.out.println("Value range for Family A: " + rangea.getStart() + " - " + rangea.getEnd());
Range rangeb = dictionary.get("Family B");
System.out.println("Value range for Family B: " + rangeb.getStart() + " - " + rangeb.getEnd());
Range rangec = dictionary.get("Family C");
System.out.println("Value range for Family C: " + rangec.getStart() + " - " + rangec.getEnd());
}
public static String getName() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your name: ");
if (scanner.hasNextLine()) {
String nameInput = scanner.nextLine();
System.out.println("Hello, " + nameInput + ".");
return nameInput;
} else {
// handle no input available
return null;
}
}
}
/**
* Prompts the user to enter a numerical value and prints the corresponding family group.
*/
public static void counter() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a numerical value: ");
if (scanner.hasNextLine()) {
String userInputString = scanner.nextLine();
try {
int userInput = Integer.parseInt(userInputString);
String whatFamily = getGroup(userInput);
System.out.println("Family: " + whatFamily);
} catch (NumberFormatException e) {
System.out.println("No Family LOL get rekt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Determines the group based on the given value.
*
* @param value the value to be evaluated
* @return the group corresponding to the value
*/
public static String getGroup(int value) {
// Check if the value is less than 0
if (value < 0) {
return "Family A";
}
// Check if the value is within the range of Family A
else if (value >= 1 && value <= 10) {
return "Family A";
}
// Check if the value is within the range of Family B
else if (value >= 11 && value <= 20) {
return "Family B";
}
// Check if the value is within the range of Family C
else if (value >= 21 && value <= 30) {
return "Family C";
}
// Check if the value is greater than 30
else if (value > 30) {
return "Family C";
}
// Value is outside the range of any family
else {
return "No Family LOL get rekt";
}
}
}
class Range {
private int start;
private int end;
public Range(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
}
//Exec.java
import java.util.HashMap;
import java.util.Map;
public class Exec {
/**
* Initializes a dictionary with groups and their corresponding value ranges.
* Prints the value range for each group and prompts user input.
*
* @param args command line arguments
*/
public static void main(String[] args) {
// Prompt user input
Sorter.getName();
Sorter.dict();
Sorter.counter();
}
}
您的多个扫描仪导致出现问题。只需使用一个并将其传递给方法即可。
// Prompt user input
Scanner scanner = new Scanner(System.in);
Sorter.getName(scanner);
Sorter.dict();
Sorter.counter(scanner);