这些方法还有其他代码方式吗?它根据某个数字要求用户输入 n 次

Is there any other way of code for the methods? It asks for user input n times based on a certain number

我正在制作这个 Study Schedule 程序,其中一部分是我询问用户有多少科目,然后他们说(例如)5。根据科目数量,它会问“有多少个科目,第n个科目叫什么名字。

我基本上已经写下了代码并且可以运行,但是有什么可以简化它的吗?感觉厚实且重复。此外,方法名称并不重要,因为这仅用于测试。

import java.util.*;

class NumberInput {
    public double NumberInput;
    public String SubjectName1;
    public String SubjectName2;
    public String SubjectName3;
    public String SubjectName4;
    public String SubjectName5;
    public String SubjectName6;

    public static void main(String[] args) {
        NumberInput wow = new NumberInput();
        wow.InputNumber();
        wow.InputString();
        wow.confirmResults();
    }

    public double DoubleInput(String numberQuestion) {
        Scanner input = new Scanner(System.in);
        System.out.println(numberQuestion);
        return input.nextDouble();
    }

    public String StringInput(String stringQuestion) {
        Scanner input = new Scanner(System.in);
        System.out.println(stringQuestion);
        return input.nextLine();
    }

    public double InputNumber() {
    NumberInput = DoubleInput("How many subjects would you like to study?");
    if (NumberInput > 6) {
        System.out.println("Maximum Subjects exceeded. At the most, please answer 6");
    }
    return (NumberInput);
    }

    public void InputString() {
    if (NumberInput == 1) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
    }

    if (NumberInput == 2) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
    }

    if (NumberInput == 3) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
    }

    if (NumberInput == 4) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
    }

    if (NumberInput == 5) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
        SubjectName5 = StringInput("What is the name of the fifth Subject: ");
    }

    if (NumberInput == 6) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
        SubjectName5 = StringInput("What is the name of the fifth Subject: ");
        SubjectName6 = StringInput("What is the name of the sixth Subject: ");
    }

    }

    public void confirmResults() {
        System.out.println(SubjectName1);
        System.out.println(SubjectName2);
        System.out.println(SubjectName3);
        System.out.println(SubjectName4);
        System.out.println(SubjectName5);
        System.out.println(SubjectName6);
    }

}
public String SubjectName1;
public String SubjectName2;
public String SubjectName3;
public String SubjectName4;
public String SubjectName5;
public String SubjectName6;

可以是字符串数组:

String[] SubjectName = new String[6];

还有:

 public double InputNumber()

正在返回一个在调用时未分配的号码:

//This Line calls a function which returns a double, but you don't store the double
wow.InputNumber();

//You need something like: 
double NumInputs = wow.InputNumber();

还有:

public void InputString()

可以是具有 numInputs 的循环:

//You dont really need to capture numInputs and pass it this way.
//You could just use the NumberInput you're setting in the function, but since you return a double I'm showing you the syntax for that.
public static void main(String[] args) {
    NumberInput wow = new NumberInput();
    double numInputs = wow.InputNumber();
    wow.InputString(numInputs);
    wow.confirmResults();
}
public void confirmResults(double numInputs){
    for(int i = 1; i<= numInputs; i++){
       System.out.println("What is the subject?"); 
       //etc
    }
}

confirmResults(),这可以是对 SubjectNames 数组的循环。

一个循环和一个枚举

当您遍历主题数量时,您可以使用枚举来获取 'first'、'second' 等值...

这是一个相当基本的例子:

public class Test {

    public static void main(String[] args) {
        int numberOfSubjects = inputNumber();
        String[] subjectNames = new String[numberOfSubjects - 1];
        for (int i = 0; i < numberOfSubjects; i++) {
            Scanner input = new Scanner(System.in);
            System.out.printf("What is the name of the %s Subject%n", Number.values()[i].getCount());
            subjectNames[i] = input.nextLine();
        }
        confirmResults();
    }

    private static void confirmResults() {
        // Your implementation
    }

    private static Integer inputNumber() {
        // You implementation
        return 5;
    }
}

enum Number {
    FIRST("first"),
    SECOND("second"),
    THIRD("third"),
    FOURTH("fourth"),
    FIFTH("fifth");
    
    private final String count;

    Number(String count) {
        this.count = count;
    }

    public String getCount() {
        return count;
    }
}

您应该始终关心命名内容,尤其是当您希望人们审查您的代码时。也就是说,我本可以为这个枚举找到一个比 'Number' 更好的名字。 XD

命名是编程中最难的部分,最好早点开始养成习惯。