使用 foreach 循环从数组列表中添加值,然后使用访问器打印它们 [英] Using a foreach loop to add values from an arraylist, and then print them using accessors

查看:33
本文介绍了使用 foreach 循环从数组列表中添加值,然后使用访问器打印它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一个问题:为随机枚举对象分配概率

所以我有这个:

import java.util.List;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Integer;

public class ElectoralCollege {
    public static final String FILE = "Electoral201X.txt";
    private ArrayList <State> stateVotes;

    Random rand = new Random();
    List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);

    public ElectoralCollege() throws IOException    {

        stateVotes = new ArrayList<State>();
    }

    public void assignStates() throws IOException   {

        File f = new File(FILE);
        Scanner fReader = new Scanner(f);

        while(fReader.hasNext())    {

            String stateData = fReader.nextLine();
            int stateEnd = stateData.indexOf(" - ");
            String stateName = stateData.substring(0, stateEnd);
            String numVote = stateData.substring(stateEnd + 2);

            Party winner = parties.get(rand.nextInt(5));

            State voteInfo = new State(stateName, Integer.parseInt(numVote.trim()), winner);
            stateVotes.add(voteInfo);
        }

    }

    public void announceWinner()    {

        int dem = 0;
        int rep = 0;
        int ind = 0 ;

        for(State a : stateVotes)   {

            if(a.getWinner().equals(Party.DEMOCRAT))
                dem = dem + a.getNumVotes();
            else if(a.getWinner().equals(Party.REPUBLICAN))
                rep = rep + a.getNumVotes();
            else if(a.getWinner().equals(Party.INDEPENDENT))
                ind = ind + a.getNumVotes();

        }
            if(dem >= 270)
                System.out.println("DEMOCRATS WIN");
            else if(rep >= 270)
                System.out.println("REPUBLICANS WIN");
            else if(ind >= 270)
                System.out.println("INDEPENDENTS WIN");
            else
                System.out.println("CONGRESS MUST CHOOSE");


        for(State b : stateVotes)
            System.out.println(b.getName() + " " + b.getWinner());  
    }


}

我需要回答这个:

在announceWinner() 中查看stateVotes 并将总数加起来三个政党的选举人票.打印出总数每个政党的选举人票,如果任何政党的票数超过 270需要获胜,宣布该党为获胜者 - 否则宣布国会将不得不做出选择.最后,打印每个州的个人获胜者.

In announceWinner() look through stateVotes and add up the total electoral votes for each of the three parties. Print out the total electoral votes for each party and if any party has more than the 270 needed to win, announce that party as the winner - otherwise announce that Congress will have to make the choice. Finally, print the individual winner of each state.

出于某种原因,它总是打印 CONGRESS MUST CHOOSE. 进一步调查后,dem、ind 和 rep 的所有 int 值始终为 0. 不确定是否需要初始化变量或不像我那样做,但是当我不这样做时它一直给我错误.

For some reason it always prints CONGRESS MUST CHOOSE. Upon further investigation all int values of dem, ind and rep are always 0. Not sure if I need to initialize the variable or not like I did, but it kept giving me errors when I did not.

Accessor 方法都是基本 State.class 的一部分,它有一个构造函数和三个访问器方法来访问 state 对象中的每个参数.由哪个 stateVotes 组成.

The Accessor methods are all part of a basic State.class, that has a constructor and three accessor methods to access every parameter in the state object. Which stateVotes is made up of.

stateVotes 中的状态值也全部设置.从旧问题中,我从文件中读取了一行文本,将其制成状态对象,然后将其放入 stateVotes 数组列表中.我对那个文件中的每一行文本都这样做了.

State values in stateVotes are also all set. From the old question I read a line of text from a file, made it into a state object, then put it into the stateVotes arraylist. I did this for each line of text in that file.

第二个 foreach 循环根本不打印任何内容.

State 类很简单:

State class is simple enough:

public class State {

    private String Name;
    private int votes;
    private Party winningParty;

    public State(String stateName, int numVote, Party winner)   {

        Name = stateName;
        votes = numVote;
        winningParty = winner;

    }

    public Party getWinner()    {

        return winningParty;    
    }

    public String getName()     {

        return Name;
    }

    public int getNumVotes()    {

        return votes;

    }


}

州数据的示例是Floria 29 DEMOCRAT".

Example of the State data is "Floria 29 DEMOCRAT".

推荐答案

如果 Party.DEMOCRAT 返回一个字符串那么你需要使用

If Party.DEMOCRAT returns a string then you need to use

if(a.getWinner().equals(Party.DEMOCRAT))

不是

if(a.getWinner() == Party.DEMOCRAT)

这背后的基本原理是因为您正在比较字符串而不是值.字符串不能与 == 进行比较,您必须使用 .compair 或 .equals 来限定内容.

The rationale behind this is because you are comparing strings not values. Strings can not be compared with == you must use .compair or .equals to qualify the contents.

这篇关于使用 foreach 循环从数组列表中添加值,然后使用访问器打印它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆