gpt4 book ai didi

java - 重复该程序,直到用户在数组中键入 none

转载 作者:行者123 更新时间:2023-12-01 09:24:57 25 4
gpt4 key购买 nike

我试图重复运行我的代码,直到用户不输入任何内容。当我输入州名时,我需要它来制作州花和州鸟一次,而不会出现任何错误消息(再试一次)。但因为我写了行 for(int i=0; i<stateInfo.length; i++) ,它重复“重试”49 次,并在第 n 行给出答案,其中状态位于数组上。我需要通过仅打印州花和州鸟来使其工作。

//import Scanner, time.Duration, time.Instant
import java.util.Scanner;
import java.time.Duration;
import java.time.Instant;

public class StateFlowerBird {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

//Time counts starts
Instant start = Instant.now();
//greeting user
greetings();

//construct Scanner
Scanner scannerIn = new Scanner(System.in);
String state = "";

do {
//2D Arrays with State, state flower, and state bird
String[][] stateInfo = new String[][]{
{"Alabama", "Camellia", "Yellowhammer"},
{"Alaska", "Forget-me-not", "Willow Ptarmigan"},
{"Arizona", "Saguaro cactus", "Cactus Wren"},
{"Arkansas", "Apple blossom", "Mockingbird"},
{"California", "Golden poppy", "California Valley Quail"},
{"Colorado", "Rocky Mountain Columbine", "Lark Bunting"},
{"Connecticut", "Mountain laurel", "Robin"},
{"Delaware", "Peach blossom", "Blue Hen Chicken"},
{"Florida", "Orange blossom", "Mockingbird"},
{"Georgia", "Cherokee rose", "Brown Thrasher"},
{"Hawaii", "Hibiscus", "Nene"},
{"Idaho", "Syringa", "Mountain Bluebird"},
{"Illinois", "Native violet", "Cardinal"},
{"Indiana", "Peony", "Cardinal"},
{"Iowa", "Wild rose", "Eastern Goldfinch"},
{"Kansas", "Native sunflower", "Western Meadowlark"},
{"Kentucky", "Goldenrod", "Cardinal"},
{"Louisiana", "Magnolia", "Eastern Brown"},
{"Maine", "Pine cone & tassle", "Chickadee"},
{"Maryland", "Black Eyed Susan", "Baltimore Oriole"},
{"Massachusettes", "Mayflower", "Chickadee"},
{"Michigan", "Apple blossom", "Robin"},
{"Minnesota", "Lady slipper", "Common Loon"},
{"Mississippi", "Magnolia", "Mockingbird"},
{"Missouri", "Hawthorn", "Bluebird"},
{"Montana", "Bitterroot", "Western Meadowlark"},
{"Nebraska", "Goldenrod", "Western Meadowlark"},
{"Nevada", "Sagebrush", "Mountain Bluebird"},
{"New Hampshire", "Purple lilac", "Purple Finch"},
{"New Jersey", "Purple violet", "Eastern Goldfinch"},
{"New Mexico", "yucca", "Roadrunner"},
{"New York", "Rose", "Bluebird"},
{"North Carolina", "Dogwood", "Cardinal"},
{"North Dakota", "Wild prairie rose", "Western Meadowlark"},
{"Ohio", "Scarlet carnation", "Cardinal"},
{"Oklahoma", "Mistletoe", "Scissor-tailed"},
{"Oregon", "Oregon Grape", "Western Meadowlark"},
{"Pennsylvania", "Mountain laurel", "Ruffed Grouse"},
{"Rhode Island", "Violet", "Rhode Island Red"},
{"South Carolina", "Yellow jessamine", "Great Carolina Wren"},
{"South Dakota", "Pasque flower", "Ring-necked Pheasant"},
{"Tennesssee", "Purple iris", "Mockingbird"},
{"Texas", "Texas Blue Bonnet", "Mockingbird"},
{"Utah", "Sego lily", "American Seagull"},
{"Vermont", "Red clover", "Hermit Thrush"},
{"Virginia", "Dogwood", "Cardinal"},
{"Washington", "Western rhododendron", "Willow Goldfinch"},
{"West Virginia", "Rhododendron", "Cardinal"},
{"Wisconsin", "Wood violet", "Robin"},
{"Wyoming", "Indian paint brush", "Western Meadowlark"}};

state = "";
//Ask user to type the name of state or none
System.out.println("\nEnter a State or None to exit: " + state);
state = scannerIn.nextLine();

for(int stateNum = 0;stateNum<stateInfo.length;stateNum++){
if(stateInfo[stateNum][0].equalsIgnoreCase(state)){
System.out.println("Flower: " +stateInfo[stateNum][1]
+ "\nBird: " + stateInfo[stateNum][2]);
}
else if(!stateInfo[stateNum][0].equalsIgnoreCase(state)){
errorMessage();
}
}

} while(!state.equalsIgnoreCase("None"));
if (state.equalsIgnoreCase("None")){
endStatement();
}
Instant end = Instant.now();
System.out.println("Elapsed time in seconds was: "
+ Duration.between(start, end).toNanos()/1_000_000_000.0
+ " seconds.");
System.out.println("**********************************");
System.exit(0);
}


private static void greetings() {
System.out.println("*****************************************************");
System.out.println("***** Let's find out the state flower and bird! *****");
}
private static void endStatement() {
System.out.println("\n***** Thank you for playing! *****");
}

private static void errorMessage() {
System.out.println("Invalid State. Try it again.");
}
}

最佳答案

在 if 条件中使用“break”。一旦找到匹配,循环就会中断。

关于java - 重复该程序,直到用户在数组中键入 none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39909455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com