【问题标题】:How to create dynamic array in java with unclear and diffrent inpu INDEXes?java - 如何在java中创建具有不明确和不同输入索引的动态数组?
【发布时间】:2015-03-25 22:51:10
【问题描述】:

我是 Java 新手,我需要动态数组...我发现的所有东西都是用于动态数组的,我们应该使用“数组列表”,这没关系,但是当我希望索引是从输入给出的 X 的幂时,我面临ERORR!..索引不清楚,也没有指定什么是一次或二次幂!....谁能帮我解决它?

    public static void main(String[] args) throws Exception {
    Scanner Reader = new Scanner(System.in);
    ArrayList<Float> Zarayeb = new ArrayList<Float>();  
    Float s ;
    int m;
    System.out.print("Add Count of equation Sentences : ");
    int N = Reader.nextInt();
    if (N == 0)
            return;
    for (int i = 0; i < N  ; i++) {
        s = Reader.nextFloat() ;
        System.out.print("x^");
        m = Reader.nextInt();
        if (Zarayeb.get(m)== null)
            Zarayeb.add(0 , s);
        else{
            Float l ;
            l = Zarayeb.get(m);
            Zarayeb.add (m , l+s);
        }
        if (i < N-1)
            System.out.print("\r+");
    }
    System.out.print("Add Count of equation Sentences : ");
    N = Reader.nextInt();
    if (N == 0)
            return;
    for (int i = 0; i < N  ; i++) {
        s = Reader.nextFloat() ;
        System.out.print("x^");
        m = Reader.nextInt();
        if (Zarayeb.get(m)== null)
            Zarayeb.add(m , s);
        else{
            Float l ;
            l = Zarayeb.get(m);
            Zarayeb.add (m , l+s);
        }
        if (i < N-1)
            System.out.print("\r+");
    }
    System.out.print("Enter X: ");
    float X = Reader.nextFloat();
    float Sum = 0;
    for (int i = 0; i < Zarayeb.size();i++) {
    Sum += (Zarayeb.get(i) * Math.pow(X,i));
}
System.out.println("\nThe final answer is : " + Sum);

【问题讨论】:

  • when I want the indexes were the power of X that given from input the program comes with error .. the indexes are unclear and that is not specified what is the first or 2th power 一点都不清楚,请改写。
  • , I face ERORR ! .. 什么错误?复制确切的错误(如果是运行时 - 完整的堆栈跟踪)
  • @Sami 你为什么不接受我的回答?

标签: java arrays dynamic indexing


【解决方案1】:

首先,我稍微重构了您的代码以理解它:

具有顶层逻辑的主类:

import java.util.Scanner;

public class Main {

    private Scanner scanner;
    private final Totals totals = new Totals();

    public static void main(final String[] args) {
        final Main app = new Main();
        app.run();
    }

    private void run() {
        scanner = new Scanner(System.in);
        try {
            readAndProcessEquationSentences();
        } finally {
            scanner.close();
        }
    }

    private void readAndProcessEquationSentences() {
        readSentences(true);
        readSentences(false);
        System.out.println("The final answer is : " + totals.calculateSum(readBaseInput()));
    }

    private void readSentences(final boolean useInitialLogic) {
        System.out.print("Enter number of equation sentences:");
        final int numberOfSentences = scanner.nextInt();
        if (numberOfSentences == 0) {
            throw new RuntimeException("No sentences");
        }
        for (int i = 0; i < numberOfSentences; i++) {
            Sentence sentence = Sentence.read(scanner);
            if (useInitialLogic) {
                totals.addInitialSentence(sentence);
            } else {
                totals.addNextSentence(sentence);
            }
            if (i < numberOfSentences - 1) {
                System.out.print("\r+");
            }
        }
    }

    private float readBaseInput() {
        System.out.print("Enter base: ");
        return scanner.nextFloat();
    }
}

代表用户输入的一个等式句子的句子类:

import java.util.Scanner;

public class Sentence {
    private Float x;
    private int y;

    public static Sentence read(final Scanner scanner) {
        final Sentence sentence = new Sentence();
        System.out.println("Enter x^y");
        System.out.print("x=");
        sentence.x = scanner.nextFloat();
        System.out.println();
        System.out.print("y=");
        sentence.y = scanner.nextInt();
        System.out.println();
        return sentence;
    }

    public Float getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

跟踪总数的Totals类:

import java.util.ArrayList;
import java.util.List;

public class Totals {

    private final List<Float> values = new ArrayList<Float>();

    public void addInitialSentence(final Sentence sentence) {
        if (values.size() <= sentence.getY()) {
            addToStart(sentence);
        } else {
            addToValue(sentence);
        }
    }

    private void addToStart(final Sentence sentence) {
        values.add(0, sentence.getX());
    }

    public void addNextSentence(final Sentence sentence) {
        if (values.size() <= sentence.getY()) {
            values.add(sentence.getY(), sentence.getX());
        } else {
            addToValue(sentence);
        }
    }

    private void addToValue(final Sentence sentence) {
        Float total = values.get(sentence.getY());
        total = total + sentence.getX();
        values.add(sentence.getY(), total);
    }

    public float calculateSum(final float base) {
        float sum = 0;
        for (int i = 0; i < values.size(); i++) {
            sum += (values.get(i) * Math.pow(base, i));
        }
        return sum;
    }
}

我完全不知道这应该做什么。我根据这个模糊的想法命名了变量。

您让用户在两个单独的循环中输入值,逻辑略有不同,我称之为“初始”和“下一个”。

在最初的循环中你是这样做的:

    if (Zarayeb.get(m) == null)
        Zarayeb.add(0 , s);

在下一个循环中:

    if (Zarayeb.get(m) == null)
        Zarayeb.add(m , s);

这有问题,因为如果 m 超出或范围,ArrayList.get(m) 将抛出 IndexOutOfBoundException。所以我把它改成了:

    if (Zarayeb.size() <= m) {
        ....
    }

但是,在“下一个”情况下,这仍然不能解决问题。当输入 'm' 值而 ArrayList 中尚不存在任何元素时,在第二个循环中应该发生什么?

为什么要分两个循环输入句子?

具体应该实现什么逻辑?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多