打印用户输入/多维数组

时间:2018-02-25 23:01:51

标签: java if-statement multidimensional-array

如果这是一个重复的问题,我道歉。我不知道该找什么。

这是一项任务,所以我真的希望推动正确的方向,而不仅仅是得到答案。代码是反复要求用户输入返回州鸟和州花的州名。一旦用户输入"无,"打印出包含用户输入的每个州的州,鸟和花的摘要。目前,摘要仅打印用户输入的最后状态的信息。

我为这项草率的工作感到抱歉。我才开始学习!

# within each group repeat each point 
# then slice the first and last out and 
# add a variable called linegroup, which provides grouping for start and endpoints of each line
troops %<>% group_by(group) %>%
  slice(rep(1:n(), each = 2)) %>%
  slice(-c(1, n())) %>%
  mutate(linegroup = lapply(1:(n()/2), function(x) rep(x, 2)) %>% unlist) %>% 
  ungroup

# create linestring sf object by summarizing the points, 
# grab the last survivor and direction value of each group (i.e. the 'endpoint' value)
troops_line <- st_as_sf(troops, coords = c("long", "lat"), crs = 4326) %>%
  group_by(group, linegroup) %>%
  summarise(survivors = last(survivors), direction = last(direction), do_union = FALSE) %>%
  st_cast("LINESTRING")

gp <- ggplot(troops_line) +
  geom_sf(aes(color = direction, size = survivors), show.legend = "line") 

这是第二节课:

public class StateInformation {

    private String[][] stateInfo =  {
        {"Alabama", "Yellowhammer", "Camellia"},
        {"Alaska", "Willow Ptarmigan", "Alpine Forget-Me-Not"},
        {"Arizona" , "Cactus Wren", "Saguaro Cactus Blossom"},
        {"Arkansas", "Northern Mockingbird", "Apple Blossom"},
        {"California", "California Quail", "California Poppy"},
        {"Colorado", "Lark Bunting", "Rocky Mountain Columbine"},
        {"Connecticut", "American Robin", "Mountain Laurel"},
        {"Delaware", "Blue Hen Chicken", "Peach Blossom"},
        {"Florida", "Northern Mockingbird", "Orange Blossom"},
        {"Georgia", "Brown Thrasher", "Cherokee Rose"},
        {"Hawaii", "Nene", "{Pua Aloalo"},
        {"Idaho", "Mountain Bluebird", "Syringa"},
        {"Illinois", "Greater Prairie-Chicken", "Violet"},
        {"Indiana", "Northern Cardinal", "Peony"},
        {"Iowa", "Eastern Goldfinch", "Wild Rose"},
        {"Kansas", "Weatern Meadowlark", "Wild Native Sunflower"},
        {"Kentucky", "Northern Cardinal", "Goldenrod"},
        {"Louisana", "Brown Pelican", "Louisana Iris"},
        {"Maine", "Black-Capped Chickadee", "White Pine Cone and Tassel"},
        {"Maryland", "Baltimore Oriole", "Black-Eyed Susan"},
        {"Massachusetts", "Black-Capped Chickadee", "Mayflower"},
        {"Michigan", "American Robin", "Apple Blossom"},
        {"Minnesota", "Common Loon", "Pink and White Lady Slipper"},
        {"Mississippi", "Northern Mockingbird", "Magnolia"},
        {"Missouri", "Eastern Bluebird", "White Hawthorn Blossom"},
        {"Montana", "Western Meadowlark", "Bitterroot"},
        {"Nebraska", "Western Meadowlark", "Goldenrod"},
        {"Neveda", "Mountain Bluebird", "Sagebrush"},
        {"New Hampshire", "Purple Finch", "Pink Lady's Slipper"},
        {"New Jersey", "Eastern Goldfinch", "Violet"},
        {"New Mexico", "Greater Roadrunner", "Yucca"},
        {"New York", "Eastern Bluebird", "Rose"},
        {"North Carolina", "Northern Cardinal", "Dogwood"},
        {"North Dakota", "Western Meadowlark", "Wild Prairie Rose"},
        {"Ohio", "Northern Cardinal", "White Trillium"},
        {"Oklahoma", "Scissor-Tailed Flycatcher", "Mistletoe"},
        {"Oregon", "Western Meadowlark", "Oregon Grape"},
        {"Pennslyvania", "Ruffed Grouse", "Mountain Laurel"},
        {"Rhode Island", "Rhode Island Red Chicken", "Violet"},
        {"South Carolina", "Carolina Wren", "Yellow Jessamine"},
        {"South Dakota", "Ring-necked Pheasant", "American Pasque"},
        {"Tennessee", "Northern Mockingbird", "Passion Flower"},
        {"Texas", "Northern Mockingbird", "Ennis"},
        {"Utah", "California Gull", "Sego Lily"},
        {"Vermont", "Hermit Thrush", "Red Clover"},
        {"Virginia", "Northern Cardinal", "American Dogwood"},
        {"Washington", "Willow Goldfinch", "Coast Rhododendron"},
        {"West Virginia", "Northern Cardinal", "Rhododendron"},
        {"Wisconsin", "American Robin", "Wood Violet"},
        {"Wyoming", "Western Meadowlark", "Indian Paintbrush"},
    };//End array initialization

    public StateInformation() {
    }

    public String[][] getStateInfo(){
       return stateInfo;
    }

    public void setState(String[][] state) {
        this.stateInfo = stateInfo;
    }
}
//End of Class

2 个答案:

答案 0 :(得分:2)

要获取活动摘要,您必须将每个州存储在一个集合中。这里使用Set来避免重复 显示存储在Set中的每个状态的示例代码,然后迭代Set:

public class TestStateInformation {
public static void main(String[] args) {

    StateInformation states = new StateInformation();
    String[][] state = states.getStateInfo();
    // Inserting scanner & variable.
    Scanner scanner = new Scanner(System.in);
    Set<Integer> positions = new HashSet<Integer>();

    while (true) {
        // Prompt the user to input a state name.
        System.out.println("Please enter a state or None to exit: ");
        // Read the state entered by user, including leading/trailing white
        // spaces.
        String stateInfo = scanner.nextLine();

        // If loop to end if None is entered.
        if (stateInfo.equalsIgnoreCase("None")) {
            System.out.println(
                    "***** Thank you! *****" + '\n' + "A summary report for each State, Bird, and Flower is: \n");

            for (Integer p : positions) {
                System.out.println(getState(p, state) + ", " + getBird(p, state) + ", " + getFlower(p, state)
                                + '\n');
            }
            System.out.println("Please visit our site again!");
            break;
        } else {
            int position = getStatePosition(state, stateInfo);
            if (position != -1) {
                positions.add(position);
                System.out.println("New size=" + positions.size());
                System.out.println("Bird: " + getBird(position, state) + '\n' + "Flower: "
                        + getFlower(position, state) + '\n');
            }
        }
    }
}// end while loop

使用示例输出:

***** Thank you! *****
A summary report for each State, Bird, and Flower is: 

Alaska, Willow Ptarmigan, Alpine Forget-Me-Not

California, California Quail, California Poppy

Florida, Northern Mockingbird, Orange Blossom

Please visit our site again!

答案 1 :(得分:0)

你似乎并不太遥远。你可以按照要求写出每个州的信息 - 所以你所遗漏的是最后的那些摘要吗?

您需要的是某种数据结构,以便存储有关用户询问的状态的信息。那么你需要为每个问题存储的是一个数字 - 即该状态的索引。如果您能够将所有这些存储在列表中,那么最后,您可以简单地遍历该列表,并打印出每个列表的相关信息。

由于像String[]这样的常规数组无法调整大小,因此您可能希望使用类似ArrayList<Integer>的内容。每次用户请求有关新状态的信息时,使用它来存储新的信息行。

注意:这假设您需要记录用户要求的内容;如果你不想在用户事实上多次要求相同的状态时重复相同的状态,你可以使用一些Dictionary的实现,这将只允许每个状态的一个实例包含在最终列表。

希望这足以帮助你......