使用foreach循环,从一个ArrayList添加值,然后打印出来使用访问 [英] Using a foreach loop to add values from an arraylist, and then print them using accessors

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

问题描述

前一个问题:<一href=\"http://stackoverflow.com/questions/15048404/assigning-probability-to-a-random-enum-object\">Assigning概率随机枚举对象

所以我有这样的:

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());  
    }


}

我要回答这个问题:

I need to answer this:

在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.

出于某种原因,它始终打印国会必须进行选择。 经进一步调查所有诠释DEM值,印第安纳州和代表始终为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.

存取方法是一个基本的State.class,有一个构造函数和三个存取方法来访问每个参数的状态对象的一部分。这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数组列表。我这样做是为了在该文件中的每一行文本。

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;

    }


}

状态数据的例子是弗洛里亚29民主党人。

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

推荐答案

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

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

如果(a.getWinner()。等于(Party.DEMOCRAT))

不是

如果(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循环,从一个ArrayList添加值,然后打印出来使用访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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